This repository was archived by the owner on Nov 19, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Code Template
Steven Rhodes edited this page Mar 13, 2015
·
1 revision
/* CalSol - UC Berkeley Solar Vehicle Team
* [NAME OF FILE] - [INTENDED MODULE]
* Author: [YOUR NAME]
* Date: [TODAY'S DATE]
*/
/* Define CAN message IDs */
// TODO: Define CAN IDs
/* Declare your state variables here */
// TODO: Declare state variables
long last_time = 0;
void process_packet(CanMessage &msg) {
switch(msg.id) {
/* Add cases for each CAN message ID to be received*/
// Todo: Add cases for receiving CAN IDs
default:
break;
}
}
void setup() {
/* Can Initialization w/o filters */
Can.attach(&process_packet);
Can.begin(1000);
CanBufferInit();
}
void loop() {
if (millis() - last_time > 100) {
/* Perform actions affected by external state here */
// Todo: Do something with state variables
/* Collect data and send outbound CAN packets here */
// Todo: Sending CAN packets
}
}
Example
/* CalSol - UC Berkeley Solar Vehicle Team
* MainInputTest.pde - Main Input Board
* Author: Foo Bar
* Date: Dec. 21st 2012
*/
/* Define CAN message IDs */
#define CAN_MAININPUT_EMERGENCY 0x024
#define CAN_MAININPUT_HEARTBEAT 0x044
#define CAN_TRITIUM_MOTOR_DRIVE 0x501
#define CAN_TRITIUM_POWER 0x502
#define CAN_TRITIUM_RESET 0x503
#define CAN_TRITIUM_VELOCITY 0x403
/* Pin definitions here */
#define ANALOGIN_ACCEL 2
#define ANALOGIN_BRAKE 3
#define DIGITALOUT_ERROR 15
/* Declare your state variables here */
float measured_speed;
long last_received = 0;
long last_time = 0;
typedef union {
char c[8];
float f[2];
} two_floats;
void process_packet(CanMessage &msg) {
switch(msg.id) {
/* Add cases for each CAN message ID to be received*/
case CAN_TRITIUM_VELOCITY:
// Vehicle velocity in meters/sec
measured_speed = ((two_floats*)msg.data)->f[1];
last_received = millis();
break;
default:
break;
}
}
void setup() {
/* Can Initialization w/o filters */
Can.attach(&process_packet);
Can.begin(1000);
CanBufferInit();
/* Send startup packets */
Can.send(CanMessage(CAN_TRITIUM_RESET));
two_floats data;
data.f[1] = 1.0;
Can.send(CanMessage(CAN_TRITIUM_POWER, data.c));
Serial.begin(115200);
}
void loop() {
if (millis() - last_time > 100) {
/* Perform actions affected by external state here */
if (millis() - last_received > 500) {
Serial.print("Error: lost signal from Tritium ");
Serial.print(millis() - last_received);
Serial.println(" ms ago");
} else {
Serial.print("Current speed: ");
Serial.println(measured_speed);
}
/* Collect data and send outbound CAN packets here */
Can.send(CanMessage(CAN_HEARTBEAT_MAININPUT));
two_floats data;
data.f[0] = 100.0;
data.f[1] = (float)analogRead(ANALOGIN_ACCEL) / 1023.0;
Can.send(CanMessage(CAN_TRITIUM_MOTOR_DRIVE, data.c));
}
}