Drivers for the mini robot designed for Princeton's MAE 433 course.

Dependencies:   mbed-dsp mbed-rtos mbed

Dependents:   MAE433_Library_Tester RobotBalancerv2

Committer:
Electrotiger
Date:
Thu Jun 30 23:52:51 2016 +0000
Revision:
8:da95e4ccbe4c
Parent:
5:e00cc0dab1c7
Decreased H-Bridge Period to Provide for Even Finer Control.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Electrotiger 0:9afc272fa65f 1 /*
Electrotiger 0:9afc272fa65f 2 * HC06Bluetooth.cpp
Electrotiger 0:9afc272fa65f 3 *
Electrotiger 0:9afc272fa65f 4 * Created on: Jun 4, 2016
Electrotiger 0:9afc272fa65f 5 * Author: Developer
Electrotiger 0:9afc272fa65f 6 */
Electrotiger 0:9afc272fa65f 7
Electrotiger 0:9afc272fa65f 8 #include <HC06Bluetooth.hpp>
Electrotiger 0:9afc272fa65f 9 #include <algorithm>
Electrotiger 0:9afc272fa65f 10
Electrotiger 0:9afc272fa65f 11 /* Static methods used to help configure the Baudrate. */
Electrotiger 0:9afc272fa65f 12
Electrotiger 0:9afc272fa65f 13 // WARNING: DO NOT CHANGE THESE VALUES, AS THEY ARE USED TO INDEX INTO AN ARRAY FOR IMPLEMENTATION.
Electrotiger 0:9afc272fa65f 14 enum Baudrate {B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600, B1382400, END};
Electrotiger 0:9afc272fa65f 15 const char* BaudATString[] = {"AT+BAUD1", "AT+BAUD2", "AT+BAUD3", "AT+BAUD4", "AT+BAUD5", "AT+BAUD6", "AT+BAUD7", "AT+BAUD8", "AT+BAUD9", "AT+BAUDA", "AT+BAUDB", "AT+BAUDC"};
Electrotiger 0:9afc272fa65f 16 const int32_t BaudATReplyLength[] = {6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 9};
Electrotiger 0:9afc272fa65f 17 //const char* BaudATReplyPattern[] = {"OK1200", "OK2400", "OK4800","OK9600","OK19200","OK38400","OK57600","OK115200","OK230400","OK460800","OK921600","OK1382400"};
Electrotiger 0:9afc272fa65f 18 const int32_t BaudValue[] = {1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 1382400};
Electrotiger 0:9afc272fa65f 19
Electrotiger 0:9afc272fa65f 20 /* HC06 Bluetooth Class Implementation: */
Electrotiger 0:9afc272fa65f 21 HC06Bluetooth::HC06Bluetooth(PinName TX, PinName RX, void (*lineCallbackFunc) (const char* readString), void (*charCallbackFunc) (char readChar))
Electrotiger 0:9afc272fa65f 22 : btSerialObj(TX, RX), lineCallbackFunc(lineCallbackFunc), charCallbackFunc(charCallbackFunc) {
Electrotiger 0:9afc272fa65f 23 btSerialObj.baud(115200);
Electrotiger 0:9afc272fa65f 24
Electrotiger 0:9afc272fa65f 25 // Set the interrupt to be called when a byte is received.
Electrotiger 0:9afc272fa65f 26 if ((lineCallbackFunc != NULL) || (charCallbackFunc != NULL)) {
Electrotiger 0:9afc272fa65f 27 btSerialObj.attach(this, &HC06Bluetooth::receiveByteISR);
Electrotiger 0:9afc272fa65f 28 }
Electrotiger 0:9afc272fa65f 29 }
Electrotiger 0:9afc272fa65f 30
Electrotiger 0:9afc272fa65f 31 void HC06Bluetooth::runSetup(std::string deviceName, std::string PIN) {
Electrotiger 0:9afc272fa65f 32 int numCharsReceived = 0;
Electrotiger 0:9afc272fa65f 33 // Detatch the interrupt.
Electrotiger 0:9afc272fa65f 34 btSerialObj.attach(NULL);
Electrotiger 0:9afc272fa65f 35 /* Sweep through a list of Baud rates until we find the one that the device has previously been set to. */
Electrotiger 0:9afc272fa65f 36 bool baudFound = false;
Electrotiger 0:9afc272fa65f 37 Timer timeOut;
Electrotiger 0:9afc272fa65f 38 timeOut.start();
Electrotiger 0:9afc272fa65f 39 // For every baud rate in the list:
Electrotiger 0:9afc272fa65f 40 for (int i = 0; (i < END) && (!baudFound); i++) {
Electrotiger 0:9afc272fa65f 41 // Set the communication baud rate to it.
Electrotiger 0:9afc272fa65f 42 btSerialObj.baud(BaudValue[i]);
Electrotiger 0:9afc272fa65f 43 // Send the test command "AT" to the device.
Electrotiger 0:9afc272fa65f 44 btSerialObj.puts("AT");
Electrotiger 0:9afc272fa65f 45 // While the time out has not been reached:
Electrotiger 0:9afc272fa65f 46 for(timeOut.reset(); timeOut.read_ms() < 1000; ) {
Electrotiger 0:9afc272fa65f 47 // If the serial object is readable, make sure the read character matches the reply string "OK".
Electrotiger 0:9afc272fa65f 48 if (btSerialObj.readable() && !baudFound) {
Electrotiger 0:9afc272fa65f 49 baudFound = true;
Electrotiger 0:9afc272fa65f 50 break;
Electrotiger 0:9afc272fa65f 51 }
Electrotiger 0:9afc272fa65f 52 }
Electrotiger 0:9afc272fa65f 53 }
Electrotiger 0:9afc272fa65f 54 // Flush whatever's in the input buffer.
Electrotiger 0:9afc272fa65f 55 while(btSerialObj.readable()) {
Electrotiger 0:9afc272fa65f 56 btSerialObj.getc();
Electrotiger 0:9afc272fa65f 57 }
Electrotiger 0:9afc272fa65f 58 //Overwrite the Baud rate to 115200.
Electrotiger 0:9afc272fa65f 59 btSerialObj.puts(BaudATString[B115200]);
Electrotiger 0:9afc272fa65f 60 btSerialObj.baud(BaudValue[B115200]);
Electrotiger 0:9afc272fa65f 61 // Wait for the 8 character reply "OK115200"
Electrotiger 0:9afc272fa65f 62 for(numCharsReceived = 0 ; numCharsReceived < BaudATReplyLength[B115200]; numCharsReceived++) {
Electrotiger 0:9afc272fa65f 63 //while(!btSerialObj.readable());
Electrotiger 0:9afc272fa65f 64 btSerialObj.getc();
Electrotiger 0:9afc272fa65f 65 }
Electrotiger 0:9afc272fa65f 66
Electrotiger 0:9afc272fa65f 67 // Set the name of the device.
Electrotiger 0:9afc272fa65f 68 btSerialObj.puts(("AT+NAME" + deviceName.substr(0,20)).c_str());
Electrotiger 0:9afc272fa65f 69 // Wait for the 6 character reply "OKname"
Electrotiger 0:9afc272fa65f 70 for(numCharsReceived = 0 ; numCharsReceived < 6; numCharsReceived++) {
Electrotiger 0:9afc272fa65f 71 while(!btSerialObj.readable());
Electrotiger 0:9afc272fa65f 72 btSerialObj.getc();
Electrotiger 0:9afc272fa65f 73 }
Electrotiger 0:9afc272fa65f 74
Electrotiger 0:9afc272fa65f 75 //Set the password of the device.
Electrotiger 0:9afc272fa65f 76 btSerialObj.puts(("AT+PIN" + PIN.substr(0, 4)).c_str());
Electrotiger 0:9afc272fa65f 77 // Wait for the 8 character reply "OKsetpin"
Electrotiger 0:9afc272fa65f 78 for(numCharsReceived = 0 ; numCharsReceived < 8; numCharsReceived++) {
Electrotiger 0:9afc272fa65f 79 while(!btSerialObj.readable());
Electrotiger 0:9afc272fa65f 80 btSerialObj.getc();
Electrotiger 0:9afc272fa65f 81 }
Electrotiger 0:9afc272fa65f 82 // Reattach the interrupt.
Electrotiger 0:9afc272fa65f 83 btSerialObj.attach(this, &HC06Bluetooth::receiveByteISR);
Electrotiger 0:9afc272fa65f 84 }
Electrotiger 0:9afc272fa65f 85
Electrotiger 0:9afc272fa65f 86 HC06Bluetooth::~HC06Bluetooth() {
Electrotiger 0:9afc272fa65f 87 // TODO Auto-generated destructor stub
Electrotiger 0:9afc272fa65f 88 }
Electrotiger 0:9afc272fa65f 89
Electrotiger 0:9afc272fa65f 90 void HC06Bluetooth::print(const char* buffer) {
Electrotiger 0:9afc272fa65f 91 btSerialObj.puts(buffer);
Electrotiger 0:9afc272fa65f 92 }
Electrotiger 0:9afc272fa65f 93
Electrotiger 0:9afc272fa65f 94 void HC06Bluetooth::print(char c) {
Electrotiger 0:9afc272fa65f 95 btSerialObj.putc(c);
Electrotiger 0:9afc272fa65f 96 }
Electrotiger 0:9afc272fa65f 97
Electrotiger 0:9afc272fa65f 98 void HC06Bluetooth::receiveByteISR() {
Electrotiger 0:9afc272fa65f 99 while(btSerialObj.readable()) {
Electrotiger 0:9afc272fa65f 100 // Get the character from the input.
Electrotiger 0:9afc272fa65f 101 char receivedChar = btSerialObj.getc();
Electrotiger 0:9afc272fa65f 102
Electrotiger 0:9afc272fa65f 103 // Call the character callback function if it is not null.
Electrotiger 0:9afc272fa65f 104 if (charCallbackFunc != NULL) {
Electrotiger 0:9afc272fa65f 105 charCallbackFunc(receivedChar);
Electrotiger 0:9afc272fa65f 106 }
Electrotiger 0:9afc272fa65f 107
Electrotiger 5:e00cc0dab1c7 108 if (lineCallbackFunc != NULL) {
Electrotiger 5:e00cc0dab1c7 109 // If the character is a newline or carriage return, then call the line callback function.
Electrotiger 5:e00cc0dab1c7 110 if ((receivedChar == '\n') || (receivedChar == '\r')) {
Electrotiger 5:e00cc0dab1c7 111 // Terminate the buffer with a null character, since that is what strings end with.
Electrotiger 5:e00cc0dab1c7 112 receivedChar = '\0';
Electrotiger 5:e00cc0dab1c7 113 dataReceivedBuffer[dataReceivedBufferPos] = receivedChar;
Electrotiger 5:e00cc0dab1c7 114 // Copy data from the buffer to a copy.
Electrotiger 5:e00cc0dab1c7 115 std::copy(dataReceivedBuffer, dataReceivedBuffer + dataReceivedBufferPos, dataReceivedBufferCopy);
Electrotiger 5:e00cc0dab1c7 116 // Reset the buffer position.
Electrotiger 5:e00cc0dab1c7 117 dataReceivedBufferPos = 0;
Electrotiger 5:e00cc0dab1c7 118 // Call the callback function.
Electrotiger 5:e00cc0dab1c7 119 if (lineCallbackFunc != NULL) {
Electrotiger 5:e00cc0dab1c7 120 lineCallbackFunc((const char*)dataReceivedBuffer);
Electrotiger 5:e00cc0dab1c7 121 }
Electrotiger 0:9afc272fa65f 122 }
Electrotiger 5:e00cc0dab1c7 123 // Otherwise, just place it in the buffer and move on.
Electrotiger 5:e00cc0dab1c7 124 else {
Electrotiger 5:e00cc0dab1c7 125 if (dataReceivedBufferPos < dataBufferSize) {
Electrotiger 5:e00cc0dab1c7 126 dataReceivedBuffer[dataReceivedBufferPos] = receivedChar;
Electrotiger 5:e00cc0dab1c7 127 dataReceivedBufferPos++;
Electrotiger 5:e00cc0dab1c7 128 }
Electrotiger 5:e00cc0dab1c7 129 }
Electrotiger 0:9afc272fa65f 130 }
Electrotiger 0:9afc272fa65f 131 }
Electrotiger 0:9afc272fa65f 132 }
Electrotiger 0:9afc272fa65f 133