Tufts Hybrid Racing Control Node

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ControlNode.cpp Source File

ControlNode.cpp

00001 /*
00002  * File: ControlNode/ControlNode.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 "ControlNode.h"
00012 
00013 ControlNode::ControlNode() : _can(CAN_RX, CAN_TX), _console(USBTX, USBRX), _syncID(0) {
00014     //Nothing to see here
00015 }
00016 
00017 void ControlNode::Init() {
00018     /* CAN initialization */
00019     _can.frequency(CAN_FREQUENCY);
00020     _can.attach(this, &ControlNode::canReceive);
00021 
00022     /* Ticker initialization */
00023     _syncTimer.attach_us(this, &ControlNode::canSync, tickerTimeout_us);
00024 
00025     /* Print startup message */
00026     _console.printf("%s\r\n", "ControlNode instantiated.");
00027 }
00028 
00029 ControlNode::~ControlNode() {
00030     _syncTimer.detach();
00031 }
00032 
00033 void ControlNode::canReset() {
00034     CANMessage msg;
00035     msg.id = CAN_RESET;
00036     msg.len = 0;
00037     if (_can.write(msg)) {
00038         _console.printf("%s\r\n", "Reset message sent.");
00039     } else {
00040         _console.printf("%s\r\n", "Reset message send failure.");
00041     }
00042 }
00043 
00044 void ControlNode::canSync() {
00045     CANMessage msg;
00046     msg.id = CAN_SYNC;
00047     msg.len = 1;
00048     msg.data[0] = (++_syncID % 0xFF);
00049     if (_can.write(msg)) {
00050         _console.printf("%s\r\n", "Sync message sent.");
00051     } else {
00052         _console.printf("%s\r\n", "Sync message send failure.");
00053     }
00054 }
00055 
00056 void ControlNode::canSend() {
00057     CANMessage msg;
00058 
00059     if (_can.write(msg)) {
00060         _console.printf("%s\r\n", "Message sent.");
00061     } else {
00062         _console.printf("%s\r\n", "Message send failure.");
00063     }
00064 }
00065 
00066 void ControlNode::canReceive() {
00067     CANMessage msg;
00068 
00069     if (_can.read(msg)) {
00070 
00071         _console.printf("%s\r\n", "Message received.");
00072 
00073         if (msg.data[0] != _syncID) {
00074             _console.printf("%s\r\n", "'_syncID' mismatch.");
00075             return;
00076         }
00077 
00078         switch (msg.id) {
00079             case CAN_BRAKE:
00080                 break;
00081 
00082             case CAN_ACCEL:
00083                 break;
00084 
00085             case CAN_FLWS:
00086                 break;
00087 
00088             case CAN_FRWS:
00089                 break;
00090 
00091             case CAN_RLWS:
00092                 break;
00093 
00094             case CAN_RRWS:
00095                 break;
00096 
00097             case CAN_STATUS:
00098                 _console.printf("%s\r\n", "CAN_STATUS Message received.");
00099                 break;
00100 
00101             default:
00102                 _console.printf("%s\r\n", "Message decode failure.");
00103                 break;
00104         }
00105     } else {
00106         _console.printf("%s\r\n", "Message recieve failure.");
00107     }
00108 }