Skip to content

Commit 38d1593

Browse files
committed
Improvements, backpack + restraining bolt support
1 parent 3508265 commit 38d1593

File tree

5 files changed

+62
-29
lines changed

5 files changed

+62
-29
lines changed

animations/look_down.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{"servo:pan:mvabs": 50},
33
{"sleep": 0.5},
4-
{"servo:tilt:mvabs": 20},
4+
{"servo:tilt:mvabs": 80},
55
{"sleep": 1}
66
]

animations/look_up.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{"servo:pan:mvabs": 50},
33
{"sleep": 0.5},
4-
{"servo:tilt:mvabs": 80},
4+
{"servo:tilt:mvabs": 20},
55
{"sleep": 1}
66
]

arduino_sketch/Config.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,18 @@
3737
#define SERVO_SPEED_MIN 20
3838
#define SERVO_SPEED_MAX 80
3939

40-
//#define DEBUG
40+
// #define DEBUG
4141

4242
#define SERVO_COUNT 8 // Number of servos to be controlled by ServoEasing
4343

4444
#define LEG_IK_MIN 75 // Min solvable height of leg between hip and ankle
45-
#define LEG_IK_MAX 170 // Max solvable height of leg between hip and ankle
45+
#define LEG_IK_MAX 180 // Max solvable height of leg between hip and ankle
4646

4747
#define NOVAL 1000
4848

49-
//#define MPU6050_ENABLED // Enable MPU6050
49+
// #define MPU6050_ENABLED // Enable MPU6050
5050
//#define MPU6050_DEBUG // Debug in serial plotter
51-
//#define ANIMATE_ENABLED // Enable random animations
52-
51+
#define ANIMATE_ENABLED // Enable random animations
5352

5453
// Arrays to store servo min / max positions to avoid mechanical issues due
5554
// NOTE: attach() disregards this, set PosRest to be within range of the servo's physical boundaries
@@ -63,24 +62,30 @@ int PosSleep[SERVO_COUNT] = {40, 60, 95, 140, 120, 85, PosMax[7], 90};
6362
//0, 3 = HIP
6463
int PosStart[SERVO_COUNT] = {60, 0, 165, 120, 180, 10, 90, 90};
6564

66-
65+
int PosBackpack[SERVO_COUNT] = {45, 90, 165, 135, 90, 10, 90, 90}; // straighten legs and point feet to fit in backpack upright
6766

6867
//int PosRest[SERVO_COUNT] = {S1_REST, S2_REST, S3_REST, S4_REST, S5_REST, S6_REST, S7_REST, S8_REST, S9_REST};
6968
int PosRest[SERVO_COUNT] = {60, 0, 165, 120, 180, 10, 90, 90};
7069

7170
int PosConfig[SERVO_COUNT] = {90, 90, 90, 90, 90, 90, 90, 90};
7271

7372
// Poses
74-
int PosStand[SERVO_COUNT] = {40, 60, 95, 140, 120, 85, NOVAL, NOVAL};
73+
int PosStand[SERVO_COUNT] = {45, 75, 95, 135, 105, 85, NOVAL, NOVAL};
7574
int PosLookLeft[SERVO_COUNT] = {NOVAL, NOVAL, NOVAL, NOVAL, NOVAL, NOVAL, NOVAL, 180};
7675
int PosLookRight[SERVO_COUNT] = {NOVAL, NOVAL, NOVAL, NOVAL, NOVAL, NOVAL, NOVAL, 0};
77-
int PosLookRandom[SERVO_COUNT] = {NOVAL, NOVAL, NOVAL, NOVAL, NOVAL, NOVAL, -1, NOVAL}; // Made random by calling the function moveRandom() if the value is -1
76+
int PosLookRandom[SERVO_COUNT] = {NOVAL, NOVAL, NOVAL, NOVAL, NOVAL, NOVAL, -1, -1}; // Made random by calling the function moveRandom() if the value is -1
7877
int PosLookUp[SERVO_COUNT] = {NOVAL, NOVAL, NOVAL, NOVAL, NOVAL, NOVAL, 60, 90};
7978
int PosLookDown[SERVO_COUNT] = {NOVAL, NOVAL, NOVAL, NOVAL, NOVAL, NOVAL, 120, 90};
8079

8180
// Array of poses except PosRest and PosSleep (which are used for initialization and reset of position)
8281
int *Poses[] = {PosStand, PosLookLeft, PosLookRight, PosLookUp, PosLookDown, PosLookRandom};
8382

