aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Stubs Collection/Procedures/Mid tank calibration.pproj
blob: 655bbd09954e2586567d22b3334760f025b146bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*
 * StubRealTimeUsage.h
 *
 *  Created on: 15 ���� 2018
 *      Author: shlomo
 */

#ifndef MODULES_STUBS_HANDLER_STUBREALTIMEUSAGE_H_
#define MODULES_STUBS_HANDLER_STUBREALTIMEUSAGE_H_


void Stub_RealTimeUsageRequest(MessageContainer* requestContainer);


#endif /* MODULES_STUBS_HANDLER_STUBREALTIMEUSAGE_H_ */
ef='#n222'>222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Drawing;
using Google.Protobuf;
using Tango.BL.Entities;
using Tango.BL.Enumerations;
using Tango.PMR.Stubs;
using Tango.PMR.Diagnostics;
using Tango.FSE.Common.Connection;
using Tango.FSE.Common.Diagnostics;
using Tango.FSE.Procedures;
using System.IO;
using System.Windows.Forms;
using Tango.PMR.EmbeddedParameters;

public class Program
{
const double	MIN_SLOP=0.7;
const double	MAX_SLOP=1.4;
const double	ERROR_SLOP=-1;
const double	MIN_50cc_on_volt=2.3;
const double	MAX_50cc_on_volt=3.2;
const double	ERROR_50cc_on_volt=-1;

int [] Convert_Midtank_No = new Int32 [8] {1,3,5,7,2,4,6,8};
uint [,] ADC_Add_Chnl = new uint [8,2]	{
												{0x44,0x20},				
												{0x44,0x08},				
												{0x46,0x80},
												{0x46,0x20},
												{0x44,0x10},
												{0x44,0x04},
												{0x46,0x40},
												{0x46,0x10},
										};


const Int32	F3_VALVE_OUT	=	0x600008E2	;
string Machine_SN	;
private const string FILE_PATH = @"C:\MidTankLog\";
string File_name;
double Full_Cartridge=1000;	// on CC 
double 	Slop=0.92;
//----------------------
const Int32 I2C_ID =  2;
//const Int32 I2C_Slave_Add = 0x46; //must be 0x40, 0x44 or 0x46
//----------------------


private IProcedureContext context;




