9 #include "USBSabertooth_NB.h"
13 #define SABER_SERIAL Serial2
17 #define MOTOR_FULL_POWER 2047
18 #define TILT_MOTOR_FULL_POWER 2047
19 #define MOTOR_HALF_3QUARTERS 1400
20 #define MOTOR_HALF_POWER 1024
21 #define MOTOR_QUARTER_POWER 300
23 #define YOKE_ANGLE_2LEGS_TARGET -13
24 #define YOKE_ANGLE_2LEGS_LIMIT -10
26 #define YOKE_ANGLE_3LEGS_TARGET 23
27 #define YOKE_ANGLE_2TO3LEGS_SLOWDOWN 20
29 #define YOKE_ANGLE_CENTER_LEG_START -10
41 Stance( HardwareSerial& saberSerialPort,
47 byte saberAddress = 128) :
48 fSaberSerialInit(saberSerialPort),
49 fSaberSerial(saberSerialPort),
50 fST(fSaberSerial, saberAddress),
51 fTiltUpPin(tiltUpPin),
52 fTiltDownPin(tiltDownPin),
54 fLegDownPin(legDownPin),
57 for (
unsigned i = 0; i <
SizeOfArray(fLegPotReading); i++)
58 fLegPotReading[i] = 0;
66 byte saberAddress = 128) :
67 Stance(SABER_SERIAL, tiltUpPin, tiltDownPin, legUpPin, legDownPin, legPotPin, saberAddress)
79 if (strcmp(cmd,
"TWOLEGS") == 0)
83 else if (strcmp(cmd,
"THREELEGS") == 0)
94 return (fLegHappy == 0 && fTiltHappy == 0 && fStance == 1);
102 return (fLegHappy == 0 && fTiltHappy == 0 && fStance == 2);
134 pinMode(fTiltUpPin, INPUT_PULLUP);
135 pinMode(fTiltDownPin, INPUT_PULLUP);
136 pinMode(fLegUpPin, INPUT_PULLUP);
137 pinMode(fLegDownPin, INPUT_PULLUP);
138 pinMode(fLegPotPin, INPUT);
154 uint32_t currentMillis = millis();
188 fLegDn = digitalRead(fLegDownPin);
189 fLegUp = digitalRead(fLegUpPin);
190 fTiltDn = digitalRead(fTiltDownPin);
191 fTiltUp = digitalRead(fTiltUpPin);
195 fLegPotTotal = fLegPotTotal - fLegPotReading[fLegPotIndex];
197 fLegPotReading[fLegPotIndex] = analogRead(fLegPotPin);
199 fLegPotTotal = fLegPotTotal + fLegPotReading[fLegPotIndex++];
204 fLegPos = map(fLegPotTotal /
SizeOfArray(fLegPotReading), 0, 1023, 0, 50);
208 fShowTimeThreeToTwo = 0;
213 fShowTimeTwoToThree = 0;
215 if (currentMillis - fLastDisplayMillis >= kDisplayInterval || lastMotor1 != 0 || lastMotor2 != 0 || fLegPos != fLegLastPos)
217 fLastDisplayMillis = currentMillis;
220 if (currentMillis - fLastStanceMillis >= kStanceInterval)
222 fLastStanceMillis = currentMillis;
227 if (currentMillis - fLastShowTimeMillis >= kShowTimeInterval)
229 fLastShowTimeMillis = currentMillis;
230 fShowTimeThreeToTwo++;
231 fShowTimeTwoToThree++;
233 fLegLastPos = fLegPos;
237 class USBSabertoothSerialInit
240 USBSabertoothSerialInit(HardwareSerial& port)
246 USBSabertoothSerialInit fSaberSerialInit;
247 USBSabertoothSerial fSaberSerial;
261 int fStanceTarget = 0;
264 unsigned fLegPotIndex = 0;
265 int fLegPotReading[10];
266 int fLegPotTotal = 0;
268 int fLegLastPos = -1;
271 int fCurrentChannel1 = 0;
272 int fCurrentChannel2 = 0;
277 uint32_t fLastDisplayMillis = 0;
278 uint32_t fLastStanceMillis = 0;
279 uint32_t fLastShowTimeMillis = 0;
280 uint32_t fLastStatusMillis = 0;
281 uint32_t fShowTimeThreeToTwo = 0;
282 uint32_t fShowTimeTwoToThree = 0;
284 static const int kDisplayInterval = 1000;
285 static const int kStanceInterval = 100;
286 static const int kShowTimeInterval = 100;
287 static const int kMaxStatusInterval = 5000;
302 void legMotorUpSlow()
322 void legMotorDownSlow()
363 void tiltMotorDownSlow()
386 if (lastMotor1 != 0 || lastMotor2 != 0)
405 else if (fLegDn == 1)
417 else if (fLegUp == 1)
429 else if (fTiltDn == 1)
441 else if (fTiltUp == 1)
454 float yokeAnkle = getYokeAngle();
455 if (fLegHappy == 0 && fTiltHappy == 0)
457 if (fLegUp == 0 && fLegDn == 1 && fTiltUp == 0 && fTiltDn == 1)
462 if (fLegUp == 1 && fLegDn == 0 && fTiltUp == 1 && fTiltDn == 0)
467 if (fLegUp == 0 && fLegDn == 1 && fTiltUp == 1 && fTiltDn == 1)
471 if (fLegUp == 1 && fLegDn == 1 && fTiltUp == 0 && fTiltDn == 1)
475 if (fLegUp == 1 && fLegDn == 0 && fTiltUp == 0 && fTiltDn == 1)
479 if (fLegUp == 1 && fLegDn == 0 && fTiltUp == 1 && fTiltDn == 1)
483 if (fLegUp == 1 && fLegDn == 1 && fTiltUp == 1 && fTiltDn == 0)
487 if (fLegUp == 1 && fLegDn == 1 && fTiltUp == 1 && fTiltDn == 1)
497 if (fStanceTarget == 0)
503 if (fStanceTarget == fStance)
515 if (fStanceTarget == 1 && fStance == 2)
523 if (fStanceTarget == 1 && fStance == 3)
529 if (fStanceTarget == 1 && fStance == 4)
535 if (fStanceTarget == 1 && fStance == 5)
541 if (fStanceTarget == 1 && fStance == 6)
547 if (fStanceTarget == 2 && fStance == 1)
554 if (fStanceTarget == 2 && fStance == 3)
561 if (fStanceTarget == 2 && fStance == 4)
568 if (fStanceTarget == 2 && fStance == 5)
574 if (fStanceTarget == 2 && fStance == 6)
588 float yokeAnkle = getYokeAngle();
589 fTiltDn = digitalRead(fTiltDownPin);
590 fLegDn = digitalRead(fLegDownPin);
597 else if (fLegDn == 1 )
608 Serial.print(
"STOP : yokeAnkle="); Serial.println(yokeAnkle);
611 else if (fTiltDn == 1)
631 float yokeAnkle = getYokeAngle();
632 fTiltUp = digitalRead(fTiltUpPin);
633 fLegUp = digitalRead(fLegUpPin);
634 fTiltDn = digitalRead(fTiltDownPin);
643 if (fLegUp == 1 && fShowTimeThreeToTwo >= 22 && fShowTimeThreeToTwo <= 26)
648 if (fLegUp == 1 && fShowTimeThreeToTwo >= 27)
666 return (fBodyIMU != NULL && fTiltIMU != NULL) ? fBodyIMU->
getRoll() - fTiltIMU->
getRoll() : 0.0 / 0.0;
701 if (fBodyIMU != NULL)