demo demo
/
BluetoothSumo
Revision 0:41f85a3f645d, committed 2009-09-19
- Comitter:
- demo
- Date:
- Sat Sep 19 18:32:13 2009 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r 41f85a3f645d Motor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motor.cpp Sat Sep 19 18:32:13 2009 +0000 @@ -0,0 +1,24 @@ +// H-Bridge Motor Control +// Copyright (c) 2009 sford +// Released under the MIT License: http://mbed.org/license/mit + +#include "Motor.h" + +#include "mbed.h" + +Motor::Motor(PinName pwm, PinName fwd, PinName rev) : _pwm(pwm), _fwd(fwd), _rev(rev) { + + // Set initial condition of PWM + _pwm.period(0.001); + _pwm = 0; + + // Initial condition of output enables + _fwd = 0; + _rev = 0; +} + +void Motor::speed(float speed) { + _fwd = (speed > 0.0); + _rev = (speed < 0.0); + _pwm = abs(speed); +}
diff -r 000000000000 -r 41f85a3f645d Motor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motor.h Sat Sep 19 18:32:13 2009 +0000 @@ -0,0 +1,21 @@ +// H-Bridge Motor Control +// Copyright (c) 2009 sford +// Released under the MIT License: http://mbed.org/license/mit + +#ifndef MBED_MOTOR_H +#define MBED_MOTOR_H + +#include "mbed.h" + +class Motor { +public: + Motor(PinName pwm, PinName fwd, PinName rev); + void speed(float); + +protected: + PwmOut _pwm; + DigitalOut _fwd; + DigitalOut _rev; +}; + +#endif
diff -r 000000000000 -r 41f85a3f645d NokiaPresenter.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NokiaPresenter.cpp Sat Sep 19 18:32:13 2009 +0000 @@ -0,0 +1,101 @@ +// Nokia Presenter Bluetooth +// Copyright (c) 2009 sford +// Released under the MIT License: http://mbed.org/license/mit + +#include "NokiaPresenter.h" + +#include "mbed.h" + +NokiaPresenter::NokiaPresenter(PinName tx, PinName rx, PinName rst) : _bt(tx, rx), _rst(rst) { + printf("Setup bt module\n"); + _bt.baud(9600); + _rst = 0; + wait(0.5); + _rst = 1; + wait(0.5); + + printf("Ensure we are in AT command mode...\n"); + _bt.printf("+++"); + wait(1); + while (_bt.readable()) { + _bt.getc(); // empty buffer + } + + printf("Looking for BT Module...\n"); + at("AT\r", "\r\nOK\r\n"); + + printf("Reset factory settings...\n"); + at("AT&F\r", "\r\nOK\r\n"); + + printf("Setting name...\n"); + at("AT+BTLNM=\"simon\"\r", "\r\nOK\r\n"); + + printf("Allow connections...\n"); + at("AT+BTAUT=1, 0\r", "\r\nOK\r\n"); + + printf("Start BT server...\n"); + at("AT+BTSRV=1\r", "\r\nOK\r\n"); + + printf("Waiting for presenter connection...\n"); + char buffer[23]; + for (int i=0; i<21; i++) { + buffer[i] = _bt.getc(); + } + + printf("Got request, starting handshake...\n"); + for (int i=0; i<21; i++) { + _bt.putc(buffer[i]); + } + for (int i=0; i<3; i++) { + buffer[i] = _bt.getc(); + } + const char connect[27] = {0x05,0x18,0x00,0x01,0x10,0x01,0x02,0x00,0xFF,0xFF,0x00,0x44,0x00,0x65,0x00,0x73,0x00,0x6B,0x00,0x74,0x00,0x6F,0x00,0x70,0x00,0x00,0x00}; + for (int i=0; i<27; i++) { + _bt.putc(connect[i]); + } + + printf("Handshake complete, waiting for desktop request...\n"); + for (int i=0; i<23; i++) { + buffer[i] = _bt.getc(); + } + for (int i=0; i<23; i++) { + _bt.putc(buffer[i]); + } + + printf("Presenter connection all setup!\n"); +} + +char NokiaPresenter::key() { + char buffer[6]; + for (int i=0; i<6; i++) { + buffer[i] = _bt.getc(); + } + + if (buffer[3] == 0x01) { + return 0x00; // keyup + } + if (buffer[5] == 0xF8) { + switch (buffer[4]) { + case 0x07: + return 'L'; + case 0x08: + return 'R'; + case 0x09: + return 'U'; + case 0x0A: + return 'D'; + case 0x45: + return 'C'; + } + } + return buffer[4]; +} + +void NokiaPresenter::at(char *command, char *response) { + _bt.printf(command); + for (int i=0; i<strlen(response); i++) { + if (_bt.getc() != response[i]) { + error("AT Command [%s] Failed", command); + } + } +}
diff -r 000000000000 -r 41f85a3f645d NokiaPresenter.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NokiaPresenter.h Sat Sep 19 18:32:13 2009 +0000 @@ -0,0 +1,21 @@ +// Nokia Presenter Bluetooth +// Copyright (c) 2009 sford +// Released under the MIT License: http://mbed.org/license/mit + +#include "mbed.h" + +#ifndef MBED_NOKIA_PRESENTER_H +#define MBED_NOKIA_PRESENTER_H + +class NokiaPresenter { +public: + NokiaPresenter(PinName tx, PinName rx, PinName rst); + char key(); + +protected: + void at(char *command, char *response); + Serial _bt; + DigitalOut _rst; +}; + +#endif
diff -r 000000000000 -r 41f85a3f645d main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Sep 19 18:32:13 2009 +0000 @@ -0,0 +1,48 @@ +// Sumo Robot control via Bluetooth/Nokia Presenter +// Copyright (c) 2009 sford +// Released under the MIT License: http://mbed.org/license/mit + +#include "mbed.h" +#include "Motor.h" +#include "NokiaPresenter.h" + +Motor left(p23, p6, p5); // pwm, fwd, rev +Motor right(p24, p8, p7); // pwm, fwd, rev + +BusOut leds(LED1, LED2, LED3, LED4); + +int main() { + leds = 1; + NokiaPresenter nokia(p28, p27, p29); // tx, rx, rst + leds = 2; + + while(1) { + switch(nokia.key()) { + case 0: + left.speed(0.0); + right.speed(0.0); + leds = 0; + break; + case 'U': + left.speed(1.0); + right.speed(1.0); + leds = 1 << 0 | 1 << 2; + break; + case 'D': + left.speed(-1.0); + right.speed(-1.0); + leds = 1 << 1 | 1 << 3; + break; + case 'L': + left.speed(-1.0); + right.speed(1.0); + leds = 1 << 1 | 1 << 2; + break; + case 'R': + left.speed(1.0); + right.speed(-1.0); + leds = 1 << 0 | 1 << 3; + break; + } + } +}
diff -r 000000000000 -r 41f85a3f645d mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Sep 19 18:32:13 2009 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/f63353af7be8