    public void OnExecute(IProcedureContext context)
    {
      

	Int32 Timout ;
	Int32 Interval;
	Int32 midtankno = 0;
	double Full_Point=0;			//on Voltage
	double resualt;
	double Sum=0 ;
	double Old_Sum=0 ;
	double Start_Point=0;
	

	if (!Directory.Exists(FILE_PATH))
		{
			Directory.CreateDirectory(FILE_PATH);
		}			



    var response =context.RequestUserInputFor<String>("Enter Midtank S/N: ", "");
	context.Write("\n");
	Machine_SN = response;
	context.WriteLine(Machine_SN);
	File_name= FILE_PATH +Machine_SN + DateTime.Now.ToString("MM_dd_yyyy_HH_mm_ss")+".csv";
	context.Write(File_name);
 	context.WriteToFile(File_name,"MidTank S/N:," + Machine_SN);
	

    var response1 =context.RequestUserInputFor<String>("Enter Midtank number to calibrate  (1-8):", "");
   context.WriteLine(response1);
	midtankno = Int32.Parse(response1);
 	context.AppendToFile(File_name,"MidTank Number:\t" + response1);
	if (midtankno==8)
	{
		Slop=1;
		Timout=60;		//60 minutes
		Interval=60;	//60 second
	}
	else
	{
		Slop=0.92;
		Timout=10*2;		//10 minutes
		Interval=30;	//30 second

	}
    var response10 =context.RequestUserInputFor<String>("Enter Cartridge Valume on CC  (500-1500):", "");
   context.WriteLine(response10);
	Full_Cartridge = double.Parse(response10);
 	context.AppendToFile(File_name,"Cartridge Valume:\t" + response10);
 	context.AppendToFile(File_name,"Slop value:\t" + Slop);


	DialogResult result = MessageBox.Show("Are you sure to calibrate Midtank No: "+ midtankno + " with "+ Full_Cartridge +" CC" , "Warning",MessageBoxButtons.YesNo);
	if(result == DialogResult.Yes)
	{



		string box_msg = "Please Insert cartridge ";

		MessageBox.Show(box_msg);
		
		open_valve(midtankno,1,1)		;	//open Air valve
		open_valve(midtankno,0,1)		;	//open Ink valve
		Int32 i=0; 
		while (i<Timout)					//check for 15 minitus 
		{
			Sum=0;
			for (Int32 j=0;j<Interval;j++)
			{
				resualt=read_pressure_sensor(midtankno);
				context.AppendToFile(File_name,resualt+",V");
				Sum=Sum+resualt;
				Thread.Sleep(1000);
			}
			
			
			if (i==0) 
				Start_Point=Sum/Interval;
			
			if (Sum>Old_Sum)
				Old_Sum=Sum-Old_Sum;
			else
				Old_Sum=Old_Sum-Sum;
		
   
			if ((check_valve(midtankno,1) != 0) && (check_valve(midtankno,0) != 0))
			{
				i++;
				if ( Old_Sum <0.02)	
					i=Timout+1;		//end 
				Old_Sum=Sum;
			}
			else
			{
			open_valve(midtankno,1,0)		;	//open Air valve
			open_valve(midtankno,0,0)		;	//open Ink valve
			Old_Sum=0;
			
			}	
		}
		Full_Point=Sum/Interval;
		

		close_valve(midtankno,1,1)		;	//close Air valve
		close_valve(midtankno,0,1)		;	//close Ink valve

			
		if (insert_slop_t_eeprom(Full_Point, midtankno,Start_Point)==0)
		{
				box_msg ="Turn Power Off and On to finish the calibration.";
				MessageBox.Show(box_msg);


    	context.AddResult(ResultType.Passed, "Test passed", "Test passed.");
				
		}
		else
		{
				box_msg ="Calibration error";
				MessageBox.Show(box_msg);


    	context.AddResult(ResultType.Failed, "Test Fail", "Test Fail.");
				
		}
	}

}


