git.haldean.org droidcopter / 8dbcb86
Added firmware Will Brown 11 years ago
3 changed file(s) with 120 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
00 #define BAD_REQUEST "BADREQUEST"
11 #define HEARTBEAT_PULSE "PULSE"
22 #define MESSAGE_TIMEOUT_CYCLES 100
3 #define MOTOR_ARM_TIME 5000
4 #define MOTOR_ARM_VALUE 20
35 #define MOTOR_COUNT 4
46 #define SPEED_HEADER "NEWSPEED"
57 #define STATUS_LED 13
79 #define WORD_BUFFER_LENGTH 4
810 #define WORD_LENGTH 3
911
12 #include <SoftwareServo.h>
13
1014 struct motor {
1115 unsigned char pin;
1216 unsigned int current_speed;
1317 unsigned int next_speed;
18 SoftwareServo ctrl;
1419 };
1520
1621 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}
1824 };
1925
2026 char buffer[WORD_BUFFER_LENGTH];
2733 void write_speeds(void) {
2834 for (int i = 0; i < MOTOR_COUNT; i++) {
2935 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);
3137 }
3238 }
3339
40 /**
41 * Print the speeds of the motors to serial.
42 */
3443 void print_speeds(void) {
3544 Serial.print(SPEED_HEADER);
3645 for (int i = 0; i < MOTOR_COUNT; i++) {
37 Serial.print(":"
38 );
46 Serial.print(":");
3947 Serial.print(motors[i].current_speed);
4048 }
4149 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 }
4266 }
4367
4468 /**
4973 pinMode(STATUS_LED, OUTPUT);
5074
5175 /* 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();
5677
5778 /* Initialize serial monitor. */
5879 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 }