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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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;
}
}
|