Tufts Hybrid Racing Sensor Node

Files at this revision

API Documentation at this revision

Comitter:
wsalis01
Date:
Thu Jan 12 20:44:36 2012 +0000
Child:
1:fbb17be9a65d
Commit message:

Changed in this revision

CANProtocol.lib Show annotated file Show diff for this revision Revisions of this file
SensorNode.cpp Show annotated file Show diff for this revision Revisions of this file
SensorNode.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CANProtocol.lib	Thu Jan 12 20:44:36 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/wsalis01/code/CANProtocol/#3f7b957d3617
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SensorNode.cpp	Thu Jan 12 20:44:36 2012 +0000
@@ -0,0 +1,98 @@
+/*
+ * File: SensorNode/SensorNode.cpp
+ * Author: William Jessup Salisbury
+ * Company: Tufts Hybrid Racing Team
+ * Copyright: CC BY-NC-SA 3.0
+ * Date: 1/12/2012
+ */
+
+#include "mbed.h"
+#include "CANProtocol.h"
+#include "SensorNode.h"
+
+SensorNode::SensorNode() : _leftWheel(LW_PIN), _rightWheel(RW_PIN), _can(CAN_RX, CAN_TX), _console(USBTX, USBRX), _leftTicks(0), _rightTicks(0), _leftRevolutions(0), _rightRevolutions(0), _syncID(0)  {
+    /* InterruptIn initialization */
+    _leftWheel.mode(PullUp);
+    _leftWheel.fall(this, &SensorNode::leftTick);
+    _rightWheel.mode(PullUp);
+    _rightWheel.fall(this, &SensorNode::rightTick);
+    
+    /* CAN initialization */
+    _can.attach(this, &SensorNode::canReceive);
+    
+    /* Ticker initialization */
+    _statusTicker.attach(this, &SensorNode::canSend, tickerTimeout);
+    
+    /* Hello, World! */
+    _console.printf("%s\r\n", "SensorNode instantiated");
+}
+
+SensorNode::~SensorNode() {
+    _statusTicker.detach();
+}
+
+void SensorNode::leftTick() {
+    if ( (++_leftTicks % ticksPerRevolution) == 0 ) {
+        _leftTicks = 0;
+        ++_leftRevolutions;
+    }
+}
+
+void SensorNode::rightTick() {
+    if ( (++_rightTicks % ticksPerRevolution) == 0 ) {
+        _rightTicks = 0;
+        ++_rightRevolutions;
+    }
+}
+
+void SensorNode::canSend() {
+    CANMessage msg;
+    msg.id = CAN_STATUS;
+    msg.len = 8;
+    msg.data[0] = 0xD;
+    msg.data[1] = 0xE;
+    msg.data[2] = 0xA;
+    msg.data[3] = 0xD;
+    msg.data[4] = 0xB;
+    msg.data[5] = 0xE;
+    msg.data[6] = 0xE;
+    msg.data[7] = 0xF;
+    if (_can.write(msg)) {
+        _console.printf("%s\r\n", "Message sent.");
+    } else {
+        _console.printf("%s\r\n", "Message send failure.");
+    }
+}
+
+void SensorNode::canReceive() {
+    CANMessage msg;
+
+    if (_can.read(msg)) {
+        _console.printf("%s\n", "Message received.");
+        switch (msg.id) {
+            case CAN_RESET:
+                Reset();
+                break;
+
+            case CAN_SYNC:
+                _syncID = msg.data[0];
+
+                break;
+
+            default:
+                _console.printf("%s\r\n", "Message decode failure.");
+                break;
+        }
+    } else {
+        _console.printf("%s\r\n", "Message recieve failure.");
+        error("%s\r\n", "func:'canReceive()' error: 'Message recieve failure.'");
+    }
+}
+
+void SensorNode::Reset() {
+    _leftTicks = 0;
+    _rightTicks = 0;
+    _leftRevolutions = 0;
+    _rightRevolutions = 0;
+    _can.reset();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SensorNode.h	Thu Jan 12 20:44:36 2012 +0000
@@ -0,0 +1,42 @@
+/*
+ * File: SensorNode/SensorNode.h
+ * Author: William Jessup Salisbury
+ * Company: Tufts Hybrid Racing Team
+ * Copyright: CC BY-NC-SA 3.0
+ * Date: 1/12/2012
+ */
+
+#ifndef SENSOR_NODE_H
+#define SENSOR_NODE_H
+
+#include "mbed.h"
+
+const PinName CAN_RX = p9;
+const PinName CAN_TX = p10;
+const PinName LW_PIN = p11;
+const PinName RW_PIN = p12;
+
+const int ticksPerRevolution = 32;
+const int tickerTimeout = 1;
+
+class SensorNode {
+public:
+    SensorNode();
+    ~SensorNode();
+private:
+    inline void Reset();
+    void leftTick();
+    void rightTick();
+    void canSend();
+    void canReceive();
+    
+    InterruptIn _leftWheel, _rightWheel;
+    CAN _can;
+    Serial _console;
+    Ticker _statusTicker;
+    volatile int _leftTicks, _rightTicks;
+    volatile int _leftRevolutions, _rightRevolutions;
+    volatile char _syncID;
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jan 12 20:44:36 2012 +0000
@@ -0,0 +1,17 @@
+/*
+ * File: SensorNode/main.cpp
+ * Author: William Jessup Salisbury
+ * Company: Tufts Hybrid Racing Team
+ * Copyright: CC BY-NC-SA 3.0
+ * Date: 1/12/2012
+ */
+
+#include "mbed.h"
+#include "SensorNode.h"
+
+SensorNode sn;
+
+int main() {
+    while (true)
+        __WFI();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Jan 12 20:44:36 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/5364839841bd