Will Salisbury / SensorNode
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SensorNode.cpp Source File

SensorNode.cpp

00001 /*
00002  * File: SensorNode/SensorNode.cpp
00003  * Author: William Jessup Salisbury
00004  * Company: Tufts Hybrid Racing Team
00005  * Copyright: CC BY-NC-SA 3.0
00006  * Date: 1/12/2012
00007  */
00008 
00009 #include "mbed.h"
00010 #include "CANProtocol.h"
00011 #include "SensorNode.h"
00012 
00013 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)  {
00014     /* InterruptIn initialization */
00015     _leftWheel.mode(PullUp);
00016     _leftWheel.fall(this, &SensorNode::leftTick);
00017     _rightWheel.mode(PullUp);
00018     _rightWheel.fall(this, &SensorNode::rightTick);
00019 
00020     /* CAN initialization */
00021     _can.frequency(CAN_FREQUENCY);
00022     _can.attach(this, &SensorNode::canReceive);
00023 
00024     /* Ticker initialization */
00025     //_statusTicker.attach(this, &SensorNode::canSend, tickerTimeout);
00026 
00027     /* Hello, World! */
00028     _console.printf("%s\r\n", "SensorNode instantiated");
00029 }
00030 
00031 SensorNode::~SensorNode() {
00032     _statusTicker.detach();
00033 }
00034 
00035 void SensorNode::leftTick() {
00036     if ( (++_leftTicks % ticksPerRevolution) == 0 ) {
00037         _leftTicks = 0;
00038         ++_leftRevolutions;
00039     }
00040 }
00041 
00042 void SensorNode::rightTick() {
00043     if ( (++_rightTicks % ticksPerRevolution) == 0 ) {
00044         _rightTicks = 0;
00045         ++_rightRevolutions;
00046     }
00047 }
00048 
00049 void SensorNode::canSend() {
00050     CANMessage msg;
00051     msg.id = CAN_STATUS;
00052     msg.len = 8;
00053     msg.data[0] = _syncID;
00054     msg.data[1] = 0xE;
00055     msg.data[2] = 0xA;
00056     msg.data[3] = 0xD;
00057     msg.data[4] = 0xB;
00058     msg.data[5] = 0xE;
00059     msg.data[6] = 0xE;
00060     msg.data[7] = 0xF;
00061     if (_can.write(msg)) {
00062         _console.printf("%s\r\n", "Message sent.");
00063     } else {
00064         _console.printf("%s\r\n", "Message send failure.");
00065     }
00066 }
00067 
00068 void SensorNode::canReceive() {
00069     CANMessage msg;
00070 
00071     if (_can.read(msg)) {
00072         switch (msg.id) {
00073             case CAN_RESET:
00074                 _console.printf("%s\r\n", "CAN_RESET Message received.");
00075                 Reset();
00076                 break;
00077 
00078             case CAN_SYNC:
00079                 _console.printf("%s\r\n", "CAN_SYNC Message received.");
00080                 _syncID = msg.data[0];
00081                 canSend();
00082                 break;
00083 
00084             default:
00085                 _console.printf("%s\r\n", "Message decode failure.");
00086                 break;
00087         }
00088     } else {
00089         _console.printf("%s\r\n", "Message recieve failure.");
00090     }
00091 }
00092 
00093 void SensorNode::Reset() {
00094     _leftTicks = 0;
00095     _rightTicks = 0;
00096     _leftRevolutions = 0;
00097     _rightRevolutions = 0;
00098     _can.reset();
00099 }