Tufts Hybrid Racing Control Node
Revision 1:edb687d65942, committed 2012-04-15
- Comitter:
- wsalis01
- Date:
- Sun Apr 15 00:13:28 2012 +0000
- Parent:
- 0:9b224b68e7c7
- Commit message:
- Initial Commit
Changed in this revision
--- a/CANProtocol.lib Thu Jan 12 20:44:16 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/wsalis01/code/CANProtocol/#f25554922a25
--- a/ControlNode.cpp Thu Jan 12 20:44:16 2012 +0000 +++ b/ControlNode.cpp Sun Apr 15 00:13:28 2012 +0000 @@ -1,101 +1,108 @@ -/* - * File: ControlNode/ControlNode.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 "ControlNode.h" - -ControlNode::ControlNode() : _can(CAN_RX, CAN_TX), _console(USBTX, USBRX), _syncID(0) { - /* CAN initialization */ - _can.attach(this, &ControlNode::canReceive); - - /* Ticker initialization */ - _syncTimer.attach_us(this, &ControlNode::canSync, tickerTimeout_us); - - /* Print startup message */ - _console.printf("%s\r\n", "ControlNode instantiated."); -} - -ControlNode::~ControlNode() { - _syncTimer.detach(); -} - -void ControlNode::canReset() { - CANMessage msg; - msg.id = CAN_RESET; - msg.len = 0; - if (_can.write(msg)) { - _console.printf("%s\r\n", "Reset message sent."); - } else { - _console.printf("%s\r\n", "Reset message send failure."); - } -} - -void ControlNode::canSync() { - CANMessage msg(CAN_SYNC); - msg.len = 1; - msg.data[0] = (++_syncID % 0xFF); - if (_can.write(msg)) { - _console.printf("%s\r\n", "Sync message sent."); - } else { - _console.printf("%s\r\n", "Sync message send failure."); - } -} - -void ControlNode::canSend() { - CANMessage msg; - - - if (_can.write(msg)) { - _console.printf("%s\r\n", "Message sent."); - } else { - _console.printf("%s\r\n", "Message send failure."); - } -} - -void ControlNode::canReceive() { - CANMessage msg; - - if (_can.read(msg)) { - - _console.printf("%s\r\n", "Message received."); - - if (msg.data[0] != _syncID) { - _console.printf("%s\r\n", "'_syncID' mismatch."); - return; - } - - switch (msg.id) { - case CAN_BRAKE: - break; - - case CAN_ACCEL: - break; - - case CAN_FLWS: - break; - - case CAN_FRWS: - break; - - case CAN_RLWS: - break; - - case CAN_RRWS: - break; - - case CAN_STATUS: - break; - - default: - break; - } - } else { - _console.printf("%s\r\n", "Message recieve failure."); - } -} +/* + * File: ControlNode/ControlNode.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 "ControlNode.h" + +ControlNode::ControlNode() : _can(CAN_RX, CAN_TX), _console(USBTX, USBRX), _syncID(0) { + //Nothing to see here +} + +void ControlNode::Init() { + /* CAN initialization */ + _can.frequency(CAN_FREQUENCY); + _can.attach(this, &ControlNode::canReceive); + + /* Ticker initialization */ + _syncTimer.attach_us(this, &ControlNode::canSync, tickerTimeout_us); + + /* Print startup message */ + _console.printf("%s\r\n", "ControlNode instantiated."); +} + +ControlNode::~ControlNode() { + _syncTimer.detach(); +} + +void ControlNode::canReset() { + CANMessage msg; + msg.id = CAN_RESET; + msg.len = 0; + if (_can.write(msg)) { + _console.printf("%s\r\n", "Reset message sent."); + } else { + _console.printf("%s\r\n", "Reset message send failure."); + } +} + +void ControlNode::canSync() { + CANMessage msg; + msg.id = CAN_SYNC; + msg.len = 1; + msg.data[0] = (++_syncID % 0xFF); + if (_can.write(msg)) { + _console.printf("%s\r\n", "Sync message sent."); + } else { + _console.printf("%s\r\n", "Sync message send failure."); + } +} + +void ControlNode::canSend() { + CANMessage msg; + + if (_can.write(msg)) { + _console.printf("%s\r\n", "Message sent."); + } else { + _console.printf("%s\r\n", "Message send failure."); + } +} + +void ControlNode::canReceive() { + CANMessage msg; + + if (_can.read(msg)) { + + _console.printf("%s\r\n", "Message received."); + + if (msg.data[0] != _syncID) { + _console.printf("%s\r\n", "'_syncID' mismatch."); + return; + } + + switch (msg.id) { + case CAN_BRAKE: + break; + + case CAN_ACCEL: + break; + + case CAN_FLWS: + break; + + case CAN_FRWS: + break; + + case CAN_RLWS: + break; + + case CAN_RRWS: + break; + + case CAN_STATUS: + _console.printf("%s\r\n", "CAN_STATUS Message received."); + break; + + default: + _console.printf("%s\r\n", "Message decode failure."); + break; + } + } else { + _console.printf("%s\r\n", "Message recieve failure."); + } +}
--- a/ControlNode.h Thu Jan 12 20:44:16 2012 +0000 +++ b/ControlNode.h Sun Apr 15 00:13:28 2012 +0000 @@ -8,23 +8,25 @@ #ifndef CONTROL_NODE_H #define CONTROL_NODE_H + #include "mbed.h" -const PinName CAN_RX = p9; -const PinName CAN_TX = p10; +const PinName CAN_RX = p30; +const PinName CAN_TX = p29; -const int tickerTimeout_us = 1000; +const int tickerTimeout_us = 1000000; class ControlNode { public: ControlNode(); ~ControlNode(); + void Init(); private: void canReset(); void canSync(); void canSend(); void canReceive(); - + CAN _can; Serial _console; Ticker _syncTimer;
--- a/main.cpp Thu Jan 12 20:44:16 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,17 +0,0 @@ -/* - * File : ControlNode/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 "ControlNode.h" - -ControlNode cn; - -int main() { - while (true) - __WFI(); -}
--- a/mbed.bld Thu Jan 12 20:44:16 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/5364839841bd