	int	insert_slop_t_eeprom( double FullPoint,Int32 midtankno, double StartPoint)
{
//	context.Write("\n ");

		StartPoint=FullPoint-StartPoint;
		StartPoint=StartPoint/Full_Cartridge;
		if ((StartPoint>0.7 ) && (StartPoint>1.3 ))
		{
			MidTankDataSetupRequest midTankDataSetupRequest = new MidTankDataSetupRequest();

			MidTankData midTankData = new MidTankData();
			midTankData.MidTankId = Convert.ToUInt32 (midtankno-1);
			midTankData.InitialOffsetA =FullPoint - (Full_Cartridge/1000-0.1)/Slop;
			midTankData.SlopeB = Slop;
			
			context.Write("\nMidtank No:" + midtankno + "=\tVoltage Zero is  ="+(FullPoint - (Full_Cartridge/1000-0.1)/Slop)+"\tSlop="+ Slop);
			context.AppendToFile(File_name,"\nMidtank No:" + midtankno + "=\tVoltage Zero is  ="+(FullPoint - (Full_Cartridge/1000-0.1)/Slop)+"\tSlop="+ Slop);

			
			midTankDataSetupRequest.MidTankInfo.Add(midTankData);
		

			var response =context.Send<MidTankDataSetupResponse>(midTankDataSetupRequest);

			return	0;
		}
		else
			context.Write("\nError");
			context.AppendToFile(File_name,"\n Slop Error");
			return	1;
	

    }



int adc_configuration(uint I2C_Slave_Add,uint channel ) 
	{
	StubI2CWriteBytesRequest stubI2CWriteBytesRequest = new StubI2CWriteBytesRequest();
	stubI2CWriteBytesRequest.I2CId = I2C_ID;
	stubI2CWriteBytesRequest.SlaveAddress = I2C_Slave_Add;
	
	UInt32 uInt32 = new UInt32();
	stubI2CWriteBytesRequest.BytesTWrite.Add(0x00);//Byte 0 to write
	stubI2CWriteBytesRequest.BytesTWrite.Add(channel);//Byte 1 to Write, must be 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02 or 0x01 for different channels
	stubI2CWriteBytesRequest.BytesTWrite.Add(0x80);//Byte 2 to Write
	
	var response =context.Send<StubI2CWriteBytesResponse>(stubI2CWriteBytesRequest);
	
	return 1;
	}


int adc_set_for_read_ch(uint I2C_Slave_Add) 
	{
	StubI2CWriteBytesRequest stubI2CWriteBytesRequest = new StubI2CWriteBytesRequest();
	stubI2CWriteBytesRequest.I2CId = I2C_ID;
	stubI2CWriteBytesRequest.SlaveAddress = I2C_Slave_Add;
	
	UInt32 uInt32 = new UInt32();
	stubI2CWriteBytesRequest.BytesTWrite.Add(0x01);//Byte 0 to write

	var response =context.Send<StubI2CWriteBytesResponse>(stubI2CWriteBytesRequest);
	
	return 1;
	}




double adc_read_ch(uint I2C_Slave_Add) 
	{
	StubI2CReadBytesRequest stubI2CReadBytesRequest = new StubI2CReadBytesRequest();
	stubI2CReadBytesRequest.I2CId = I2C_ID;
	stubI2CReadBytesRequest.SlaveAddress = I2C_Slave_Add;
	stubI2CReadBytesRequest.NumberOfBytesToRead = 2; // Number of bytes to read

	var response1 =context.Send<StubI2CReadBytesResponse>(stubI2CReadBytesRequest);
	uint temph=0;
	uint templ=0;
	uint temp;
	double calc;
	double calc1;
	double calc2;
			temph=response1.ReadBytes[0];
			templ=response1.ReadBytes[1];
			temp = (temph << 8) | templ;
			temp=temp & 0x0fff;
				
			calc = (double)temp *2.5 / 4096.0;
		
			calc1 = (1.96- calc) * 4.64 + 1.96;
			context.Write("Vsensor [volts]:\t");
			context.Write(calc1 + "\n");

	
	return calc1;
	}

double		read_pressure_sensor(Int32 i )
	{
double resualt =0 ;
		   switch (i)
      {
        case 1:
			adc_configuration(0x44,0x20);		//a2d_address 0x44 channel channel 2
			adc_set_for_read_ch(0x44);
			resualt=adc_read_ch(0x44);
		break;
		
		case 2:
			adc_configuration(0x44,0x08);		//a2d_address 0x44 channel channel 4
			adc_set_for_read_ch(0x44);
			resualt=adc_read_ch(0x44);
		break;
		
		case 3:
			adc_configuration(0x46,0x80);		//a2d_address 0x46 channel channel 0
			adc_set_for_read_ch(0x46);
			resualt=adc_read_ch(0x46);
		break;
		
		case 4:
			adc_configuration(0x46,0x20);		//a2d_address 0x46 channel channel 2
			adc_set_for_read_ch(0x46);
			resualt=adc_read_ch(0x46);
		break;
		
		case 5:
			adc_configuration(0x44,0x10);		//a2d_address 0x44 channel channel 3
			adc_set_for_read_ch(0x44);
			resualt=adc_read_ch(0x44);
		break;
		
		case 6:
			adc_configuration(0x44,0x04);		//a2d_address 0x44 channel channel 5
			adc_set_for_read_ch(0x44);
			resualt=adc_read_ch(0x44);
		break;
		
		case 7:
			adc_configuration(0x46,0x40);		//a2d_address 0x46 channel channel 1
			adc_set_for_read_ch(0x46);
			resualt=adc_read_ch(0x46);
		break;
		
		case 8:
			adc_configuration(0x46,0x10);		//a2d_address 0x46 channel channel 3
			adc_set_for_read_ch(0x46);
			resualt=adc_read_ch(0x46);
		break;
		
        default:
        context.Write("Error");

        break;
	  }	
		
		return resualt;
	}	


//--------------------------------
int open_valve (int valve_nm,int valve_type,int check_valve)		//valve_type=0 is Air ,1 is Ink ; check_valve=1 send feedbak
{
	int temp1=Convert_Midtank_No[valve_nm-1]	;

	string temp;
	SetBit (F3_VALVE_OUT,(((valve_type+1)*8)- temp1), 1);	
	
		if (check_valve!=0)
		{
			temp="Air";
			if (valve_type==1)
			temp="Ink";
				
			DialogResult result = MessageBox.Show("Did " + temp + "Valve of Midtank " + valve_nm +" open?", "Warning",MessageBoxButtons.YesNo);
			if(result == DialogResult.No)
				{
//					Units_status[(valve_nm-1)*3+valve_type,1]="Fail";
					context.Write(temp +"Valve\tFail\n");	
					return 0;
				}
			else 
				{
//					Units_status[(valve_nm-1)*3+valve_type,1]="Pass";
					context.Write(temp +"Valve\tOpen\n");	
					return 1;
				}
			
		}
		return 1;

}
//--------------------------------	
int close_valve (int valve_nm,int valve_type,int check_valve)
{
	int temp1=Convert_Midtank_No[valve_nm-1];	

	string temp;
	SetBit (F3_VALVE_OUT,(((valve_type+1)*8)- temp1), 0);
	if (check_valve!=0)
		{
			temp="Air";
			if (valve_type==1)
			temp="Ink";
				
			DialogResult result = MessageBox.Show("Did " + temp +" Valve of Midtank " + valve_nm +" close?", "Warning",MessageBoxButtons.YesNo);
			if(result == DialogResult.No)
				{
//					Units_status[(valve_nm-1)*3+valve_type,1]="Fail";
					context.Write(temp +"Valve\tFail\n");	
					return 0;
				}
			else 
				{
//					Units_status[(valve_nm-1)*3+valve_type,1]="Pass";
					context.Write(temp +"Valve\tclose\n");	
					return 1;
				}			
		}
	return 1;

}

public Int32 SetBit(Int32 Adr, Int32 BitNo, Int32 Bit)
{
	Int32 BitMask;
	Int32 RetVal;
	RetVal = Fpga_Read_Reg(Adr);
	Int32 RV = RetVal  ;
	
	if (Bit == 0x1) 
	{
		BitMask = 0x1 << BitNo;
		RV = RV | BitMask;
		Fpga_Write_Reg( Adr, RV );
	}
	else if (Bit == 0x0)
	{
		BitMask = ~(0x1 << BitNo);
		RV = RV & BitMask;
		Fpga_Write_Reg(Adr, RV );	
	}
	return 1;
}

int Fpga_Write_Reg(Int32 Addr, Int32 Data)
{
	var response =context.Send<StubFpgaWriteRegResponse>("StubFpgaWriteRegRequest" ,Addr, Data);
	
	return 1;
}


Int32 check_valve (Int32 valve_nm,int valve_type)
{
	Int32 temp1=Convert_Midtank_No[valve_nm-1];	
	
    var response =context.Send<StubFpgaReadRegResponse>("StubFpgaReadRegRequest" ,F3_VALVE_OUT);
	Int32 RV =(Int32) response.Value & 0xffff ;
	RV = RV & ( 0x1 << (((valve_type+1)*8)- temp1));
	return RV;

}


Int32 Fpga_Read_Reg(Int32 Addr)
{
    
context.Write("123");

	var response =context.Send<StubFpgaReadRegResponse>("StubFpgaReadRegRequest" ,Addr);
	context.Write("456");

	Int32 RV= (Int32) response.Value & 0xffff;
	context.Write("789");

	return RV;
}    
    
    
    
}