Update firmware for better stability at low speeds.
Will Brown
11 years ago
7 | 7 | #define STATUS_LED 13 |
8 | 8 | #define STATUS_LED_CYCLES 30000 |
9 | 9 | #define WORD_BUFFER_LENGTH 4 |
10 | #define WORD_LENGTH 3 | |
10 | #define WORD_LENGTH 2 | |
11 | 11 | |
12 | #include <SoftwareServo.h> | |
12 | #include <Servo.h> | |
13 | 13 | |
14 | 14 | struct motor { |
15 | 15 | unsigned char pin; |
16 | 16 | unsigned int current_speed; |
17 | 17 | unsigned int next_speed; |
18 | SoftwareServo ctrl; | |
18 | Servo ctrl; | |
19 | 19 | }; |
20 | 20 | |
21 | 21 | struct motor motors[MOTOR_COUNT] = { |
22 | {6, 0, 0, SoftwareServo()}, {9, 0, 0, SoftwareServo()}, | |
23 | {10, 0, 0, SoftwareServo()}, {11, 0, 0, SoftwareServo()} | |
22 | {6, 0, 0, Servo()}, {9, 0, 0, Servo()}, | |
23 | {10, 0, 0, Servo()}, {11, 0, 0, Servo()} | |
24 | 24 | }; |
25 | 25 | |
26 | 26 | char buffer[WORD_BUFFER_LENGTH]; |
68 | 68 | * Initialize messenger service and speed vector. |
69 | 69 | */ |
70 | 70 | void setup(void) { |
71 | /* Initialize serial monitor. */ | |
72 | Serial.begin(115200); | |
73 | ||
71 | 74 | /* Set the serial status LED to output. */ |
72 | 75 | pinMode(STATUS_LED, OUTPUT); |
73 | 76 | |
74 | 77 | /* Initialize the motor controllers. */ |
75 | 78 | init_motors(); |
76 | 79 | |
77 | /* Initialize serial monitor. */ | |
78 | Serial.begin(115200); | |
80 | Serial.println("BEGIN"); | |
79 | 81 | } |
80 | 82 | |
81 | 83 | /** |
98 | 100 | } |
99 | 101 | |
100 | 102 | buffer[WORD_LENGTH] = '\0'; |
101 | motors[i].next_speed = atoi(buffer); | |
103 | motors[i].next_speed = map(atoi(buffer), -1, 99, 67, 160); | |
102 | 104 | } else { |
103 | 105 | Serial.println(BAD_REQUEST); |
104 | 106 | } |
119 | 121 | digitalWrite(STATUS_LED, LOW); |
120 | 122 | } |
121 | 123 | |
124 | #ifdef ENABLE_HEARTBEAT | |
122 | 125 | if (led_cycles % 10000 == 0) { |
123 | // Serial.println(HEARTBEAT_PULSE); | |
126 | Serial.println(HEARTBEAT_PULSE); | |
124 | 127 | } |
128 | #endif | |
125 | 129 | |
126 | 130 | } |
127 | 131 |
0 | #include <SoftwareServo.h> | |
0 | #include <Servo.h> | |
1 | 1 | |
2 | 2 | // Motors |
3 | 3 | #define PIN_FRONT 11 |
4 | #define PIN_REAR 5 | |
5 | #define PIN_LEFT 6 | |
6 | #define PIN_RIGHT 7 | |
4 | #define PIN_REAR 9 | |
5 | #define PIN_LEFT 10 | |
6 | #define PIN_RIGHT 6 | |
7 | 7 | #define MINCOMMAND 0 |
8 | 8 | |
9 | struct Motor { | |
9 | struct Motors { | |
10 | 10 | int command[4]; |
11 | 11 | int axisCommand[3]; |
12 | 12 | boolean armed; |
18 | 18 | 1000 |
19 | 19 | }; |
20 | 20 | |
21 | SoftwareServo motorFront; | |
22 | SoftwareServo motorRear; | |
23 | SoftwareServo motorLeft; | |
24 | SoftwareServo motorRight; | |
21 | Servo motorFront; | |
22 | Servo motorRear; | |
23 | Servo motorLeft; | |
24 | Servo motorRight; | |
25 | 25 | |
26 | 26 | void setupMotors() { |
27 | 27 | motorFront.attach(PIN_FRONT); |
32 | 32 | // Arm ESCs |
33 | 33 | setAllMotors(MINCOMMAND); |
34 | 34 | updateMotors(); |
35 | unsigned long tempTime = millis(); | |
36 | while(millis()<tempTime+1000*4) { | |
37 | SoftwareServo::refresh(); | |
35 | } | |
36 | ||
37 | void setup() { | |
38 | setupMotors(); | |
39 | } | |
40 | ||
41 | void loop() {;} | |
42 | ||
43 | void setAllMotors(int cmd) { | |
44 | int i; | |
45 | for (i=0; i<4; i++) { | |
46 | motor.command[i] = cmd; | |
38 | 47 | } |
39 | 48 | } |
40 | 49 | |
41 | void wait(int ms) { | |
42 | int temp = millis(); | |
43 | while(millis()-temp<ms) { | |
44 | SoftwareServo::refresh(); | |
50 | void updateMotors() { | |
51 | int i; | |
52 | for (i=0; i<4; i++) { | |
53 | motorFront.write(motor.command[i]); | |
45 | 54 | } |
46 | 55 | } |
47 | 56 | |
48 | void setAllMotors(int cmd) { | |
49 | motor.command[MOTOR_FRONT] = cmd; | |
50 | motor.command[MOTOR_REAR] = cmd; | |
51 | motor.command[MOTOR_LEFT] = cmd; | |
52 | motor.command[MOTOR_RIGHT] = cmd; | |
53 | } | |
54 | ||
55 | void updateMotors() { | |
56 | motorFront.write(motor.command[MOTOR_FRONT]); | |
57 | motorRear.write(motor.command[MOTOR_REAR]); | |
58 | motorLeft.write(motor.command[MOTOR_LEFT]); | |
59 | motorRight.write(motor.command[MOTOR_RIGHT]); | |
60 | } | |
61 |