Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of racing_robots by
Revision 8:597ce8a7d34b, committed 2015-06-01
- Comitter:
- sillevl
- Date:
- Mon Jun 01 14:47:32 2015 +0000
- Parent:
- 7:a72215b1910b
- Child:
- 9:0385d1bfc38b
- Commit message:
- Added support for start/stop via Xbee
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MbedJSONValue.lib Mon Jun 01 14:47:32 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/samux/code/MbedJSONValue/#10a99cdf7846
--- a/mbed.bld Wed Feb 25 15:56:14 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/9ad691361fac \ No newline at end of file
--- a/racing_robots.cpp Wed Feb 25 15:56:14 2015 +0000
+++ b/racing_robots.cpp Mon Jun 01 14:47:32 2015 +0000
@@ -10,8 +10,12 @@
*/
void _init(void) {
// DO our init here
-
init(); // Students init
+#ifdef XBBE
+ if(!xbee.hasCode()){
+ error("no unique code is set when using Xbee for start/stop");
+ }
+#endif
}
/*
--- a/robot_logic.cpp Wed Feb 25 15:56:14 2015 +0000
+++ b/robot_logic.cpp Mon Jun 01 14:47:32 2015 +0000
@@ -1,4 +1,5 @@
#include "robot_logic.h"
+#include "xbee.h"
// Some defines
#define MAX_SPEED 100
@@ -10,6 +11,10 @@
// Static scope variables
static m3pi m3pi;
+#ifdef XBEE
+Xbee xbee(p28, p27);
+#endif
+
// Static scope variables for keeping internal state
static int internal_speed = 0; // [-100, +100]
static int internal_turnspeed = 0; // [-100, +100]
@@ -28,7 +33,7 @@
internal_speed = speed;
- if (speed == 0) {
+ if (speed == 0 || !canDrive()) {
m3pi.stop();
} else if (speed > 0) {
m3pi.forward(MAX_REAL_SPEED*speed/MAX_SPEED);
@@ -60,6 +65,11 @@
left = MIN;
else if (left > MAX_SPEED)
left = MAX_SPEED;
+
+ if(!canDrive()){
+ left = right = 0;
+ }
+
m3pi.left_motor(MAX_REAL_SPEED*left/MAX_SPEED);
m3pi.right_motor(MAX_REAL_SPEED*right/MAX_SPEED);
}
@@ -160,4 +170,20 @@
error("Illegal LED state");
}
m3pi.leds(internal_led_state);
+}
+
+int canDrive()
+{
+#ifdef XBEE
+ return !xbee.stopped();
+#endif
+ return true;
+}
+
+
+void setCode(int code)
+{
+#ifdef XBEE
+ xbee.setCode(code);
+#endif
}
\ No newline at end of file
--- a/robot_logic.h Wed Feb 25 15:56:14 2015 +0000 +++ b/robot_logic.h Mon Jun 01 14:47:32 2015 +0000 @@ -109,4 +109,18 @@ */ void await(int milliseconds); +/** + * If using Xbee to start/stop the robot, you can set a unique code to prevent starting or stopping other robots + * + * @param code A number between 0 and 1000. + */ +void setCode(int code); + +/** + * Use this method if you need to know if something (eg: Xbee) is preventing the robot from driving + * + * @return Boolean true if the robot can drive, false if the robot cannot drive + */ +int canDrive(); + #endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/xbee.cpp Mon Jun 01 14:47:32 2015 +0000
@@ -0,0 +1,77 @@
+#include "xbee.h"
+#include "MbedJSONValue.h"
+
+Xbee::Xbee(PinName tx, PinName rx){
+ xbee = new Serial(tx, rx);
+ xbee->baud(115200);
+ xbee->attach(this, &Xbee::received, Serial::RxIrq);
+ run = false;
+ printf("xbee constructor\r\n");
+ rst = new DigitalOut(p26);
+ reset();
+ setCode(-1);
+}
+
+void Xbee::reset(){
+ rst->write(0);
+ wait_ms(1);
+ rst->write(1);
+ wait_ms(1);
+}
+
+void Xbee::setCode(int code){
+ if( code >= 0 && code < 1000){
+ error("Code must be between 0 and 999.");
+ }
+ this->code = code;
+}
+
+int Xbee::hasCode(){
+ return code != -1;
+}
+
+int Xbee::running(){
+ return run;
+}
+
+int Xbee::stopped(){
+ return !running();
+}
+
+void Xbee::received(){
+ char c;
+ while(xbee->readable()){
+ c = xbee->getc();
+ putc(c, stdout);
+ buffer[buffer_pos] = c;
+ buffer_pos++;
+ if(c == '\n'){
+ buffer[buffer_pos] = '\0';
+
+ printf("buffer: %s\r\n", buffer);
+
+ buffer_pos = 0;
+ MbedJSONValue json;
+ parse(json, buffer);
+
+ int code = -1;
+ if(json.hasMember("start")){
+ code = json["start"].get<int>();
+ } else if(json.hasMember("stop")){
+ code = json["stop"].get<int>();
+ } else {
+ break;
+ }
+
+ printf("code: %d\r\n", code);
+
+ if(json.hasMember("start") && code == this->code){
+ run = true;
+ printf("start\r\n");
+ } else if(code == this->code){
+ run = false;
+ printf("stop\r\n");
+ }
+ }
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/xbee.h Mon Jun 01 14:47:32 2015 +0000
@@ -0,0 +1,32 @@
+#ifndef XBEE_H
+#define XBEE_H
+
+#include "mbed.h"
+
+class Xbee{
+
+ public:
+ Xbee(PinName tx, PinName rx);
+ int running();
+ int stopped();
+ void setCode(int code);
+ int hasCode();
+
+ protected:
+ Serial* xbee;
+ void received();
+
+ private:
+ int run;
+ int code;
+
+ int buffer_pos;
+ char buffer[256];
+
+ void reset();
+ DigitalOut* rst;
+
+};
+
+
+#endif
\ No newline at end of file
