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:
Tue Jun 28 01:24:36 2016 +0000
Revision:
5:e00cc0dab1c7
Parent:
1:918a505314ea
Added checks to ensure that buffer overflow will not occur in the HC-06 Bluetooth module when receiving data.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Electrotiger 0:9afc272fa65f 1 /**
Electrotiger 0:9afc272fa65f 2 * @file HC06Bluetooth.hpp
Electrotiger 0:9afc272fa65f 3 * @date June 4th, 2016
Electrotiger 0:9afc272fa65f 4 * @author Weimen Li
Electrotiger 0:9afc272fa65f 5 * @class HC06Bluetooth
Electrotiger 0:9afc272fa65f 6 * @brief This class creates an object representing the HC06 Bluetooth Module
Electrotiger 0:9afc272fa65f 7 * The baud rate for the device is configured to be 115200.
Electrotiger 0:9afc272fa65f 8 * @code
Electrotiger 0:9afc272fa65f 9 // First, make sure to have included the hpp file:
Electrotiger 0:9afc272fa65f 10 #include HC06Bluetooth.hpp
Electrotiger 0:9afc272fa65f 11 // In the scope that you need it, create a new instance of the HC06Bluetooth object by calling:
Electrotiger 0:9afc272fa65f 12 HC06Bluetooth HC06BluetoothObj(TXPin, RXPin, "My Cool Robot", someCallBackFunction);
Electrotiger 0:9afc272fa65f 13 // The "someCallBackFunction" is a function you write that is called when a new line has been read.
Electrotiger 0:9afc272fa65f 14 // For instance, one simple callback is to simply echo (reprint to output) the received line:
Electrotiger 0:9afc272fa65f 15
Electrotiger 0:9afc272fa65f 16 void BTEchoCallback(const char* receivedLine) {
Electrotiger 0:9afc272fa65f 17 HC06BluetoothObj.print(receivedLine);
Electrotiger 0:9afc272fa65f 18 }
Electrotiger 0:9afc272fa65f 19
Electrotiger 0:9afc272fa65f 20 // A more sophisticated callback would be one that understands the received line contains commands,
Electrotiger 0:9afc272fa65f 21 // and parses them:
Electrotiger 0:9afc272fa65f 22 // Callback function to read a command from input
Electrotiger 0:9afc272fa65f 23 // Include the <sstream> library, since that is what we use to process information.
Electrotiger 0:9afc272fa65f 24 #include <sstream>
Electrotiger 0:9afc272fa65f 25 #include <string>
Electrotiger 0:9afc272fa65f 26 // Variables to read information into.
Electrotiger 0:9afc272fa65f 27 volatile float kConst;
Electrotiger 0:9afc272fa65f 28 volatile float iConst;
Electrotiger 0:9afc272fa65f 29 volatile float dConst;
Electrotiger 0:9afc272fa65f 30 void BTCommandCallback(const char* receivedString) {
Electrotiger 0:9afc272fa65f 31 stringstream stringStream(receivedString);
Electrotiger 0:9afc272fa65f 32 // The received string has the form "newDeviceName kConst iConst dConst"
Electrotiger 0:9afc272fa65f 33 // Necessary to use these temporary variables since stringStream cannot read into
Electrotiger 0:9afc272fa65f 34 // "volatile float", but it can read into "float".
Electrotiger 0:9afc272fa65f 35 float kConstTemp;
Electrotiger 0:9afc272fa65f 36 float iConstTemp;
Electrotiger 0:9afc272fa65f 37 float dConstTemp;
Electrotiger 0:9afc272fa65f 38 stringStream >> kConstTemp;
Electrotiger 0:9afc272fa65f 39 stringStream >> iConstTemp;
Electrotiger 0:9afc272fa65f 40 stringStream >> dConstTemp;
Electrotiger 0:9afc272fa65f 41 kConst = kConstTemp;
Electrotiger 0:9afc272fa65f 42 iConst = iConstTemp;
Electrotiger 0:9afc272fa65f 43 dConst = dConstTemp;
Electrotiger 0:9afc272fa65f 44 // Echo the received commands:
Electrotiger 0:9afc272fa65f 45 char output[256];
Electrotiger 0:9afc272fa65f 46 sprintf(output, "Received Command: kConst = %f, iConst = %f, dConst = %f", kConst, iConst, dConst);
Electrotiger 0:9afc272fa65f 47 bluetoothObj.print(output);
Electrotiger 0:9afc272fa65f 48 }
Electrotiger 0:9afc272fa65f 49
Electrotiger 0:9afc272fa65f 50 }
Electrotiger 0:9afc272fa65f 51 * @endcode
Electrotiger 0:9afc272fa65f 52 */
Electrotiger 0:9afc272fa65f 53
Electrotiger 0:9afc272fa65f 54 //TODO: Complete this example.
Electrotiger 0:9afc272fa65f 55
Electrotiger 0:9afc272fa65f 56
Electrotiger 0:9afc272fa65f 57 #ifndef HC06BLUETOOTH_H_
Electrotiger 0:9afc272fa65f 58 #define HC06BLUETOOTH_H_
Electrotiger 0:9afc272fa65f 59 #include "mbed.h"
Electrotiger 0:9afc272fa65f 60 #include <string>
Electrotiger 0:9afc272fa65f 61
Electrotiger 5:e00cc0dab1c7 62 const int dataBufferSize = 256;
Electrotiger 5:e00cc0dab1c7 63
Electrotiger 0:9afc272fa65f 64 class HC06Bluetooth {
Electrotiger 0:9afc272fa65f 65 public: // Public methods.
Electrotiger 0:9afc272fa65f 66 /**
Electrotiger 0:9afc272fa65f 67 * @brief Constructor for the HC06_Bluetooth class.
Electrotiger 0:9afc272fa65f 68 * @param TX The pin that the TX line is attached to.
Electrotiger 0:9afc272fa65f 69 * @param RX The pin that the RX line is attached to.
Electrotiger 0:9afc272fa65f 70 * @param deviceName The name that you want your system to be identified as when you connect to it i.e. "Weimen's MAE433Robot"
Electrotiger 0:9afc272fa65f 71 * @param password A 4-digit numeric PIN that you want your device to connect with. It defaults to "1234".
Electrotiger 0:9afc272fa65f 72 * @param lineCallbackFunc The callback function that will be called once a newline character is encountered on the receiving data.
Electrotiger 0:9afc272fa65f 73 * The callback function takes as an argument a string containing the line that has been read.
Electrotiger 0:9afc272fa65f 74 * @remark The callback function is run within within an interrupt service routine, so it should be written to be safe for ISRs.
Electrotiger 0:9afc272fa65f 75 * @param charCallbackFunc A function that will be called once a new character has been read. It should return void and take as an argument
Electrotiger 0:9afc272fa65f 76 * the character that has been read.
Electrotiger 0:9afc272fa65f 77 */
Weimen Li 1:918a505314ea 78 HC06Bluetooth(PinName TX = PTD3, PinName RX = PTD2, void (*lineCallbackFunc) (const char* readString) = NULL, void (*charCallbackFunc) (char readChar) = NULL);
Electrotiger 0:9afc272fa65f 79 virtual ~HC06Bluetooth();
Electrotiger 0:9afc272fa65f 80 /**
Electrotiger 0:9afc272fa65f 81 * @brief Run the setup routine to configure the device name and the device pin.
Electrotiger 0:9afc272fa65f 82 * @remark: You only need to run this once during the entire time you have the module, since the device name and PIN are saved
Electrotiger 0:9afc272fa65f 83 * by the module itself! However, you may run the function again if you would like to change the deviceName or PIN.
Electrotiger 0:9afc272fa65f 84 * @param deviceName The name of the device, as a string. It must only contain alphanumeric characters - no spaces allowed.
Electrotiger 0:9afc272fa65f 85 * @param PIN The device PIN.
Electrotiger 0:9afc272fa65f 86 */
Electrotiger 0:9afc272fa65f 87 void runSetup(std::string deviceName, std::string PIN = "1234");
Electrotiger 0:9afc272fa65f 88 /**
Electrotiger 0:9afc272fa65f 89 * @brief Print information in buffer to the output.
Electrotiger 0:9afc272fa65f 90 * @param buffer A null-terminated buffer containing the data you want to send.
Electrotiger 0:9afc272fa65f 91 */
Electrotiger 0:9afc272fa65f 92 void print(const char *buffer);
Electrotiger 0:9afc272fa65f 93 /**
Electrotiger 0:9afc272fa65f 94 * @brief Print a character to output.
Electrotiger 0:9afc272fa65f 95 * @param char The character you want to print to output.
Electrotiger 0:9afc272fa65f 96 */
Electrotiger 0:9afc272fa65f 97 void print(char c);
Electrotiger 0:9afc272fa65f 98
Electrotiger 0:9afc272fa65f 99 private:
Electrotiger 0:9afc272fa65f 100 RawSerial btSerialObj;
Electrotiger 0:9afc272fa65f 101 void receiveByteISR();
Electrotiger 0:9afc272fa65f 102 /// Pointer to a callback function the client provides when a line is received.
Electrotiger 0:9afc272fa65f 103 void (*lineCallbackFunc) (const char*);
Electrotiger 0:9afc272fa65f 104 /// Pointer to a callback function the client provides when a character is received.
Electrotiger 0:9afc272fa65f 105 void (*charCallbackFunc) (char);
Electrotiger 5:e00cc0dab1c7 106 char dataReceivedBuffer[dataBufferSize];
Electrotiger 0:9afc272fa65f 107 int32_t dataReceivedBufferPos;
Electrotiger 5:e00cc0dab1c7 108 char dataReceivedBufferCopy[dataBufferSize];
Electrotiger 0:9afc272fa65f 109 };
Electrotiger 0:9afc272fa65f 110
Electrotiger 0:9afc272fa65f 111 #endif /* HC06BLUETOOTH_H_ */