git.haldean.org droidcopter / f647aa6
Added python script to communicate with the firmware Will Brown 11 years ago
2 changed file(s) with 91 addition(s) and 35 deletion(s). Raw diff Collapse all Expand all
0 # file: rfcomm-client.py
1 # auth: Albert Huang <albert@csail.mit.edu>
2 # desc: simple demonstration of a client application that uses RFCOMM sockets
3 # intended for use with rfcomm-server
4 #
5 # $Id: rfcomm-client.py 424 2006-08-24 03:35:54Z albert $
6
7 from bluetooth import *
8 import sys
9
10 addr = None
11
12 if len(sys.argv) < 2:
13 print "Searching via UUID"
14 else:
15 addr = sys.argv[1]
16 print "Searching for %s" % addr
17
18 # search for the SampleServer service
19 uuid = "00001101-0000-1000-8000-00805F9B34FB"
20 service_matches = find_service( uuid = uuid, address = addr )
21
22 if len(service_matches) == 0:
23 print "couldn't find the SampleServer service =("
24 sys.exit(0)
25
26 first_match = service_matches[0]
27 port = first_match["port"]
28 name = first_match["name"]
29 host = first_match["host"]
30
31 print "connecting to \"%s\" on %s" % (name, host)
32
33 # Create the client socket
34 sock=BluetoothSocket( RFCOMM )
35 sock.connect((host, port))
36
37 print "connected. type stuff"
38 while True:
39 data = raw_input()
40 if len(data) == 0: break
41 sock.send(data)
42
43 sock.close()
0 #include <Messenger.h>
1
0 #define BAD_REQUEST "BADREQUEST"
1 #define HEARTBEAT_PULSE "PULSE"
2 #define MESSAGE_TIMEOUT_CYCLES 100
3 #define MOTOR_COUNT 4
4 #define SPEED_HEADER "NEWSPEED"
25 #define STATUS_LED 13
3 #define MOTOR_COUNT 4
46 #define STATUS_LED_CYCLES 30000
5 #define DEBUG_RESPONSE 1
6
7 Messenger serial = Messenger();
8 unsigned int led_cycles = 0;
7 #define WORD_BUFFER_LENGTH 4
8 #define WORD_LENGTH 3
99
1010 struct motor {
1111 unsigned char pin;
1616 struct motor motors[MOTOR_COUNT] = {
1717 {6, 0, 0}, {9, 0, 0}, {10, 0, 0}, {11, 0, 0}
1818 };
19
20 char buffer[WORD_BUFFER_LENGTH];
21 unsigned int led_cycles = 0;
22
1923
2024 /**
2125 * Updates the speeds of the motors.
2832 }
2933
3034 void print_speeds(void) {
31 Serial.print("New speeds: ");
35 Serial.print(SPEED_HEADER);
3236 for (int i = 0; i < MOTOR_COUNT; i++) {
37 Serial.print(":"
38 );
3339 Serial.print(motors[i].current_speed);
34 Serial.print(' ');
3540 }
3641 Serial.println();
37 }
38
39 /**
40 * Handle a message from the controller. Sets the speeds then
41 * requests a speed update.
42 */
43 void handle_message(void) {
44 for (int i = 0; i < MOTOR_COUNT; i++) {
45 if (! serial.available()) {
46 Serial.println("You didn't send me a correctly formatted message.");
47 #ifdef DEBUG_RESPONSE
48 print_speeds();
49 #endif
50 return;
51 } else {
52 motors[i].next_speed = serial.readInt() % 256;
53 }
54 }
55
56 write_speeds();
57 #ifdef DEBUG_RESPONSE
58 print_speeds();
59 #endif
6042 }
6143
6244 /**
7456
7557 /* Initialize serial monitor. */
7658 Serial.begin(115200);
77 serial.attach(handle_message);
7859 }
7960
8061 /**
8162 * Wait for serial information to come in.
8263 */
8364 void loop(void) {
84 while (Serial.available() > 0) {
65 if (Serial.available() >= WORD_LENGTH) {
66 int i, j, timeout;
8567 led_cycles = 0;
8668 digitalWrite(STATUS_LED, HIGH);
8769
88 serial.process(Serial.read());
70 for (i=0; i<MOTOR_COUNT; i++) {
71 for (timeout=0; timeout < MESSAGE_TIMEOUT_CYCLES; timeout++) {
72 if (Serial.available() >= WORD_LENGTH) break;
73 }
74
75 if (Serial.available() >= WORD_LENGTH) {
76 for (j=0; j<WORD_LENGTH; j++) {
77 buffer[j] = Serial.read();
78 }
79
80 buffer[WORD_LENGTH] = '\0';
81 motors[i].next_speed = atoi(buffer);
82 } else {
83 Serial.println(BAD_REQUEST);
84 }
85 }
86
87 /* Flush serial buffer after reading a line. */
88 while (Serial.available()) {
89 Serial.print("");
90 Serial.read();
91 }
92
93 write_speeds();
94 print_speeds();
8995 }
9096
9197 led_cycles++;
9298 if (led_cycles >= STATUS_LED_CYCLES) {
9399 digitalWrite(STATUS_LED, LOW);
94100 }
101
102 if (led_cycles % 10000 == 0) {
103 // Serial.println(HEARTBEAT_PULSE);
104 }
105
95106 }
107