-
Notifications
You must be signed in to change notification settings - Fork 2
/
application.cpp
670 lines (593 loc) · 16.8 KB
/
application.cpp
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*
******************************************************************************
application.cpp
VCU1200_Base
Erik Stafl, edited by Alex Hernandez
1/23/2015, Edited: 3/7/2023
Latest edit: 4/5/23
Written for Tiva TM4C123BH6PGE
Language: C++
Copyright (c) 2013-2016 Stafl Systems, LLC. All rights reserved, lol
******************************************************************************
*/
#include "application.h"
// Main Application Object
Application application;
// Reference to Board Object
extern VCU1200_Board board;
Application::Application()
{
}
void Application::initialize()
{
// Set CAN Termination Resistor Setting
board.setCANTermination(CAN_1, CAN_1_TERMINATION);
board.setCANTermination(CAN_2, CAN_2_TERMINATION);
// Initialize Inputs for this Application
initializeInputs();
// Initialize CAN Receive
initializeCANReceive();
// Set Initial State to Startup
changeState(STARTUP);
}
void Application::tick()
{
// Process State Machine
processState();
//Blink the Lights With User Input
calculateLEDS();
// Send CAN Data
sendCANData();
// Increment Counters
incrementCounters();
}
//blinkLights Action for the LightsOn State
void Application::calculateLEDS()
{
if (state == SYSTEM_ON)
{
/* //ON
float OFF_LEDS;
for(int i = 0; i < potentiometer_command; i++)
{
setOutput(LED[i], ON);
}
//OFF LEDS
OFF_LEDS = 14 - potentiometer_command;
for(int i = 0; i < OFF_LEDS; i++)
{
setOutput(LED[i], OFF);
}
//potentiometer_command = getAnalogInput(POTENTIOMETER);
//code below maps potentiometer
if(potentiometer_command == 14)
{
setOutput(LED1, ON);
setOutput(LED2, ON);
setOutput(LED3, ON);
setOutput(LED4, ON);
setOutput(LED5, ON);
setOutput(LED6, ON);
setOutput(LED7, ON);
setOutput(LED8, ON);
setOutput(LED9, ON);
setOutput(LED10, ON);
setOutput(LED11, ON);
setOutput(LED12, ON);
setOutput(LED13, ON);
setOutput(LED14, ON);
}
else if(potentiometer_command == 13)
{
setOutput(LED1, ON);
setOutput(LED2, ON);
setOutput(LED3, ON);
setOutput(LED4, ON);
setOutput(LED5, ON);
setOutput(LED6, ON);
setOutput(LED7, ON);
setOutput(LED8, ON);
setOutput(LED9, ON);
setOutput(LED10, ON);
setOutput(LED11, ON);
setOutput(LED12, ON);
setOutput(LED13, ON);
setOutput(LED14, OFF);
}
else if(potentiometer_command == 12)
{
setOutput(LED1, ON);
setOutput(LED2, ON);
setOutput(LED3, ON);
setOutput(LED4, ON);
setOutput(LED5, ON);
setOutput(LED6, ON);
setOutput(LED7, ON);
setOutput(LED8, ON);
setOutput(LED9, ON);
setOutput(LED10, ON);
setOutput(LED11, ON);
setOutput(LED12, ON);
setOutput(LED13, OFF);
setOutput(LED14, OFF);
}
else if(potentiometer_command == 11)
{
setOutput(LED1, ON);
setOutput(LED2, ON);
setOutput(LED3, ON);
setOutput(LED4, ON);
setOutput(LED5, ON);
setOutput(LED6, ON);
setOutput(LED7, ON);
setOutput(LED8, ON);
setOutput(LED9, ON);
setOutput(LED10, ON);
setOutput(LED11, ON);
setOutput(LED12, OFF);
setOutput(LED13, OFF);
setOutput(LED14, OFF);
}
else if(potentiometer_command == 10)
{
setOutput(LED1, ON);
setOutput(LED2, ON);
setOutput(LED3, ON);
setOutput(LED4, ON);
setOutput(LED5, ON);
setOutput(LED6, ON);
setOutput(LED7, ON);
setOutput(LED8, ON);
setOutput(LED9, ON);
setOutput(LED10, ON);
setOutput(LED11, OFF);
setOutput(LED12, OFF);
setOutput(LED13, OFF);
setOutput(LED14, OFF);
}
else if(potentiometer_command == 9)
{
setOutput(LED1, ON);
setOutput(LED2, ON);
setOutput(LED3, ON);
setOutput(LED4, ON);
setOutput(LED5, ON);
setOutput(LED6, ON);
setOutput(LED7, ON);
setOutput(LED8, ON);
setOutput(LED9, ON);
setOutput(LED10, OFF);
setOutput(LED11, OFF);
setOutput(LED12, OFF);
setOutput(LED13, OFF);
setOutput(LED14, OFF);
}
else if(potentiometer_command == 8)
{
setOutput(LED1, ON);
setOutput(LED2, ON);
setOutput(LED3, ON);
setOutput(LED4, ON);
setOutput(LED5, ON);
setOutput(LED6, ON);
setOutput(LED7, ON);
setOutput(LED8, ON);
setOutput(LED9, OFF);
setOutput(LED10, OFF);
setOutput(LED11, OFF);
setOutput(LED12, OFF);
setOutput(LED13, OFF);
setOutput(LED14, OFF);
}
else if(potentiometer_command == 7)
{
setOutput(LED1, ON);
setOutput(LED2, ON);
setOutput(LED3, ON);
setOutput(LED4, ON);
setOutput(LED5, ON);
setOutput(LED6, ON);
setOutput(LED7, ON);
setOutput(LED8, OFF);
setOutput(LED9, OFF);
setOutput(LED10, OFF);
setOutput(LED11, OFF);
setOutput(LED12, OFF);
setOutput(LED13, OFF);
setOutput(LED14, OFF);
}
else if(potentiometer_command == 6)
{
setOutput(LED1, ON);
setOutput(LED2, ON);
setOutput(LED3, ON);
setOutput(LED4, ON);
setOutput(LED5, ON);
setOutput(LED6, ON);
setOutput(LED7, OFF);
setOutput(LED8, OFF);
setOutput(LED9, OFF);
setOutput(LED10, OFF);
setOutput(LED11, OFF);
setOutput(LED12, OFF);
setOutput(LED13, OFF);
setOutput(LED14, OFF);
}
else if(potentiometer_command == 5)
{
setOutput(LED1, ON);
setOutput(LED2, ON);
setOutput(LED3, ON);
setOutput(LED4, ON);
setOutput(LED5, ON);
setOutput(LED6, OFF);
setOutput(LED7, OFF);
setOutput(LED8, OFF);
setOutput(LED9, OFF);
setOutput(LED10, OFF);
setOutput(LED11, OFF);
setOutput(LED12, OFF);
setOutput(LED13, OFF);
setOutput(LED14, OFF);
}
else if(potentiometer_command == 4)
{
setOutput(LED1, ON);
setOutput(LED2, ON);
setOutput(LED3, ON);
setOutput(LED4, ON);
setOutput(LED5, OFF);
setOutput(LED6, OFF);
setOutput(LED7, OFF);
setOutput(LED8, OFF);
setOutput(LED9, OFF);
setOutput(LED10, OFF);
setOutput(LED11, OFF);
setOutput(LED12, OFF);
setOutput(LED13, OFF);
setOutput(LED14, OFF);
}
else if(potentiometer_command == 3)
{
setOutput(LED1, ON);
setOutput(LED2, ON);
setOutput(LED3, ON);
setOutput(LED4, OFF);
setOutput(LED5, OFF);
setOutput(LED6, OFF);
setOutput(LED7, OFF);
setOutput(LED8, OFF);
setOutput(LED9, OFF);
setOutput(LED10, OFF);
setOutput(LED11, OFF);
setOutput(LED12, OFF);
setOutput(LED13, OFF);
setOutput(LED14, OFF);
}
else if(potentiometer_command == 2)
{
setOutput(LED1, ON);
setOutput(LED2, ON);
setOutput(LED3, OFF);
setOutput(LED4, OFF);
setOutput(LED5, OFF);
setOutput(LED6, OFF);
setOutput(LED7, OFF);
setOutput(LED8, OFF);
setOutput(LED9, OFF);
setOutput(LED10, OFF);
setOutput(LED11, OFF);
setOutput(LED12, OFF);
setOutput(LED13, OFF);
setOutput(LED14, OFF);
}
else if(potentiometer_command == 1)
{
setOutput(LED1, ON);
setOutput(LED2, OFF);
setOutput(LED3, OFF);
setOutput(LED4, OFF);
setOutput(LED5, OFF);
setOutput(LED6, OFF);
setOutput(LED7, OFF);
setOutput(LED8, OFF);
setOutput(LED9, OFF);
setOutput(LED10, OFF);
setOutput(LED11, OFF);
setOutput(LED12, OFF);
setOutput(LED13, OFF);
setOutput(LED14, OFF);
}
else if(potentiometer_command == 0)
{
setOutput(LED1, OFF);
setOutput(LED2, OFF);
setOutput(LED3, OFF);
setOutput(LED4, OFF);
setOutput(LED5, OFF);
setOutput(LED6, OFF);
setOutput(LED7, OFF);
setOutput(LED8, OFF);
setOutput(LED9, OFF);
setOutput(LED10, OFF);
setOutput(LED11, OFF);
setOutput(LED12, OFF);
setOutput(LED13, OFF);
setOutput(LED14, OFF);
}
*/
}
}
void Application::changeState(SystemState new_state)
{
switch (new_state)
{
case STARTUP:
//Set Default Actions for this state
setOutput(LED1, OFF);
setOutput(LED2, OFF);
setOutput(LED3, OFF);
setOutput(LED4, OFF);
setOutput(LED5, OFF);
setOutput(LED6, OFF);
setOutput(LED7, OFF);
setOutput(LED8, OFF);
setOutput(LED9, OFF);
setOutput(LED10, OFF);
setOutput(LED11, OFF);
setOutput(LED12, OFF);
setOutput(LED13, OFF);
setOutput(LED14, OFF);
// Bootloader is OK in this State
board.setBootloaderSafe(true);
// Override is OK in this State
board.setOutputOverrideSafe(true);
break;
case SYSTEM_OFF:
//Set Default Actions for this state
setOutput(LED1, OFF);
setOutput(LED2, OFF);
setOutput(LED3, OFF);
setOutput(LED4, OFF);
setOutput(LED5, OFF);
setOutput(LED6, OFF);
setOutput(LED7, OFF);
setOutput(LED8, OFF);
setOutput(LED9, OFF);
setOutput(LED10, OFF);
setOutput(LED11, OFF);
setOutput(LED12, OFF);
setOutput(LED13, OFF);
setOutput(LED14, OFF);
//setOutput(POTENTIOMETER_5V, OFF); //gives error
// Bootloader is OK in this State
board.setBootloaderSafe(true);
// Override is OK in this State
board.setOutputOverrideSafe(true);
break;
case SYSTEM_ON:
//Analog Controlled Lights
//Actions Defined in calculateLEDS function called in tick() function
//setOutput(POTENTIOMETER_5V, ON);
// Bootloader is OK in this State
board.setBootloaderSafe(true);
// Override is OK in this State
board.setOutputOverrideSafe(true);
break;
}
// Update Current State
state = new_state;
// Reset State Counter every 1 second
state_counter = 0;
}
void Application::incrementCounters()
{
state_counter++;
// Parent Class Function
CANReceiver::incrementCounters();
}
void Application::initializeCANReceive()
{
// Temporary Mailbox for Setup
tCANMsgObject can_rx_message;
// Setup Receive Messages on CAN Interface
can_rx_message.ui32MsgID = 0x300; //message ID for Rx to VCU
can_rx_message.ui32MsgIDMask = 0x700; // The message identifier mask used when identifier filtering is enabled.
can_rx_message.ui32Flags = MSG_OBJ_RX_INT_ENABLE | MSG_OBJ_USE_ID_FILTER;
board.can.can_1.initializeReceiveMessage(this, &can_rx_message);
/*
// Messages from MiniMon
can_rx_message.ui32MsgID = 0x300;
can_rx_message.ui32MsgIDMask = 0x700;
can_rx_message.ui32Flags = MSG_OBJ_RX_INT_ENABLE | MSG_OBJ_USE_ID_FILTER;
board.can.can_1.initializeReceiveMessage(this, &can_rx_message);
*/
}
bool Application::receiveCANMessage(CANPort can_port, tCANMsgObject* message, unsigned int mailbox)
{
// True if Message has been Processed
bool processed = false;
uint16_t tempvar;
//int16_t stemp;
// 0-8 Userinput Lights to Turn on
if (message->ui32MsgID == 0x300 )//if it's the designated ID for VCU Rx, i.e. 0x300
{
//potentiometer_command = message->pui8MsgData[0]; //stick the value as an ascii char, resolution 128?
processed = true;
}
//temporary function for idAddress
if (message->ui32MsgID == idAddress){
parseCANBytes(message);
}
if(message->ui32MsgID == 0x0A5){
tempvar = message->pui8MsgData[0];
tempvar << 8 ;
tempvar += message->pui8MsgData[1];
//process tempvar into function
//maybe clear tempvar, i.e. tempvar = 0;
//maybe not, depends on state machine needs and use of computational power
//
//do again for bytes 2 and 3
}
if (processed)
{
last_message_counter = 0;
message_received = true;
}
return processed;
}
//
void Application::parseCANbytes(tCANMsgObject* message){
}
void Application::sendCANData()
{
uint16_t utemp;
// CAN Message Object for Transmission
CANMessage msg(CANMessage::ID_STANDARD, CANMessage::DATA_FRAME, 8);
// every 100th tick
if (board.getStartupCounter() % 100 == 0)
{
//MESSAGE # 1 Primary System Status (ID 0x200)
msg.setID(0x200);
msg.setLength(1);
//Alex's Message #2
msg.setID(0x201); //set program status, ID
msg.setLength(6); // setting message length to be char array of size 6
//getData method returns char array of size 8, named data, can assign a value using "="
msg.getData() [0] = 1;
msg.getData() [1] = 1;
msg.getData() [2] = 1;
msg.getData() [3] = 1;
msg.getData() [4] = 1;
msg.getData() [5] = 1;
board.can.sendMessage(CAN_1, &msg);
/*
// Current Analog Voltage Input
utemp = getAnalogInput(POTENTIOMETER); //also adds error; tracing code, it likely returns a low voltage; commenting out
//utemp = 3.5; //temporarily hardcoding 3.5V as utemp
msg.getData()[1] = utemp >> 8; //why the fuck does this bitshift by 2^3? getAnalogInput returns a float, but fuck it I guess
// bitshift by 8 for a 16bit unsigned int
msg.getData()[0] = utemp;
//commenting out 479
msg.getData()[1] =
// Send Message
board.can.sendMessage(CAN_1, &msg);
//MESSAGE #2 Program Status (ID 0x201)
msg.setID(0x201);
msg.setLength(2);
// On/Off Input Status
msg.getData()[1] = getInput(ON_OFF_SWTICH);
// Command Reference
msg.getData()[0] = current_command.command_reference;
// Send Message
board.can.sendMessage(CAN_1, &msg);*/
/* //MESSAGE#3 XY Stage Commanded Position (ID 0x212)
msg.setID(0x212);
msg.setLength(8);
// X Commanded Position
msg.getData()[7] = x_axis.getCommandedPosition() >> 24;
msg.getData()[6] = x_axis.getCommandedPosition() >> 16;
msg.getData()[5] = x_axis.getCommandedPosition() >> 8;
msg.getData()[4] = x_axis.getCommandedPosition();
// Y Commanded Position
msg.getData()[3] = y_axis.getCommandedPosition() >> 24;
msg.getData()[2] = y_axis.getCommandedPosition() >> 16;
msg.getData()[1] = y_axis.getCommandedPosition() >> 8;
msg.getData()[0] = y_axis.getCommandedPosition();
// Send Message
board.can.sendMessage(CAN_1, &msg);
*/
/* //MESSAGE #4 Input States (ID 0x250)
msg.setID(0x201);
msg.setLength(2);
// On/Off Input Status
msg.getData()[1] = getInput(ON_OFF_SWTICH);
// Command Reference
msg.getData()[0] = current_command.command_reference;
// Send Message
board.can.sendMessage(CAN_1, &msg);
//MESSAGE#3 XY Stage Commanded Position (ID 0x212)
msg.setID(0x212);
msg.setLength(8);
// X Commanded Position
msg.getData()[7] = x_axis.getCommandedPosition() >> 24;
msg.getData()[6] = x_axis.getCommandedPosition() >> 16;
msg.getData()[5] = x_axis.getCommandedPosition() >> 8;
msg.getData()[4] = x_axis.getCommandedPosition();
// Y Commanded Position
msg.getData()[3] = y_axis.getCommandedPosition() >> 24;
msg.getData()[2] = y_axis.getCommandedPosition() >> 16;
msg.getData()[1] = y_axis.getCommandedPosition() >> 8;
msg.getData()[0] = y_axis.getCommandedPosition();
// Send Message
board.can.sendMessage(CAN_1, &msg);
//MESSAGE #4 Input States (ID 0x250)
msg.setID(0x250);
msg.setLength(6);
// Reserved
msg.getData()[5] = 0xFF;
// Joystick Position
msg.getData()[4] = joystick;
// Turn Knob Position
msg.getData()[3] = turn_knob;
// Input States 3
msg.getData()[2] = secondary_vcu.getInput(SecondaryVCU::FOOT_PEDAL_1);
msg.getData()[2] |= secondary_vcu.getInput(SecondaryVCU::FOOT_PEDAL_2) << 1;
msg.getData()[2] |= secondary_vcu.getInput(SecondaryVCU::X_HLFP) << 2;
msg.getData()[2] |= secondary_vcu.getInput(SecondaryVCU::Y_HLFP) << 3;
msg.getData()[2] |= secondary_vcu.getInput(SecondaryVCU::LEFT_ELECTRODE_SW) << 4;
msg.getData()[2] |= secondary_vcu.getInput(SecondaryVCU::RIGHT_ELECTRODE_SW) << 5;
// Input States 2
msg.getData()[1] = getInput(WELDER_GOOD);
msg.getData()[1] |= getInput(WELDER_NG) << 1;
msg.getData()[1] |= getInput(WELDER_END) << 2;
msg.getData()[1] |= getInput(WELDER_CAUTION) << 3;
msg.getData()[1] |= getInput(WELDER_COUNT_UP) << 4;
msg.getData()[1] |= getInput(WELDER_READY) << 5;
msg.getData()[1] |= getInput(START_BUTTONS) << 6;
msg.getData()[1] |= secondary_vcu.getInput(SecondaryVCU::LIGHT_CURTAIN_IN) << 7;
// Input States 1
msg.getData()[0] = getInput(KEYSWITCH);
msg.getData()[0] |= getInput(YELLOW_BUTTON) << 1;
msg.getData()[0] |= getInput(BLUE_BUTTON) << 2;
msg.getData()[0] |= getInput(E_STOP) << 3;
msg.getData()[0] |= getInput(X_POS_LIMIT) << 4;
msg.getData()[0] |= getInput(X_NEG_LIMIT) << 5;
msg.getData()[0] |= getInput(Y_POS_LIMIT) << 6;
msg.getData()[0] |= getInput(Y_NEG_LIMIT) << 7;
// Send Message
board.can.sendMessage(CAN_1, &msg);*/
}
}
void Application::processState()
{
switch (state)
{
case STARTUP:
if (state_counter >= MIN_STATE_TIME)
{
changeState(SYSTEM_ON);
}
break;
case SYSTEM_OFF:
if(state_counter >= MIN_STATE_TIME)
{/*
if(getInput(ON_OFF_SWITCH))
{
changeState(SYSTEM_ON);
}
*/
}
break;
case SYSTEM_ON:
if(state_counter >= MIN_STATE_TIME)
{/*
if(!getInput(ON_OFF_SWITCH))
{
changeState(SYSTEM_OFF);
}
*/
}
break;
}
}