83+
int backpackPin = 12;
84+
bool backpack = false;
85+
86+
int restrainPin = PIN_A1;
87+
bool restrainingBolt = false;
88+
8489
void blinkLED()
8590
{
8691
digitalWrite(LED_BUILTIN, HIGH);

arduino_sketch/ServoManager.h

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,23 @@ class ServoManager
2626
void doInit()
2727
{
2828
// IMPORTANT: Communication from Pi uses index, these must be attached in the same order as they are referenced in the pi config
29-
ServoLLH.attach(PIN_SLLH, PosStart[0]);
30-
ServoLLK.attach(PIN_SLLK, PosStart[1]);
31-
ServoLLA.attach(PIN_SLLA, PosStart[2]);
32-
ServoRLH.attach(PIN_SRLH, PosStart[3]);
33-
ServoRLK.attach(PIN_SRLK, PosStart[4]);
34-
ServoRLA.attach(PIN_SRLA, PosStart[5]);
29+
if (backpack == true)
30+
{
31+
ServoLLH.attach(PIN_SLLH, PosBackpack[0]);
32+
ServoLLK.attach(PIN_SLLK, PosBackpack[1]);
33+
ServoLLA.attach(PIN_SLLA, PosBackpack[2]);
34+
ServoRLH.attach(PIN_SRLH, PosBackpack[3]);
35+
ServoRLK.attach(PIN_SRLK, PosBackpack[4]);
36+
ServoRLA.attach(PIN_SRLA, PosBackpack[5]);
37+
}
38+
else {
39+
ServoLLH.attach(PIN_SLLH, PosStart[0]);
40+
ServoLLK.attach(PIN_SLLK, PosStart[1]);
41+
ServoLLA.attach(PIN_SLLA, PosStart[2]);
42+
ServoRLH.attach(PIN_SRLH, PosStart[3]);
43+
ServoRLK.attach(PIN_SRLK, PosStart[4]);
44+
ServoRLA.attach(PIN_SRLA, PosStart[5]);
45+
}
3546
ServoNT.attach(PIN_SNT, PosStart[6]);
3647
ServoNP.attach(PIN_SNP, PosStart[7]);
3748

@@ -118,20 +129,18 @@ class ServoManager
118129

119130
void moveSingleServo(uint8_t pServoIndex, int pPos, boolean isRelative)
120131
{
121-
if (isRelative)
132+
if ((backpack == true || restrainingBolt == true) && pServoIndex < 6)
133+
{
134+
// Serial.println("Backpack or restrained mode, skipping leg servos");
135+
}
136+
else if (isRelative)
122137
{
123-
// Serial.print(" Moving from: ");
124-
// Serial.print(ServoEasing::ServoEasingNextPositionArray[pServoIndex]);
125-
// Serial.print(" to: ");
126-
// Serial.println(ServoEasing::ServoEasingNextPositionArray[pServoIndex] + pPos);
127138
ServoEasing::ServoEasingNextPositionArray[pServoIndex] = ServoEasing::ServoEasingNextPositionArray[pServoIndex] + pPos;
128139
}
129140
else
130141
{
131142
ServoEasing::ServoEasingNextPositionArray[pServoIndex] = pPos;
132143
}
133-
// setEaseToForAllServosSynchronizeAndStartInterrupt(tSpeed);
134-
135144
// Return actual value to Pi
136145
PiConnect::write_i16(ServoEasing::ServoEasingNextPositionArray[pServoIndex]);
137146
}

arduino_sketch/arduino_sketch.ino

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ boolean calibrateRest = false;
3333

3434
bool shouldMove = false;
3535

36+
bool piControl = false;
37+
3638
void setup()
3739
{
3840
// Init LED pin
@@ -47,6 +49,21 @@ void setup()
4749
servoManager.doInit();
4850
servoManager.setSpeed(SERVO_SPEED_MIN);
4951

52+
53+
pinMode(backpackPin, INPUT_PULLUP); // sets the digital pin as input
54+
if (digitalRead(backpackPin) == LOW) // Check once on startup
55+
{
56+
Serial.println("Backpack detected");
57+
backpack = true;
58+
}
59+
60+
pinMode(restrainPin, INPUT_PULLUP); // sets the digital pin as input
61+
if (digitalRead(restrainPin) == LOW) // Check once on startup
62+
{
63+
Serial.println("Restraint detected");
64+
restrainingBolt = true;
65+
}
66+
5067
#ifdef MPU6050_ENABLED
5168
tilt.doInit();
5269
#endif
@@ -57,7 +74,7 @@ void setup()
5774
// allTo90();
5875

5976
// Move to rest position + calculate IK and store as rest position
60-
//doRest();
77+
// doRest();
6178

6279
// Custom log message (enable DEBUG in Config.h to see this)
6380
cLog("Start loop");
@@ -83,7 +100,7 @@ void allTo90()
83100
void doRest()
84101
{
85102
cLog("Resting");
86-
isResting = true;
103+
87104
// Reset to slow speed
88105
servoManager.setSpeed(SERVO_SPEED_MIN);
89106
if (calibrateRest == false)
@@ -163,6 +180,7 @@ void loop()
163180
bool receiving = getOrdersFromPi();
164181
while(receiving)
165182
{
183+
piControl = true;
166184
delay(50); // Wait a short time for any other orders
167185
receiving = getOrdersFromPi();
168186
shouldMove = !receiving; // Only move when there are no other orders
@@ -179,17 +197,17 @@ void loop()
179197
// Orders from pi will set sleep time so that the animation does not take precedence
180198
if (!isSleeping())
181199
{
182-
if (isResting)
200+
if (isResting && piControl == false) // Only do this if the Pi is not in control (i.e. switched off)
183201
{
184202
#ifdef ANIMATE_ENABLED
185203
animateRandomly();
186204
#endif
187-
setSleep(random(3000, 5000));
188205
}
189206
else
190207
{
191208
// doRest();
192-
setSleep(random(3000, 20000));
209+
isResting = true;
210+
setSleep(random(1000, 5000));
193211
}
194212
}
195213

@@ -219,7 +237,8 @@ void animateRandomly()
219237
// Scale headTiltOffset value between 0 and 180 (inverted) to scale of LEG_IK_MIN and LEG_IK_MAX
220238
float legHeight = map(headTiltOffset, 180, 0, LEG_IK_MIN, LEG_IK_MAX);
221239
// Move legs to that height
222-
servoManager.moveLegs(legHeight, 0);
240+
// servoManager.moveLegs(LEG_IK_MAX, 0);
241+
servoManager.moveServos(PosStand);
223242
shouldMove = true;
224243
#ifdef DEBUG
225244
Serial.print("Moving legs ");

0 commit comments

Comments
 (0)