-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutosteer.ino
845 lines (690 loc) · 27 KB
/
Autosteer.ino
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
/*
UDP Autosteer code for Teensy 4.1
For AgOpenGPS
01 Feb 2022
Like all Arduino code - copied from somewhere else :)
So don't claim it as your own
*/
////////////////// User Settings /////////////////////////
//How many degrees before decreasing Max PWM
#define LOW_HIGH_DEGREES 3.0
/* PWM Frequency ->
490hz (default) = 0
122hz = 1
3921hz = 2
*/
#define PWM_Frequency 1
//WAS Calabration
float inputWAS[] = { -50.00, -45.0, -40.0, -35.0, -30.0, -25.0, -20.0, -15.0, -10.0, -5.0, 0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0 }; //Input WAS do not adjust
float outputWAS[] = { -50.00, -45.0, -40.0, -35.0, -30.0, -25.0, -20.0, -15.0, -10.0, -5.0, 0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0 };
/////////////////////////////////////////////
// if not in eeprom, overwrite
#define EEP_Ident 2400
// *********** Motor drive connections **************888
//Connect ground only for cytron, Connect Ground and +5v for IBT2
//Dir1 for Cytron Dir, Both L and R enable for IBT2
#define DIR1_RL_ENABLE 4
//PWM1 for Cytron PWM, Left PWM for IBT2
#define PWM1_LPWM 2
//Not Connected for Cytron, Right PWM for IBT2
#define PWM2_RPWM 33
#define PWM2_OPT 37
//--------------------------- Switch Input Pins ------------------------
#define STEERSW_PIN 6
#define WORKSW_PIN 7
#define REMOTE_PIN 8
//Define sensor pin for current or pressure sensor
#define CURRENT_SENSOR_PIN A17
#define PRESSURE_SENSOR_PIN A14
#define CONST_180_DIVIDED_BY_PI 57.2957795130823
#include <Wire.h>
#include <EEPROM.h>
#include "zADS1115.h"
ADS1115_lite adc(ADS1115_DEFAULT_ADDRESS); // Use this for the 16-bit version ADS1115
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN3, RX_SIZE_256, TX_SIZE_256> V_Bus; //Steering Valve Bus
boolean engageCAN = 0; //Variable for Engage from CAN
uint32_t Time; //Time Arduino has been running
uint32_t myTime;
uint32_t relayTime; //Time to keep "Button Pressed" from CAN Message
uint32_t lastpush;
long unsigned int lastIdActive = 0;
#include <IPAddress.h>
// ethernet
#include <NativeEthernet.h>
#include <NativeEthernetUdp.h>
//uint8_t Ethernet::buffer[200]; // udp send and receive buffer
uint8_t autoSteerUdpData[UDP_TX_PACKET_MAX_SIZE]; // Buffer For Receiving UDP Data
//loop time variables in microseconds
const uint16_t LOOP_TIME = 25; //40Hz
uint32_t autsteerLastTime = LOOP_TIME;
uint32_t currentTime = LOOP_TIME;
const uint16_t WATCHDOG_THRESHOLD = 100;
const uint16_t WATCHDOG_FORCE_VALUE = WATCHDOG_THRESHOLD + 2; // Should be greater than WATCHDOG_THRESHOLD
uint8_t watchdogTimer = WATCHDOG_FORCE_VALUE;
//Heart beat hello AgIO
uint8_t helloFromIMU[] = { 128, 129, 121, 121, 5, 0, 0, 0, 0, 0, 71 };
uint8_t helloFromAutoSteer[] = { 0x80, 0x81, 126, 126, 5, 0, 0, 0, 0, 0, 71 };
int16_t helloSteerPosition = 0;
//fromAutoSteerData FD 253 - ActualSteerAngle*100 -5,6, SwitchByte-7, pwmDisplay-8
uint8_t PGN_253[] = {0x80,0x81, 126, 0xFD, 8, 0, 0, 0, 0, 0,0,0,0, 0xCC };
int8_t PGN_253_Size = sizeof(PGN_253) - 1;
//fromAutoSteerData FD 250 - sensor values etc
uint8_t PGN_250[] = { 0x80,0x81, 126, 0xFA, 8, 0, 0, 0, 0, 0,0,0,0, 0xCC };
int8_t PGN_250_Size = sizeof(PGN_250) - 1;
uint8_t aog2Count = 0;
float sensorReading;
float sensorSample;
elapsedMillis gpsSpeedUpdateTimer = 0;
//EEPROM
int16_t EEread = 0;
//Relays
bool isRelayActiveHigh = true;
uint8_t relay = 0, relayHi = 0, uTurn = 0;
uint8_t tram = 0;
//Switches
uint8_t remoteSwitch = 0, workSwitch = 0, steerSwitch = 1, switchByte = 0;
//On Off
uint8_t guidanceStatus = 0;
uint8_t prevGuidanceStatus = 0;
bool guidanceStatusChanged = false;
//speed sent as *10
float gpsSpeed = 0;
//steering variables
float steerAngleActual = 0;
float steerAngleSetPoint = 0; //the desired angle from AgOpen
int16_t steeringPosition = 0; //from steering sensor
float steerAngleError = 0; //setpoint - actual
//pwm variables
int16_t pwmDrive = 0, pwmDisplay = 0;
float pValue = 0;
float errorAbs = 0;
float highLowPerDeg = 0;
//Steer switch button ***********************************************************************************************************
uint8_t currentState = 1, reading, previous = 0;
uint8_t pulseCount = 0; // Steering Wheel Encoder
bool encEnable = false; //debounce flag
uint8_t thisEnc = 0, lastEnc = 0;
//Variables for settings
struct Storage {
uint8_t Kp = 40; // proportional gain
uint8_t lowPWM = 10; // band of no action
int16_t wasOffset = 0;
uint8_t minPWM = 9;
uint8_t highPWM = 60; // max PWM value
float steerSensorCounts = 30;
float AckermanFix = 1; // sent as percent
}; Storage steerSettings; // 11 bytes
//Variables for settings - 0 is false
struct Setup {
uint8_t InvertWAS = 0;
uint8_t IsRelayActiveHigh = 0; // if zero, active low (default)
uint8_t MotorDriveDirection = 0;
uint8_t SingleInputWAS = 1;
uint8_t CytronDriver = 1;
uint8_t SteerSwitch = 0; // 1 if switch selected
uint8_t SteerButton = 0; // 1 if button selected
uint8_t ShaftEncoder = 0;
uint8_t PressureSensor = 0;
uint8_t CurrentSensor = 0;
uint8_t PulseCountMax = 5;
uint8_t IsDanfoss = 0;
uint8_t IsUseY_Axis = 0; //Set to 0 to use X Axis, 1 to use Y avis
}; Setup steerConfig; // 9 bytes
void steerConfigInit()
{
if (steerConfig.CytronDriver)
{
pinMode(PWM2_RPWM, OUTPUT);
pinMode(PWM2_OPT, OUTPUT);
}
}
void steerSettingsInit()
{
// for PWM High to Low interpolator
highLowPerDeg = ((float)(steerSettings.highPWM - steerSettings.lowPWM)) / LOW_HIGH_DEGREES;
}
void autosteerSetup()
{
//PWM rate settings. Set them both the same!!!!
/* PWM Frequency ->
490hz (default) = 0
122hz = 1
3921hz = 2
*/
if (PWM_Frequency == 0)
{
analogWriteFrequency(PWM1_LPWM, 490);
analogWriteFrequency(PWM2_RPWM, 490);
analogWriteFrequency(PWM2_OPT, 490);
}
else if (PWM_Frequency == 1)
{
analogWriteFrequency(PWM1_LPWM, 122);
analogWriteFrequency(PWM2_RPWM, 122);
analogWriteFrequency(PWM2_OPT, 122);
}
else if (PWM_Frequency == 2)
{
analogWriteFrequency(PWM1_LPWM, 3921);
analogWriteFrequency(PWM2_RPWM, 3921);
analogWriteFrequency(PWM2_OPT, 3921);
}
//keep pulled high and drag low to activate, noise free safe
pinMode(WORKSW_PIN, INPUT_PULLUP);
pinMode(STEERSW_PIN, INPUT_PULLUP);
pinMode(REMOTE_PIN, INPUT_PULLUP);
pinMode(DIR1_RL_ENABLE, OUTPUT);
// Disable digital inputs for analog input pins
pinMode(CURRENT_SENSOR_PIN, INPUT_DISABLE);
pinMode(PRESSURE_SENSOR_PIN, INPUT_DISABLE);
//set up communication
Wire1.end();
Wire1.begin();
// Check ADC
if(adc.testConnection())
{
Serial.println("ADC Connecton OK");
}
else
{
Serial.println("ADC Connecton FAILED!");
Autosteer_running = false;
}
//50Khz I2C
//TWBR = 144; //Is this needed?
EEPROM.get(0, EEread); // read identifier
if (EEread != EEP_Ident) // check on first start and write EEPROM
{
EEPROM.put(0, EEP_Ident);
EEPROM.put(10, steerSettings);
EEPROM.put(40, steerConfig);
EEPROM.put(60, networkAddress);
}
else
{
EEPROM.get(10, steerSettings); // read the Settings
EEPROM.get(40, steerConfig);
EEPROM.get(60, networkAddress);
}
steerSettingsInit();
steerConfigInit();
if (Autosteer_running)
{
Serial.println("Autosteer running, waiting for AgOpenGPS");
// Autosteer Led goes Red if ADS1115 is found
digitalWrite(AUTOSTEER_ACTIVE_LED, 0);
digitalWrite(AUTOSTEER_STANDBY_LED, 1);
}
else
{
Autosteer_running = false; //Turn off auto steer if no ethernet (Maybe running T4.0)
// if(!Ethernet_running)Serial.println("Ethernet not available");
Serial.println("Autosteer disabled, GPS only mode");
return;
}
adc.setSampleRate(ADS1115_REG_CONFIG_DR_128SPS); //128 samples per second
adc.setGain(ADS1115_REG_CONFIG_PGA_6_144V);
delay (3000);
CAN_setup();
}// End of Setup
void autosteerLoop()
{
ReceiveUdp();
// Loop triggers every 100 msec and sends back gyro heading, and roll, steer angle etc
currentTime = systick_millis_count;
if (currentTime - autsteerLastTime >= LOOP_TIME)
{
autsteerLastTime = currentTime;
//reset debounce
encEnable = true;
//If connection lost to AgOpenGPS, the watchdog will count up and turn off steering
if (watchdogTimer++ > 250)
{
watchdogTimer = WATCHDOG_FORCE_VALUE;
steerSwitch = 1; // reset values like it turned off
currentState = 1;
}
//read all the switches
workSwitch = digitalRead(WORKSW_PIN); // read work switch
//Engage steering via 1 PCB Button or 2 Tablet
// 1 PCB Button pressed?
reading = digitalRead(STEERSW_PIN);
if (steerConfig.SteerSwitch == 1)
{
// Switch is off so reset ready for next switch on
if (reading == HIGH)
{
currentState = 1;
steerSwitch = 1;
previous = reading;
}
}
// 2 Has tablet button been pressed?
if (guidanceStatusChanged)
{
if (guidanceStatus == 1) //Must have changed Off >> On
{
currentState = 0;
steerSwitch = 0;
}
}
// If AOG has stopped steering, wait then turn off steerswitch ready for next engage.
static int switchCounter = 0;
if (steerSwitch == 0 && guidanceStatus == 0)
{
if (switchCounter++ > 30)
{
currentState = 1;
steerSwitch = 1;
}
}
else
{
switchCounter = 0;
}
if (engageCAN) reading = LOW; //CAN Engage is ON (Button is Pressed)
// Arduino software button code
if (reading == LOW && previous == HIGH)
{
if (currentState == 1)
{
currentState = 0;
steerSwitch = 0;
}
else
{
currentState = 1;
steerSwitch = 1;
}
}
previous = reading;
// Encoder sensor?
if (steerConfig.ShaftEncoder && pulseCount >= steerConfig.PulseCountMax)
{
steerSwitch = 1;
currentState = 1;
previous = 0;
}
// Pressure sensor?
if (steerConfig.PressureSensor)
{
sensorSample = (float)analogRead(PRESSURE_SENSOR_PIN);
sensorSample *= 0.25;
sensorReading = sensorReading * 0.6 + sensorSample * 0.4;
if (sensorReading >= steerConfig.PulseCountMax)
{
steerSwitch = 1;
currentState = 1;
previous = 0;
}
}
// Current sensor?
if (steerConfig.CurrentSensor)
{
sensorSample = (float)analogRead(CURRENT_SENSOR_PIN);
sensorSample = (abs(775 - sensorSample)) * 0.5;
sensorReading = sensorReading * 0.7 + sensorSample * 0.3;
sensorReading = min(sensorReading, 255);
if (sensorReading >= steerConfig.PulseCountMax)
{
steerSwitch = 1;
currentState = 1;
previous = 0;
}
}
remoteSwitch = digitalRead(REMOTE_PIN);
switchByte = 0;
switchByte |= (remoteSwitch << 2); //put remote in bit 2
switchByte |= (steerSwitch << 1); //put steerswitch status in bit 1 position
switchByte |= workSwitch;
//get steering position
if (steerConfig.SingleInputWAS) //Single Input ADS
{
adc.setMux(ADS1115_REG_CONFIG_MUX_SINGLE_0);
steeringPosition = adc.getConversion();
adc.triggerConversion();//ADS1115 Single Mode
steeringPosition = (steeringPosition >> 1); //bit shift by 2 0 to 13610 is 0 to 5v
helloSteerPosition = steeringPosition - 6800;
}
else //ADS1115 Differential Mode
{
adc.setMux(ADS1115_REG_CONFIG_MUX_DIFF_0_1);
steeringPosition = adc.getConversion();
adc.triggerConversion();
steeringPosition = (steeringPosition >> 1); //bit shift by 2 0 to 13610 is 0 to 5v
helloSteerPosition = steeringPosition - 6800;
}
//DETERMINE ACTUAL STEERING POSITION
//convert position to steer angle. 32 counts per degree of steer pot position in my case
// ***** make sure that negative steer angle makes a left turn and positive value is a right turn *****
if (steerConfig.InvertWAS)
{
steeringPosition = (steeringPosition - 6805 - steerSettings.wasOffset); // 1/2 of full scale
steerAngleActual = (float)(steeringPosition) / -steerSettings.steerSensorCounts;
}
else
{
steeringPosition = (steeringPosition - 6805 + steerSettings.wasOffset); // 1/2 of full scale
steerAngleActual = (float)(steeringPosition) / steerSettings.steerSensorCounts;
}
//Ackerman fix
if (steerAngleActual < 0) steerAngleActual = (steerAngleActual * steerSettings.AckermanFix);
//WAS fault or over 25km, cut steering
if ((steerAngleActual < inputWAS[0]) || (steerAngleActual > inputWAS[20]) || gpsSpeed > 25)
{
steerSwitch = 1; // reset values like it turned off
currentState = 1;
previous = 0;
watchdogTimer = WATCHDOG_FORCE_VALUE;
}
//Map WAS
float mappedWAS = multiMap<float>(steerAngleActual, inputWAS, outputWAS, 21);
steerAngleActual = mappedWAS;
if (watchdogTimer < WATCHDOG_THRESHOLD)
{
//Enable H Bridge for IBT2, hyd aux, etc for cytron
if (steerConfig.CytronDriver)
{
if (steerConfig.IsRelayActiveHigh)
{
digitalWrite(PWM2_RPWM, 0);
digitalWrite(PWM2_OPT, 0);
}
else
{
digitalWrite(PWM2_RPWM, 1);
digitalWrite(PWM2_OPT, 1);
}
}
else digitalWrite(DIR1_RL_ENABLE, 1);
steerAngleError = steerAngleActual - steerAngleSetPoint; //calculate the steering error
//if (abs(steerAngleError)< steerSettings.lowPWM) steerAngleError = 0;
//Don't turn wheels if speed less than 0.3km/hr
if (gpsSpeed < 0.2) steerAngleError = 0;
calcSteeringPID(); //do the pid
motorDrive(); //out to motors the pwm value
// Autosteer Led goes GREEN if autosteering
digitalWrite(AUTOSTEER_ACTIVE_LED, 1);
digitalWrite(AUTOSTEER_STANDBY_LED, 0);
}
else
{
//we've lost the comm to AgOpenGPS, or just stop request
//Disable H Bridge for IBT2, hyd aux, etc for cytron
if (steerConfig.CytronDriver)
{
if (steerConfig.IsRelayActiveHigh)
{
digitalWrite(PWM2_RPWM, 1);
digitalWrite(PWM2_OPT, 1);
}
else
{
digitalWrite(PWM2_RPWM, 0);
digitalWrite(PWM2_OPT, 0);
}
}
else digitalWrite(DIR1_RL_ENABLE, 0); //IBT2
pwmDrive = 0; //turn off steering motor
motorDrive(); //out to motors the pwm value
pulseCount = 0;
// Autosteer Led goes back to RED when autosteering is stopped
digitalWrite (AUTOSTEER_STANDBY_LED, 1);
digitalWrite (AUTOSTEER_ACTIVE_LED, 0);
}
} //end of timed loop
//This runs continuously, outside of the timed loop, keeps checking for new udpData, turn sense
//delay(1);
// Speed pulse
if (gpsSpeedUpdateTimer < 1000)
{
if (speedPulseUpdateTimer > 200) // 100 (10hz) seems to cause tone lock ups occasionally
{
speedPulseUpdateTimer = 0;
//130 pp meter, 3.6 kmh = 1 m/sec = 130hz or gpsSpeed * 130/3.6 or gpsSpeed * 36.1111
//gpsSpeed = ((float)(autoSteerUdpData[5] | autoSteerUdpData[6] << 8)) * 0.1;
float speedPulse = gpsSpeed * 36.1111;
//Serial.print(gpsSpeed); Serial.print(" -> "); Serial.println(speedPulse);
if (gpsSpeed > 0.11) { // 0.10 wasn't high enough
tone(velocityPWM_Pin, uint16_t(speedPulse));
}
else {
noTone(velocityPWM_Pin);
}
}
}
else // if gpsSpeedUpdateTimer hasn't update for 1000 ms, turn off speed pulse
{
noTone(velocityPWM_Pin);
}
if (encEnable)
{
thisEnc = digitalRead(REMOTE_PIN);
if (thisEnc != lastEnc)
{
lastEnc = thisEnc;
if ( lastEnc) EncoderFunc();
}
}
VBus_Receive();
} // end of main loop
// UDP Receive
void ReceiveUdp()
{
uint16_t len = Eth_udpAutoSteer.parsePacket();
// if (len > 0)
// {
// Serial.print("ReceiveUdp: ");
// Serial.println(len);
// }
// Check for len > 4, because we check byte 0, 1, 3 and 3
if (len > 4)
{
Eth_udpAutoSteer.read(autoSteerUdpData, UDP_TX_PACKET_MAX_SIZE);
if (autoSteerUdpData[0] == 0x80 && autoSteerUdpData[1] == 0x81 && autoSteerUdpData[2] == 0x7F) //Data
{
if (autoSteerUdpData[3] == 0xFE && Autosteer_running) //254
{
gpsSpeed = ((float)(autoSteerUdpData[5] | autoSteerUdpData[6] << 8)) * 0.1;
gpsSpeedUpdateTimer = 0;
prevGuidanceStatus = guidanceStatus;
guidanceStatus = autoSteerUdpData[7];
guidanceStatusChanged = (guidanceStatus != prevGuidanceStatus);
//Bit 8,9 set point steer angle * 100 is sent
steerAngleSetPoint = ((float)(autoSteerUdpData[8] | ((int8_t)autoSteerUdpData[9]) << 8)) * 0.01; //high low bytes
//Serial.print("steerAngleSetPoint: ");
//Serial.println(steerAngleSetPoint);
//Serial.println(gpsSpeed);
if ((bitRead(guidanceStatus, 0) == 0) /* || (gpsSpeed < 0.1)*/ || (steerSwitch == 1))
{
watchdogTimer = WATCHDOG_FORCE_VALUE; //turn off steering motor
}
else //valid conditions to turn on autosteer
{
watchdogTimer = 0; //reset watchdog
}
//Bit 10 Tram
tram = autoSteerUdpData[10];
//Bit 11
relay = autoSteerUdpData[11];
//Bit 12
relayHi = autoSteerUdpData[12];
//----------------------------------------------------------------------------
//Serial Send to agopenGPS
int16_t sa = (int16_t)(steerAngleActual * 100);
PGN_253[5] = (uint8_t)sa;
PGN_253[6] = sa >> 8;
// heading
PGN_253[7] = (uint8_t)9999;
PGN_253[8] = 9999 >> 8;
// roll
PGN_253[9] = (uint8_t)8888;
PGN_253[10] = 8888 >> 8;
PGN_253[11] = switchByte;
PGN_253[12] = (uint8_t)pwmDisplay;
//checksum
int16_t CK_A = 0;
for (uint8_t i = 2; i < PGN_253_Size; i++)
CK_A = (CK_A + PGN_253[i]);
PGN_253[PGN_253_Size] = CK_A;
//off to AOG
SendUdp(PGN_253, sizeof(PGN_253), Eth_ipDestination, portDestination);
//Steer Data 2 -------------------------------------------------
if (steerConfig.PressureSensor || steerConfig.CurrentSensor)
{
if (aog2Count++ > 2)
{
//Send fromAutosteer2
PGN_250[5] = (byte)sensorReading;
//add the checksum for AOG2
CK_A = 0;
for (uint8_t i = 2; i < PGN_250_Size; i++)
{
CK_A = (CK_A + PGN_250[i]);
}
PGN_250[PGN_250_Size] = CK_A;
//off to AOG
SendUdp(PGN_250, sizeof(PGN_250), Eth_ipDestination, portDestination);
aog2Count = 0;
}
}
//Serial.println(steerAngleActual);
//--------------------------------------------------------------------------
}
//steer settings
else if (autoSteerUdpData[3] == 0xFC && Autosteer_running) //252
{
//PID values
steerSettings.Kp = ((float)autoSteerUdpData[5]); // read Kp from AgOpenGPS
steerSettings.highPWM = autoSteerUdpData[6]; // read high pwm
steerSettings.lowPWM = (float)autoSteerUdpData[7]; // read lowPWM from AgOpenGPS
steerSettings.minPWM = autoSteerUdpData[8]; //read the minimum amount of PWM for instant on
float temp = (float)steerSettings.minPWM * 1.2;
steerSettings.lowPWM = (byte)temp;
steerSettings.steerSensorCounts = autoSteerUdpData[9]; //sent as setting displayed in AOG
steerSettings.wasOffset = (autoSteerUdpData[10]); //read was zero offset Lo
steerSettings.wasOffset |= (autoSteerUdpData[11] << 8); //read was zero offset Hi
steerSettings.AckermanFix = (float)autoSteerUdpData[12] * 0.01;
//crc
//autoSteerUdpData[13];
//store in EEPROM
EEPROM.put(10, steerSettings);
// Re-Init steer settings
steerSettingsInit();
}
else if (autoSteerUdpData[3] == 0xFB) //251 FB - SteerConfig
{
uint8_t sett = autoSteerUdpData[5]; //setting0
if (bitRead(sett, 0)) steerConfig.InvertWAS = 1; else steerConfig.InvertWAS = 0;
if (bitRead(sett, 1)) steerConfig.IsRelayActiveHigh = 1; else steerConfig.IsRelayActiveHigh = 0;
if (bitRead(sett, 2)) steerConfig.MotorDriveDirection = 1; else steerConfig.MotorDriveDirection = 0;
if (bitRead(sett, 3)) steerConfig.SingleInputWAS = 1; else steerConfig.SingleInputWAS = 0;
if (bitRead(sett, 4)) steerConfig.CytronDriver = 1; else steerConfig.CytronDriver = 0;
if (bitRead(sett, 5)) steerConfig.SteerSwitch = 1; else steerConfig.SteerSwitch = 0;
if (bitRead(sett, 6)) steerConfig.SteerButton = 1; else steerConfig.SteerButton = 0;
if (bitRead(sett, 7)) steerConfig.ShaftEncoder = 1; else steerConfig.ShaftEncoder = 0;
steerConfig.PulseCountMax = autoSteerUdpData[6];
//was speed
//autoSteerUdpData[7];
sett = autoSteerUdpData[8]; //setting1 - Danfoss valve etc
if (bitRead(sett, 0)) steerConfig.IsDanfoss = 1; else steerConfig.IsDanfoss = 0;
if (bitRead(sett, 1)) steerConfig.PressureSensor = 1; else steerConfig.PressureSensor = 0;
if (bitRead(sett, 2)) steerConfig.CurrentSensor = 1; else steerConfig.CurrentSensor = 0;
if (bitRead(sett, 3)) steerConfig.IsUseY_Axis = 1; else steerConfig.IsUseY_Axis = 0;
//crc
//autoSteerUdpData[13];
EEPROM.put(40, steerConfig);
// Re-Init
steerConfigInit();
}//end FB
else if (autoSteerUdpData[3] == 200) // Hello from AgIO
{
if(Autosteer_running)
{
int16_t sa = (int16_t)(steerAngleActual * 100);
helloFromAutoSteer[5] = (uint8_t)sa;
helloFromAutoSteer[6] = sa >> 8;
helloFromAutoSteer[7] = (uint8_t)helloSteerPosition;
helloFromAutoSteer[8] = helloSteerPosition >> 8;
helloFromAutoSteer[9] = switchByte;
SendUdp(helloFromAutoSteer, sizeof(helloFromAutoSteer), Eth_ipDestination, portDestination);
}
if(useBNO08xRVC)
{
SendUdp(helloFromIMU, sizeof(helloFromIMU), Eth_ipDestination, portDestination);
}
}
else if (autoSteerUdpData[3] == 201)
{
//make really sure this is the subnet pgn
if (autoSteerUdpData[4] == 5 && autoSteerUdpData[5] == 201 && autoSteerUdpData[6] == 201)
{
networkAddress.ipOne = autoSteerUdpData[7];
networkAddress.ipTwo = autoSteerUdpData[8];
networkAddress.ipThree = autoSteerUdpData[9];
//save in EEPROM and restart
EEPROM.put(60, networkAddress);
SCB_AIRCR = 0x05FA0004; //Teensy Reset
}
}//end 201
//whoami
else if (autoSteerUdpData[3] == 202)
{
//make really sure this is the reply pgn
if (autoSteerUdpData[4] == 3 && autoSteerUdpData[5] == 202 && autoSteerUdpData[6] == 202)
{
IPAddress rem_ip = Eth_udpAutoSteer.remoteIP();
//hello from AgIO
uint8_t scanReply[] = { 128, 129, Eth_myip[3], 203, 7,
Eth_myip[0], Eth_myip[1], Eth_myip[2], Eth_myip[3],
rem_ip[0],rem_ip[1],rem_ip[2], 23 };
//checksum
int16_t CK_A = 0;
for (uint8_t i = 2; i < sizeof(scanReply) - 1; i++)
{
CK_A = (CK_A + scanReply[i]);
}
scanReply[sizeof(scanReply)-1] = CK_A;
static uint8_t ipDest[] = { 255,255,255,255 };
uint16_t portDest = 9999; //AOG port that listens
//off to AOG
SendUdp(scanReply, sizeof(scanReply), ipDest, portDest);
}
}
} //end if 80 81 7F
}
}
#ifdef ARDUINO_TEENSY41
void SendUdp(uint8_t *data, uint8_t datalen, IPAddress dip, uint16_t dport)
{
Eth_udpAutoSteer.beginPacket(dip, dport);
Eth_udpAutoSteer.write(data, datalen);
Eth_udpAutoSteer.endPacket();
}
#endif
//ISR Steering Wheel Encoder
void EncoderFunc()
{
if (encEnable)
{
pulseCount++;
encEnable = false;
}
}
//Rob Tillaart, https://github.com/RobTillaart/MultiMap
template<typename T>
T multiMap(T value, T* _in, T* _out, uint8_t size)
{
// take care the value is within range
// value = constrain(value, _in[0], _in[size-1]);
if (value <= _in[0]) return _out[0];
if (value >= _in[size - 1]) return _out[size - 1];
// search right interval
uint8_t pos = 1; // _in[0] already tested
while (value > _in[pos]) pos++;
// this will handle all exact "points" in the _in array
if (value == _in[pos]) return _out[pos];
// interpolate in the right segment for the rest
return (value - _in[pos - 1]) * (_out[pos] - _out[pos - 1]) / (_in[pos] - _in[pos - 1]) + _out[pos - 1];
}