-
Notifications
You must be signed in to change notification settings - Fork 19
/
AFFBWheel.ino
1657 lines (1364 loc) · 44.1 KB
/
AFFBWheel.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
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
MIT License
Copyright (c) 2022 Sulako
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <SPI.h>
#include <EEPROM.h>
#include <digitalWriteFast.h> //https://github.com/NicksonYap/digitalWriteFast
#include <avdweb_AnalogReadFast.h> //https://github.com/avandalen/avdweb_AnalogReadFast
#include "config.h"
#include "wheel.h"
#include "motor.h"
#include "settings.h"
#define AFFB_VER "1.0.2"
//global variables
Wheel_ wheel;
Motor motor;
SettingsData settings;
int16_t force;
bool timing=false;
bool fvaOut=false;
#ifdef DPB
static const uint8_t dpb[]={DPB_PINS};
#endif
#ifdef BM
static const uint8_t bm_cols[]={BM_COL_PINS};
static const uint8_t bm_rows[]={BM_ROW_PINS};
#endif
#ifdef APB
bool apb_out=false;
#endif
#ifdef ASHIFTER
bool ashifter_out=false;
#endif
uint16_t timerInfo;
uint16_t loopCount;
int8_t axisInfo=-1;
bool centerButtonState=false;
uint32_t tempButtons;
uint8_t debounceCount=0;
//constants and definitions for analog axes
#if ((PEDALS_TYPE == PT_INTERNAL) || (PEDALS_TYPE == PT_HC164))
#define DEFAULT_AA_MIN 0
#define DEFAULT_AA_MAX 1023
#endif
#if PEDALS_TYPE == PT_ADS1015
#include "bb_i2c.h"
#define DEFAULT_AA_MIN -32767
#define DEFAULT_AA_MAX 32767
ADS1015_BBI2C ads1015;
int8_t ADS1015_axis=0;
static const int8_t ADS1015_channels[]={ADS1015_CH_ACC, ADS1015_CH_BRAKE, ADS1015_CH_CLUTCH};
#endif
#if ((PEDALS_TYPE==PT_MCP3204_4W) || (PEDALS_TYPE==PT_MCP3204_SPI))
#define DEFAULT_AA_MIN 0
#define DEFAULT_AA_MAX 4095
#endif
#if PEDALS_TYPE == PT_ADS7828
#include "bb_i2c.h"
#define DEFAULT_AA_MIN 0
#define DEFAULT_AA_MAX 4095
ADS7828_BBI2C ads7828;
#endif
//constants and definions for buttons
#if BUTTONS_TYPE == BT_MCP23017
#include "bb_i2c.h"
MCP23017_BBI2C mcp23017_1;
MCP23017_BBI2C mcp23017_2;
#endif
#if BUTTONS_TYPE == BT_PCF857x
#include "bb_i2c.h"
PCF857x_BBI2C pcf857x[4];
#endif
void load(bool defaults=false);
void autoFindCenter(int force=AFC_FORCE, int period=AFC_PERIOD, int16_t treshold=AFC_TRESHOLD);
//------------------------ steering wheel sensor ----------------------------
#if STEER_TYPE == ST_TLE5010
#include <TLE5010.h> //https://github.com/vsulako/TLE5010
#include "multiturn.h"
TLE5010_SPI sensor(TLE5010_PIN_CS);
MultiTurn MT;
#define SETUP_WHEEL_SENSOR setupTLE();
#define GET_WHEEL_POS (-MT.setValue(getWheelPos()))
#define CENTER_WHEEL MT.zero();
#define SET_WHEEL_POSITION(val) MT.setPosition(-val)
inline int16_t getWheelPos(){
SPI.begin();
int16_t v=sensor.readInteger()>>(16-STEER_BITDEPTH);
SPI.end();
return v;
}
void setupTLE()
{
//4MHz on pin 5 for TLE5010
TCCR3A=(0<<COM3A1) | (1<<COM3A0) | (0<<COM3B1) | (0<<COM3B0) | (0<<COM3C1) | (0<<COM3C0) | (0<<WGM31) | (0<<WGM30);
TCCR3B=(0<<ICNC3) | (0<<ICES3) | (0<<WGM33) | (1<<WGM32) | (0<<CS32) | (0<<CS31) | (1<<CS30);
OCR3A=1;
pinMode(5, OUTPUT);
sensor.begin();
SPI.end();
sensor.atan2FuncInt=atan2_fix;
delayMicroseconds(10000); //let sensor start
GET_WHEEL_POS;
}
#endif
#if STEER_TYPE == ST_ENCODER
#include <Encoder.h> //https://github.com/PaulStoffregen/Encoder
Encoder encoder(ENCODER_PIN1, ENCODER_PIN2);
#define SETUP_WHEEL_SENSOR
#define GET_WHEEL_POS (((int32_t)encoder.read() << STEER_BITDEPTH) / ENCODER_PPR)
#define CENTER_WHEEL encoder.write(0);
#define SET_WHEEL_POSITION(val) encoder.write((val * ENCODER_PPR) / (1<<STEER_BITDEPTH))
#endif
#if STEER_TYPE == ST_AS5600
#include "bb_i2c.h"
#include "multiturn.h"
AS5600_BBI2C AS5600;
MultiTurn MT;
#define SETUP_WHEEL_SENSOR setupAS5600();
#define GET_WHEEL_POS (MT.setValue((AS5600.readAngle()-2048)<<(STEER_BITDEPTH-12)))
#define CENTER_WHEEL MT.zero();
#define SET_WHEEL_POSITION(val) MT.setPosition(val)
void setupAS5600()
{
AS5600.begin();
}
#endif
#if STEER_TYPE == ST_MLX90316
#include "multiturn.h"
MultiTurn MT;
#define SETUP_WHEEL_SENSOR setupMLX();
#define GET_WHEEL_POS (MT.setValue(getWheelPos()))
#define CENTER_WHEEL MT.zero();
#define SET_WHEEL_POSITION(val) MT.setPosition(val)
int16_t getWheelPos(){
SPI.begin();
int16_t val;
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE1));
digitalWriteFast(MLX90316_PIN_CS, 0);
delayMicroseconds(6);
SPI.transfer(0xAA);
SPI.transfer(0xFF);
delayMicroseconds(25);
((uint8_t *)&val)[1]=SPI.transfer(0xFF);
delayMicroseconds(25);
((uint8_t *)&val)[0]=SPI.transfer(0xFF);
digitalWriteFast(MLX90316_PIN_CS, 1);
SPI.endTransaction();
SPI.end();
return val>>(16-STEER_BITDEPTH);
}
void setupMLX()
{
pinMode(MLX90316_PIN_CS, OUTPUT);
digitalWriteFast(MLX90316_PIN_CS, 1);
delay(16); //let sensor start
}
#endif
//-------------------------------------------------------------------------------------
void setup() {
Serial.begin(SERIAL_BAUDRATE);
Serial.setTimeout(50);
//Steering axis sensor setup
SETUP_WHEEL_SENSOR;
//set up analog axes
#ifdef AA_PULLUP
analogReference(INTERNAL); //2.56v reference to get more resolution.
#endif
#if (PEDALS_TYPE==PT_MP_HC164)
pinMode(MP_HC164_PIN_SCK, OUTPUT);
#endif
#if (PEDALS_TYPE==PT_MCP3204_4W)
pinModeFast(MCP3204_4W_PIN_SCK, OUTPUT);
#if (MCP3204_4W_PIN_MOSI!=MCP3204_4W_PIN_MISO)
pinModeFast(MCP3204_4W_PIN_MOSI, OUTPUT);
pinModeFast(MCP3204_4W_PIN_MISO, INPUT);
#endif
#endif
#if (PEDALS_TYPE==PT_MCP3204_SPI)
pinMode(MCP3204_PIN_CS, OUTPUT);
#endif
#if (PEDALS_TYPE == PT_ADS1015)
ads1015.begin();
#endif
#if PEDALS_TYPE == PT_ADS7828
ads7828.begin();
#endif
//setup buttons
#if (BUTTONS_TYPE == BT_74HC165)
pinMode(HC165_PIN_DATA1, INPUT_PULLUP);
pinMode(HC165_PIN_DATA2, INPUT_PULLUP);
pinMode(HC165_PIN_SCK, OUTPUT);
#ifdef HC165_PIN_PL
pinMode(HC165_PIN_PL, OUTPUT);
#endif
#endif
#if (BUTTONS_TYPE == BT_CD4021B)
pinMode(CD4021_PIN_DATA1, INPUT_PULLUP);
pinMode(CD4021_PIN_DATA2, INPUT_PULLUP);
pinMode(CD4021_PIN_SCK, OUTPUT);
#ifdef CD4021_PIN_PL
pinMode(CD4021_PIN_PL, OUTPUT);
#endif
#endif
#if (BUTTONS_TYPE == BT_MCP23017)
mcp23017_1.begin(MCP23017_ADDR1);
mcp23017_2.begin(MCP23017_ADDR2);
#endif
#if BUTTONS_TYPE == BT_PCF857x
#if PCF857x_L1_TYPE==PCF8574
pcf857x[0].begin(PCF857x_L1_ADDR1);
pcf857x[1].begin(PCF857x_L1_ADDR2);
#endif
#if PCF857x_L1_TYPE==PCF8575
pcf857x[0].begin(PCF857x_L1_ADDR1);
#endif
#if PCF857x_L2_TYPE==PCF8574
pcf857x[2].begin(PCF857x_L2_ADDR1);
pcf857x[3].begin(PCF857x_L2_ADDR2);
#endif
#if PCF857x_L2_TYPE==PCF8575
pcf857x[2].begin(PCF857x_L2_ADDR1);
#endif
#endif
//direct pin buttons
#ifdef DPB
for(uint8_t i=0;i<sizeof(dpb);i++)
pinMode(dpb[i], INPUT_PULLUP);
#endif
//button matrix
#ifdef BM
for(uint8_t i=0;i<sizeof(bm_cols);i++)
pinMode(bm_cols[i], INPUT_PULLUP);
#endif
//motor setup
motor.begin();
//load settings
load();
center();
#ifdef AFC_ON
autoFindCenter();
#endif
while(true)
mainLoop();
}
void mainLoop() {
uint16_t t[5];
//Gathering data and measuring time
if (timing)
{
t[0]=micros();
wheel.axisWheel->setValue(GET_WHEEL_POS);
t[1]=micros();
readAnalogAxes();
t[2]=micros();
readButtons();
t[3]=micros();
//Send data and receive FFB commands
wheel.update();
t[4]=micros();
processFFB();
t[5]=micros();
}
else
{
wheel.axisWheel->setValue(GET_WHEEL_POS);
readAnalogAxes();
readButtons();
processUsbCmd();
wheel.update();
processFFB();
}
loopCount++;
if ((uint16_t)(millis()-timerInfo)>1000)
{
if (timing)
{
Serial.print(F("S: "));
Serial.print(t[1]-t[0]);
Serial.print(F(" A: "));
Serial.print(t[2]-t[1]);
Serial.print(F(" B: "));
Serial.print(t[3]-t[2]);
Serial.print(F(" U: "));
Serial.print(t[4]-t[3]);
Serial.print(F(" F: "));
Serial.print(t[5]-t[4]);
Serial.print(F(" loop/sec:"));
Serial.print(loopCount);
Serial.println();
loopCount=0;
timerInfo=millis();
}
}
processSerial();
}
//Processing endstop and force feedback
void processFFB()
{
int32_t excess=0;
if (wheel.axisWheel->rawValue > wheel.axisWheel->axisMax)
excess=wheel.axisWheel->rawValue - wheel.axisWheel->axisMax;
if (wheel.axisWheel->rawValue < -wheel.axisWheel->axisMax)
excess=wheel.axisWheel->rawValue + wheel.axisWheel->axisMax;
if (excess)
{
int32_t absExcess=abs(excess);
if (absExcess<settings.endstopWidth)
{
force = settings.endstopOffset + (absExcess * (16383 - settings.endstopOffset) / settings.endstopWidth);
}
else
force = 16383;
if (excess<0)
force = -force;
if (settings.gain[GAIN_ENDSTOP]!=1024)
force=applyGain(force, settings.gain[GAIN_ENDSTOP]);
}
else
{
force=wheel.ffbEngine.calculateForce(wheel.axisWheel);
}
force=applyForceLimit(force);
motor.setForce(force);
}
//scaling force to minForce & maxForce and cut at cutForce
int16_t applyForceLimit(int16_t force)
{
if (force==0)
return 0;
if ((settings.minForce!=0) || (settings.maxForce<16383))
{
if (abs(force)<1024)//slope
{
int32_t v = (((settings.maxForce-settings.minForce)) >> 4) + settings.minForce;
force = (force * v) >> 10;
}
else
force = (int16_t)((int32_t)force * (settings.maxForce-settings.minForce) >> 14) + sign(force)*settings.minForce;
}
if (settings.cutForce>=16383)
return force;
else
return constrain(force, -settings.cutForce, settings.cutForce);
}
/*
communicating with GUI:
*/
void processUsbCmd()
{
USB_GUI_Command* usbCmd=&wheel.ffbEngine.ffbReportHandler->usbCommand;
//clear output report
memset(&wheel.USB_GUI_Report, 0, sizeof(wheel.USB_GUI_Report));
void* data=wheel.USB_GUI_Report.data;
if (usbCmd->command)
{
//return data only for read commands
if (usbCmd->command<10)
{
wheel.USB_GUI_Report.command=usbCmd->command;
wheel.USB_GUI_Report.arg=usbCmd->arg[0];
}
switch(usbCmd->command)
{
//get data
case 1: //return string "AFFBW "+version
strcpy_P(((GUI_Report_Version*)data)->id, PSTR("AFFBW"));
strcpy_P(((GUI_Report_Version*)data)->ver, PSTR(AFFB_VER));
break;
case 2: //return steering axis data
((GUI_Report_SteerAxis*)data)->rawValue=wheel.axisWheel->rawValue;
((GUI_Report_SteerAxis*)data)->value=wheel.axisWheel->value;
((GUI_Report_SteerAxis*)data)->range=wheel.axisWheel->range;
((GUI_Report_SteerAxis*)data)->velocity=wheel.axisWheel->velocity;
((GUI_Report_SteerAxis*)data)->acceleration=wheel.axisWheel->acceleration;
break;
case 3: //return analog axis data
((GUI_Report_AnalogAxis*)data)->rawValue=wheel.analogAxes[usbCmd->arg[0]]->rawValue;
((GUI_Report_AnalogAxis*)data)->value=wheel.analogAxes[usbCmd->arg[0]]->value;
((GUI_Report_AnalogAxis*)data)->axisMin=wheel.analogAxes[usbCmd->arg[0]]->axisMin;
((GUI_Report_AnalogAxis*)data)->axisMax=wheel.analogAxes[usbCmd->arg[0]]->axisMax;
((GUI_Report_AnalogAxis*)data)->center=wheel.analogAxes[usbCmd->arg[0]]->getCenter();
((GUI_Report_AnalogAxis*)data)->deadzone=wheel.analogAxes[usbCmd->arg[0]]->getDZ();
((GUI_Report_AnalogAxis*)data)->autoLimit=wheel.analogAxes[usbCmd->arg[0]]->autoLimit;
((GUI_Report_AnalogAxis*)data)->hasCenter=!wheel.analogAxes[usbCmd->arg[0]]->autoCenter;
((GUI_Report_AnalogAxis*)data)->outputDisabled=wheel.analogAxes[usbCmd->arg[0]]->outputDisabled;
((GUI_Report_AnalogAxis*)data)->bitTrim=wheel.analogAxes[usbCmd->arg[0]]->bitTrim;
break;
case 4: //return buttons data
((GUI_Report_Buttons*)data)->buttons=wheel.buttons;
((GUI_Report_Buttons*)data)->centerButton=settings.centerButton;
((GUI_Report_Buttons*)data)->debounce=settings.debounce;
break;
case 5: //return gains
memcpy(data, settings.gain, sizeof(settings.gain));
break;
case 6: //return remaining settings
//GUI_Report_Settings* repSettings=(GUI_Report_Settings*)(wheel.USB_GUI_Report.data);
((GUI_Report_Settings*)data)->maxvd=round(16384.0/wheel.ffbEngine.maxVelocityDamperC);
((GUI_Report_Settings*)data)->maxvf=round(16384.0/wheel.ffbEngine.maxVelocityFrictionC);
((GUI_Report_Settings*)data)->maxacc=round(16384.0/wheel.ffbEngine.maxAccelerationInertiaC);
((GUI_Report_Settings*)data)->minForce=settings.minForce;
((GUI_Report_Settings*)data)->maxForce=settings.maxForce;
((GUI_Report_Settings*)data)->cutForce=settings.cutForce;
((GUI_Report_Settings*)data)->ffbBD=motor.bitDepth;
((GUI_Report_Settings*)data)->endstopOffset=settings.endstopOffset;
((GUI_Report_Settings*)data)->endstopWidth=settings.endstopWidth;
break;
// set
case 10://set range for steering axis
wheel.axisWheel->setRange(usbCmd->arg[0]);
break;
case 11://set limits for analog axis
wheel.analogAxes[usbCmd->arg[0]]->setLimits(usbCmd->arg[1], usbCmd->arg[2]);
break;
case 12://set center for analog axis
wheel.analogAxes[usbCmd->arg[0]]->setCenter(usbCmd->arg[1]);
break;
case 13://set deadzone for analog axis
wheel.analogAxes[usbCmd->arg[0]]->setDZ(usbCmd->arg[1]);
break;
case 14://set autolimits for analog axis
wheel.analogAxes[usbCmd->arg[0]]->setAutoLimits(usbCmd->arg[1]>0);
break;
case 15://set center button
settings.centerButton=usbCmd->arg[0];
break;
case 16://set debounce value
settings.debounce=usbCmd->arg[0];
break;
case 17://set gain
settings.gain[usbCmd->arg[0]]=usbCmd->arg[1];
break;
case 18://set misc settings
switch (usbCmd->arg[0])
{
case 0:
wheel.ffbEngine.maxVelocityDamperC=16384.0/usbCmd->arg[1];
break;
case 1:
wheel.ffbEngine.maxVelocityFrictionC=16384.0/usbCmd->arg[1];
break;
case 2:
wheel.ffbEngine.maxAccelerationInertiaC=16384.0/usbCmd->arg[1];
break;
case 3:
settings.minForce=usbCmd->arg[1];
break;
case 4:
settings.maxForce=usbCmd->arg[1];
break;
case 5:
settings.cutForce=usbCmd->arg[1];
break;
case 6:
motor.setBitDepth(usbCmd->arg[1]);
break;
case 7:
settings.endstopOffset=usbCmd->arg[1];
settings.endstopWidth=usbCmd->arg[2];
break;
}
break;
case 19://set outputDisabled and bittrim for analog axis
wheel.analogAxes[usbCmd->arg[0]]->outputDisabled=(usbCmd->arg[1]>0);
wheel.analogAxes[usbCmd->arg[0]]->bitTrim=usbCmd->arg[2];
break;
//commands
case 20: //load settings from EEPROM
load();
break;
case 21: //save settings to EEPROM
save();
break;
case 22: //load defaults
load(true);
break;
case 23://center wheel
center();
break;
}
}
usbCmd->command=0;
}
//------------------------- Reading all analog axes ----------------------------------
void readAnalogAxes()
{
#if (PEDALS_TYPE==PT_INTERNAL)
#ifdef AA_PULLUP_LINEARIZE
wheel.analogAxes[AXIS_ACC]->setValue(pullup_linearize(analogReadFast(PIN_ACC)));
wheel.analogAxes[AXIS_BRAKE]->setValue(pullup_linearize(analogReadFast(PIN_BRAKE)));
wheel.analogAxes[AXIS_CLUTCH]->setValue(pullup_linearize(analogReadFast(PIN_CLUTCH)));
#else
wheel.analogAxes[AXIS_ACC]->setValue(analogReadFast(PIN_ACC));
wheel.analogAxes[AXIS_BRAKE]->setValue(analogReadFast(PIN_BRAKE));
wheel.analogAxes[AXIS_CLUTCH]->setValue(analogReadFast(PIN_CLUTCH));
#endif
#endif
#if (PEDALS_TYPE==PT_HC164)
digitalWriteFast(MP_HC164_PIN_SCK, 1);
wheel.analogAxes[AXIS_ACC]->setValue(analogReadFast(MP_HC164_PIN_ADATA));
digitalWriteFast(MP_HC164_PIN_SCK, 0);
digitalWriteFast(MP_HC164_PIN_SCK, 1);
wheel.analogAxes[AXIS_BRAKE]->setValue(analogReadFast(MP_HC164_PIN_ADATA));
digitalWriteFast(MP_HC164_PIN_SCK, 0);
digitalWriteFast(MP_HC164_PIN_SCK, 1);
wheel.analogAxes[AXIS_CLUTCH]->setValue(analogReadFast(MP_HC164_PIN_ADATA));
digitalWriteFast(MP_HC164_PIN_SCK, 0);
#endif
#if (PEDALS_TYPE==PT_MCP3204_4W)
wheel.analogAxes[AXIS_ACC]->setValue(MCP3204_BB_read(MCP3204_CH_ACC));
wheel.analogAxes[AXIS_BRAKE]->setValue(MCP3204_BB_read(MCP3204_CH_BRAKE));
wheel.analogAxes[AXIS_CLUTCH]->setValue(MCP3204_BB_read(MCP3204_CH_CLUTCH));
#endif
#if (PEDALS_TYPE==PT_MCP3204_SPI)
SPI.begin();
wheel.analogAxes[AXIS_ACC]->setValue(MCP3204_SPI_read(MCP3204_CH_ACC));
wheel.analogAxes[AXIS_BRAKE]->setValue(MCP3204_SPI_read(MCP3204_CH_BRAKE));
wheel.analogAxes[AXIS_CLUTCH]->setValue(MCP3204_SPI_read(MCP3204_CH_CLUTCH));
SPI.end();
#endif
#if (PEDALS_TYPE == PT_ADS1015)
ADS1015_read();
#endif
#if PEDALS_TYPE == PT_ADS7828
wheel.analogAxes[AXIS_ACC]->setValue(ads7828.readADC(0));
wheel.analogAxes[AXIS_BRAKE]->setValue(ads7828.readADC(1));
wheel.analogAxes[AXIS_CLUTCH]->setValue(ads7828.readADC(2));
#endif
//additional axes
#ifdef AA_PULLUP_LINEARIZE
#ifdef PIN_AUX1
wheel.analogAxes[AXIS_AUX1]->setValue(pullup_linearize(analogReadFast(PIN_AUX1)));
#endif
#ifdef PIN_AUX2
wheel.analogAxes[AXIS_AUX2]->setValue(pullup_linearize(analogReadFast(PIN_AUX2)));
#endif
#ifdef PIN_AUX3
wheel.analogAxes[AXIS_AUX3]->setValue(pullup_linearize(analogReadFast(PIN_AUX3)));
#endif
#ifdef PIN_AUX4
wheel.analogAxes[AXIS_AUX4]->setValue(pullup_linearize(analogReadFast(PIN_AUX4)));
#endif
#else
#ifdef PIN_AUX1
wheel.analogAxes[AXIS_AUX1]->setValue(analogReadFast(PIN_AUX1));
#endif
#ifdef PIN_AUX2
wheel.analogAxes[AXIS_AUX2]->setValue(analogReadFast(PIN_AUX2));
#endif
#ifdef PIN_AUX3
wheel.analogAxes[AXIS_AUX3]->setValue(analogReadFast(PIN_AUX3));
#endif
#ifdef PIN_AUX4
wheel.analogAxes[AXIS_AUX4]->setValue(analogReadFast(PIN_AUX4));
#endif
#endif
if (fvaOut)
{
wheel.analogAxes[AXIS_AUX3]->value=(wheel.axisWheel->velocity << 1) * wheel.ffbEngine.maxVelocityDamperC;
wheel.analogAxes[AXIS_AUX4]->value=(wheel.axisWheel->acceleration << 1)* wheel.ffbEngine.maxAccelerationInertiaC;
wheel.analogAxes[AXIS_AUX2]->value=force<<1;
}
}
#ifdef AA_PULLUP_LINEARIZE
//Linearizing axis values, when using internal adc + pullup.
int16_t pullup_linearize(int16_t val)
{
//Assuming VCC=5v, ADCreference=2.56v, 0 < val < 1024
//val = val * 1024 * (R_pullup / R_pot) / (1024 * 5 / 2.56 - val)
//if Rpullup = R_pot...
//val = val * 1024 / (2000 - val)
//division is slow
//piecewise linear approximation, 16 steps
static const int16_t a[] ={0, 33, 70, 108, 150, 195, 243, 295, 352, 414, 481, 556, 638, 729, 831, 945};
static const uint8_t b[] ={33, 37, 38, 42, 45, 48, 52, 57, 62, 67, 75, 82, 91, 102, 114, 129};
uint8_t i=(val>>6);
return a[i] + (((val % 64) * b[i]) >> 6);
}
#endif
int16_t MCP3204_SPI_read(uint8_t channel)
{
int16_t val;
digitalWriteFast(MCP3204_PIN_CS, 0);
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
SPI.transfer(0b00000110);
val=SPI.transfer16(channel<<14) & 0x0FFF;
SPI.endTransaction();
digitalWriteFast(MCP3204_PIN_CS, 1);
return val;
}
int16_t MCP3204_BB_read(uint8_t channel)
{
#if (MCP3204_4W_PIN_MOSI==MCP3204_4W_PIN_MISO)
pinModeFast(MCP3204_4W_PIN_MOSI, OUTPUT);
#endif
digitalWriteFast(MCP3204_4W_PIN_MOSI, 1);
//start bit
digitalWriteFast(MCP3204_4W_PIN_SCK, 1);
digitalWriteFast(MCP3204_4W_PIN_SCK, 0);
//single
digitalWriteFast(MCP3204_4W_PIN_SCK, 1);
digitalWriteFast(MCP3204_4W_PIN_SCK, 0);
//d2
digitalWriteFast(MCP3204_4W_PIN_SCK, 1);
digitalWriteFast(MCP3204_4W_PIN_SCK, 0);
//d1 and d0
if (channel & 0b00000010)
{
digitalWriteFast(MCP3204_4W_PIN_MOSI, 1);
}
else
{
digitalWriteFast(MCP3204_4W_PIN_MOSI, 0);
}
digitalWriteFast(MCP3204_4W_PIN_SCK, 1);
digitalWriteFast(MCP3204_4W_PIN_SCK, 0);
if (channel & 0b00000001)
{
digitalWriteFast(MCP3204_4W_PIN_MOSI, 1);
}
else
{
digitalWriteFast(MCP3204_4W_PIN_MOSI, 0);
}
digitalWriteFast(MCP3204_4W_PIN_SCK, 1);
digitalWriteFast(MCP3204_4W_PIN_SCK, 0);
//skip 2 clock
digitalWriteFast(MCP3204_4W_PIN_SCK, 1);
digitalWriteFast(MCP3204_4W_PIN_SCK, 0);
digitalWriteFast(MCP3204_4W_PIN_SCK, 1);
digitalWriteFast(MCP3204_4W_PIN_SCK, 0);
#if (MCP3204_4W_PIN_MOSI==MCP3204_4W_PIN_MISO)
pinModeFast(MCP3204_4W_PIN_MISO, INPUT);
#endif
//read 12bit answer
int16_t data=0;
int16_t b=0b0000100000000000;
do{
if (digitalReadFast(MCP3204_4W_PIN_MISO))
data |= b;
digitalWriteFast(MCP3204_4W_PIN_SCK, 1);
digitalWriteFast(MCP3204_4W_PIN_SCK, 0);
}while(b>>=1);
return data;
}
#if (PEDALS_TYPE == PT_ADS1015)
void ADS1015_read()
{
wheel.analogAxes[ADS1015_axis]->setValue(ads1015.read16());
ADS1015_axis++;
if (ADS1015_axis==3)
ADS1015_axis=0;
ads1015.requestADC(ADS1015_channels[ADS1015_axis]);
}
#endif
//-----------------------------------end analog axes------------------------------
//-----------------------------------reading buttons------------------------------
void readButtons()
{
uint32_t buttons=0;
bool changed;
uint8_t i;
uint8_t* d;
if (settings.debounce)
{
d=(uint8_t *)&buttons;
changed=false;
}
else
{
wheel.buttons=0;
d=(uint8_t *)&wheel.buttons;
changed=true;
}
#if BUTTONS_TYPE == BT_74HC165
#ifdef HC165_PIN_PL
digitalWriteFast(HC165_PIN_PL,1);
#else
digitalWriteFast(HC165_PIN_SCK, 1);
digitalWriteFast(HC165_PIN_SCK, 0);
#endif
i=0x80;
do
{
if (!digitalReadFast(HC165_PIN_DATA1))
d[0]|=i;
if (!digitalReadFast(HC165_PIN_DATA2))
d[2]|=i;
digitalWriteFast(HC165_PIN_SCK, 1);
digitalWriteFast(HC165_PIN_SCK, 0);
} while(i>>=1);
i=0x80;
do
{
if (!digitalReadFast(HC165_PIN_DATA1))
d[1]|=i;
if (!digitalReadFast(HC165_PIN_DATA2))
d[3]|=i;
digitalWriteFast(HC165_PIN_SCK, 1);
digitalWriteFast(HC165_PIN_SCK, 0);
} while(i>>=1);
#ifdef HC165_PIN_PL
digitalWriteFast(HC165_PIN_PL,0);
#endif
#endif
#if BUTTONS_TYPE == BT_CD4021B
digitalWriteFast(CD4021_PIN_SCK, 1);
digitalWriteFast(CD4021_PIN_SCK, 0);
//защелка работает наоборот
#ifdef CD4021_PIN_PL
digitalWriteFast(CD4021_PIN_PL,0);
#endif
i=0x80;
do
{
if (!digitalReadFast(CD4021_PIN_DATA1))
d[0]|=i;
if (!digitalReadFast(CD4021_PIN_DATA2))
d[2]|=i;
digitalWriteFast(CD4021_PIN_SCK, 1);
digitalWriteFast(CD4021_PIN_SCK, 0);
} while(i>>=1);
i=0x80;
do
{
if (!digitalReadFast(CD4021_PIN_DATA1))
d[1]|=i;
if (!digitalReadFast(CD4021_PIN_DATA2))
d[3]|=i;
digitalWriteFast(CD4021_PIN_SCK, 1);
digitalWriteFast(CD4021_PIN_SCK, 0);
} while(i>>=1);
#ifdef CD4021_PIN_PL
digitalWriteFast(CD4021_PIN_PL,1);
#endif
#endif
#if BUTTONS_TYPE == BT_MCP23017
mcp23017_1.read16((uint16_t *)d);
mcp23017_2.read16((uint16_t *)(d+2));
*((uint32_t *)d)=~*((uint32_t *)d);
#endif
#if BUTTONS_TYPE == BT_PCF857x
//read 1-16
#if PCF857x_L1_TYPE==PCF8574
pcf857x[0].read(d);
pcf857x[1].read(d+1);
#endif
#if PCF857x_L1_TYPE==PCF8575
pcf857x[0].read16((uint16_t *)d);
#endif
//read 17-32
#if PCF857x_L2_TYPE==PCF8574
pcf857x[2].read(d+2);
pcf857x[3].read(d+3);
#endif
#if PCF857x_L2_TYPE==PCF8575
pcf857x[2].read16((uint16_t *)d+1);
#endif
*((uint32_t *)d)=~*((uint32_t *)d);
#endif
//analog pin to buttons
#ifdef APB
static const uint8_t apb_values[] ={APB_VALUES};
static const uint8_t apb_btns[] ={APB_BTNS};
uint8_t apb_val=analogReadFast(APB_PIN)>>2;
if (apb_out)
{
Serial.print(F("APB: "));
Serial.println(apb_val);
}
for(i=0;i<APB_BTN_COUNT;i++)
bitWrite(*((uint32_t *)d), apb_btns[i]-1, ((apb_val>apb_values[i]-APB_TOLERANCE) && (apb_val<apb_values[i]+APB_TOLERANCE)));
#endif
//direct pin buttons
#ifdef DPB
for(i=0;i<sizeof(dpb);i++)
bitWrite(*((uint32_t *)d), DPB_1ST_BTN-1+i, (*portInputRegister(digitalPinToPort(dpb[i])) & digitalPinToBitMask(dpb[i]))==0 );
#endif
//button matrix
#ifdef BM
uint8_t btn=BM_1ST_BTN-1;
for(i=0;i<sizeof(bm_rows);i++)
{
//pinMode(bm_rows[i], OUTPUT);
//digitalWrite(bm_rows[i], 0);
*portModeRegister(digitalPinToPort(bm_rows[i]))|=digitalPinToBitMask(bm_rows[i]);
for(uint8_t j=0;j<sizeof(bm_cols);j++)
{
bitWrite(*((uint32_t *)d), btn, (*portInputRegister(digitalPinToPort(bm_cols[j])) & digitalPinToBitMask(bm_cols[j]))==0);
btn++;
}
//pinMode(bm_rows[i], INPUT);
*portModeRegister(digitalPinToPort(bm_rows[i]))&=~digitalPinToBitMask(bm_rows[i]);
}
#endif