Added firmware
Will Brown
11 years ago
0 | 0 | #define BAD_REQUEST "BADREQUEST" |
1 | 1 | #define HEARTBEAT_PULSE "PULSE" |
2 | 2 | #define MESSAGE_TIMEOUT_CYCLES 100 |
3 | #define MOTOR_ARM_TIME 5000 | |
4 | #define MOTOR_ARM_VALUE 20 | |
3 | 5 | #define MOTOR_COUNT 4 |
4 | 6 | #define SPEED_HEADER "NEWSPEED" |
5 | 7 | #define STATUS_LED 13 |
7 | 9 | #define WORD_BUFFER_LENGTH 4 |
8 | 10 | #define WORD_LENGTH 3 |
9 | 11 | |
12 | #include <SoftwareServo.h> | |
13 | ||
10 | 14 | struct motor { |
11 | 15 | unsigned char pin; |
12 | 16 | unsigned int current_speed; |
13 | 17 | unsigned int next_speed; |
18 | SoftwareServo ctrl; | |
14 | 19 | }; |
15 | 20 | |
16 | 21 | struct motor motors[MOTOR_COUNT] = { |
17 | {6, 0, 0}, {9, 0, 0}, {10, 0, 0}, {11, 0, 0} | |
22 | {6, 0, 0, NULL}, {9, 0, 0, NULL}, | |
23 | {10, 0, 0, NULL}, {11, 0, 0, NULL} | |
18 | 24 | }; |
19 | 25 | |
20 | 26 | char buffer[WORD_BUFFER_LENGTH]; |
27 | 33 | void write_speeds(void) { |
28 | 34 | for (int i = 0; i < MOTOR_COUNT; i++) { |
29 | 35 | motors[i].current_speed = motors[i].next_speed; |
30 | analogWrite(motors[i].pin, motors[i].current_speed); | |
36 | motors[i].ctrl.write(motors[i].current_speed); | |
31 | 37 | } |
32 | 38 | } |
33 | 39 | |
40 | /** | |
41 | * Print the speeds of the motors to serial. | |
42 | */ | |
34 | 43 | void print_speeds(void) { |
35 | 44 | Serial.print(SPEED_HEADER); |
36 | 45 | for (int i = 0; i < MOTOR_COUNT; i++) { |
37 | Serial.print(":" | |
38 | ); | |
46 | Serial.print(":"); | |
39 | 47 | Serial.print(motors[i].current_speed); |
40 | 48 | } |
41 | 49 | Serial.println(); |
50 | } | |
51 | ||
52 | /** | |
53 | * Initialize and arm motors | |
54 | */ | |
55 | void init_motors(void) { | |
56 | for (int i = 0; i < MOTOR_COUNT; i++) { | |
57 | motors[i].ctrl.attach(motors[i].pin); | |
58 | } | |
59 | ||
60 | long time = millis(); | |
61 | while (millis() - time < MOTOR_ARM_TIME) { | |
62 | for (int i = 0; i < MOTOR_COUNT; i++) { | |
63 | motors[i].ctrl.write(MOTOR_ARM_VALUE); | |
64 | } | |
65 | } | |
42 | 66 | } |
43 | 67 | |
44 | 68 | /** |
49 | 73 | pinMode(STATUS_LED, OUTPUT); |
50 | 74 | |
51 | 75 | /* Initialize the motor controllers. */ |
52 | for (int i = 0; i < MOTOR_COUNT; i++) { | |
53 | pinMode(motors[i].pin, OUTPUT); | |
54 | analogWrite(motors[i].pin, 0); | |
55 | } | |
76 | init_motors(); | |
56 | 77 | |
57 | 78 | /* Initialize serial monitor. */ |
58 | 79 | Serial.begin(115200); |
0 | #include <SoftwareServo.h> | |
1 | ||
2 | // Motors | |
3 | #define PIN_FRONT 11 | |
4 | #define PIN_REAR 5 | |
5 | #define PIN_LEFT 6 | |
6 | #define PIN_RIGHT 7 | |
7 | #define MINCOMMAND 0 | |
8 | ||
9 | struct Motor { | |
10 | int command[4]; | |
11 | int axisCommand[3]; | |
12 | boolean armed; | |
13 | int minimum; | |
14 | } motor = { | |
15 | { MINCOMMAND, MINCOMMAND, MINCOMMAND, MINCOMMAND }, | |
16 | { 0, 0, 0 }, | |
17 | false, | |
18 | 1000 | |
19 | }; | |
20 | ||
21 | SoftwareServo motorFront; | |
22 | SoftwareServo motorRear; | |
23 | SoftwareServo motorLeft; | |
24 | SoftwareServo motorRight; | |
25 | ||
26 | void setupMotors() { | |
27 | motorFront.attach(PIN_FRONT); | |
28 | motorRear.attach(PIN_REAR); | |
29 | motorLeft.attach(PIN_LEFT); | |
30 | motorRight.attach(PIN_RIGHT); | |
31 | ||
32 | // Arm ESCs | |
33 | setAllMotors(MINCOMMAND); | |
34 | updateMotors(); | |
35 | unsigned long tempTime = millis(); | |
36 | while(millis()<tempTime+1000*4) { | |
37 | SoftwareServo::refresh(); | |
38 | } | |
39 | } | |
40 | ||
41 | void wait(int ms) { | |
42 | int temp = millis(); | |
43 | while(millis()-temp<ms) { | |
44 | SoftwareServo::refresh(); | |
45 | } | |
46 | } | |
47 | ||
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 |
0 | #include <Servo.h> | |
1 | #define MOTOR_MIN 68 | |
2 | #define MOTOR_MAX 172 | |
3 | ||
4 | Servo motor; | |
5 | ||
6 | void setup() { | |
7 | long time = millis(); | |
8 | motor.attach(11); | |
9 | ||
10 | while (millis() - time < 5000) { | |
11 | motor.write(20); | |
12 | } | |
13 | } | |
14 | ||
15 | void loop() { | |
16 | motor.write(100); | |
17 | /* | |
18 | time = millis(); | |
19 | while (millis() - time < 4000) { | |
20 | motor.write(172); // MAX | |
21 | } | |
22 | ||
23 | time = millis(); | |
24 | while (millis() - time < 8000) { | |
25 | motor.write(130); | |
26 | } | |
27 | */ | |
28 | } |