Skip to content

Commit

Permalink
competition code
Browse files Browse the repository at this point in the history
  • Loading branch information
cbott committed May 5, 2017
1 parent a73150e commit aafe5f1
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 37 deletions.
48 changes: 38 additions & 10 deletions CarTransmitter/CarTransmitter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#define RF95_FREQ 915.0

const int SIZE = 13;
const int SIZE = 15;
const char TERM = 255;

RH_RF95 rf95(RFM95_CS, RFM95_INT);
Expand Down Expand Up @@ -46,18 +46,9 @@ void setup() {

uint8_t transmit_data[SIZE];
void loop() {
/*
while(Serial1.available() <= SIZE){}
byte serial_buf[SIZE+1];
byte chars_read = Serial1.readBytesUntil(TERM, serial_buf, SIZE+1);
*/
byte chars_read = 13;
byte serial_buf[13];

for(int i=0; i<13; ++i){
serial_buf[i] = random(100);
}
delay(1000);
if(chars_read == SIZE){
// Successful read. Copy temporary buffer into data array
//Serial.print("\nReceived:");
Expand All @@ -74,3 +65,40 @@ void loop() {
}
}

//////////////////////////////////////////////////////////////////////
//-------------------------- Testing Loop --------------------------//
//////////////////////////////////////////////////////////////////////
// Send Fake Data
//unsigned char val0to100 = 0;
//unsigned char val0to255 = 0;
//bool valbool = 0;
//void loop() {
// uint8_t serial_buf[] = {val0to255, 0, 0, val0to100, val0to100,
// val0to100, val0to255, val0to255, val0to255,
// val0to255, val0to100, valbool, val0to100,
// valbool, val0to100};
//
// if(1){
// // Successful read. Copy temporary buffer into data array
// //Serial.print("\nReceived:");
// for(int i=0; i<SIZE; ++i){
// transmit_data[i] = serial_buf[i];
// //Serial.print(transmit_data[i], DEC);
// //Serial.print(" ");
// }
//
// // Send the packet over radio
// rf95.send(transmit_data, SIZE);
// //delay(10);
// rf95.waitPacketSent();
//
// val0to255++;
// val0to100++;
// if(val0to100 > 100){
// val0to100 = 0;
// }
// valbool = !valbool;
// delay(1000);
// }
//}

49 changes: 49 additions & 0 deletions Receiver/Receiver.ino
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,52 @@ void loop()
}
}
}



//////////////////////////////////////////////////////////////////////
//----------------------- Demonstration Loop -----------------------//
//////////////////////////////////////////////////////////////////////
// Send realistic data to demonstrate telemetry capabilities
//byte engine_t = 150;
//byte l_bat_t = 0;
//byte r_bat_t = 0;
//float l_bat_soc = 100;
//float r_bat_soc = 100;
//byte rpm = 20;
//byte l_motor_t = 80;
//byte r_motor_t = 80;
//byte l_mc_t = 50;
//byte r_mc_t = 50;
//byte accel = 0;
//byte accel_err = 0;
//byte brake = 25;
//byte brake_err = 0;
//float lv_soc = 127;
//
//void loop() {
// uint8_t serial_buf[] = {engine_t, l_bat_t, r_bat_t, l_bat_soc, r_bat_soc,
// rpm, l_motor_t, r_motor_t, l_mc_t, r_mc_t,
// accel, accel_err, brake, brake_err, lv_soc};
//
// // Send data over serial
// for(int i=0; i<15; ++i){
// Serial.print(serial_buf[i]);
// Serial.print(" ");
// }
// Serial.print("\n");
//
// delay(500);
//
// // Update the values realistically
// engine_t += random(-1,2);
// l_bat_soc -= 0.1;
// r_bat_soc -= 0.1;
// rpm += random(-4,5);
// l_motor_t += random(-2,3);
// r_motor_t += random(-2,3);
// l_mc_t += random(-2,3);
// r_mc_t += random(-2,3);
// lv_soc -= 0.2;
//}

68 changes: 43 additions & 25 deletions grapharduino.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
#grapharduino.py
from arduinoserial import Arduino
from liveplots import *
import sys
from time import strftime

SIZE = 13 #Expected packet length

SERIAL_PORT = 'COM8'
LOG_FILE = "TelemetryData.txt"

def basic_graph(arduino, log):
SIZE = 1 #Expected packet length
liveplot_init(2,1, "Basic Telemetry Data")

speedometer = Dial(row=0, col=0, title="Car Speed (MPH)", ymin=0, ymax=80, ticks=9)
speedometer = Dial(row=0, col=0, title="Car Speed (MPH)", ymin=0, ymax=90, ticks=10)
readout = Text(row=1, col=0, title="")
topspeed = -100

Expand All @@ -21,13 +18,13 @@ def basic_graph(arduino, log):
for val in arduino.read():
try:
vals = val.split()
data = map(int, vals)
data = map(float, vals)
print(data)
except (AttributeError, ValueError):
print "Received unexpected data [",val,"]"
if len(data) == SIZE:
log.write(strftime("[%H:%M:%S] ") + str(data) + "\n")
mph = data[0]/2.0
mph = data[0]
speedometer.update(mph)
topspeed = max(topspeed, mph)
readout.update("Current Speed: "+str(mph)+" MPH\nTop Speed: "+str(topspeed)+" MPH")
Expand All @@ -47,26 +44,33 @@ def car_monitor(arduino, log):
#8# [Left Motor Controller Temp] 0-255
#9# [Right Motor Controller Temp] 0-255
#10# [Accel Pedal Percentage] 0-100
#11# [Brake Pedal Percentage] 0-100
#12# [Accel Pedal Error, Brake Pedal Error] boolean
liveplot_init(3,4, "MHR17 Telemetry System")
engine_t = ScrollingLinePlot(row=0, col=0, title="Engine Temp.", ymin=0, ymax=255, width=25)
l_bat_t = ScrollingLinePlot(row=1, col=0, title="Left Battery Temp.", ymin=0, ymax=255, width=25)
r_bat_t = ScrollingLinePlot(row=2, col=0, title="Right Battery Temp.", ymin=0, ymax=255, width=25)
#11# [Accel Pedal Error] boolean
#12# [Brake Pedal Percentage] 0-100
#13# [Brake Pedal Error] boolean
#14# [Low Voltage Battery SOC] 0-120 /10
SIZE = 15
liveplot_init(3,5, "MHR17 Telemetry System")
engine_t = ScrollingLinePlot(row=0, col=3, title="Engine Temp.", ymin=0, ymax=255, width=25, ylabel="deg F")
l_bat_t = ScrollingLinePlot(row=1, col=0, title="Left Battery Temp.", ymin=0, ymax=255, width=25, ylabel="deg F")
r_bat_t = ScrollingLinePlot(row=2, col=0, title="Right Battery Temp.", ymin=0, ymax=255, width=25, ylabel="deg F")

l_bat_soc = BarChart(row=0, col=1, title="Engine Temp.", ymin=0, ymax=100, ylabel="%")
r_bat_soc = BarChart(row=0, col=2, title="Engine Temp.", ymin=0, ymax=100, ylabel="%")
l_bat_soc = BarChart(row=0, col=0, title="Left Accumulator SOC", ymin=0, ymax=100, ylabel="%")
r_bat_soc = BarChart(row=0, col=1, title="Right Accumulator SOC", ymin=0, ymax=100, ylabel="%")
lv_soc = BarChart(row=0, col=2, title="LV Battery Voltage", ymin=0, ymax=13, ylabel="V")

rpm = Dial(row=0, col=3, title="Engine RPM (x100)", ymin=0, ymax=150, ticks=8)
rpm = Dial(row=0, col=4, title="Engine RPM (x1000)", ymin=0, ymax=15, ticks=16)

l_motor_t = ScrollingLinePlot(row=1, col=1, title="Left In-Hub Temp.", ymin=0, ymax=255, width=25)
r_motor_t = ScrollingLinePlot(row=2, col=1, title="Right In-Hub Temp.", ymin=0, ymax=255, width=25)
l_motor_t = ScrollingLinePlot(row=1, col=1, title="Left In-Hub Temp.", ymin=0, ymax=255, width=25, ylabel="deg F")
r_motor_t = ScrollingLinePlot(row=2, col=1, title="Right In-Hub Temp.", ymin=0, ymax=255, width=25, ylabel="deg F")

l_mc_t = ScrollingLinePlot(row=1, col=2, title="Left Motor Controller Temp.", ymin=0, ymax=255, width=25)
r_mc_t = ScrollingLinePlot(row=2, col=2, title="Right Motor Controller Temp.", ymin=0, ymax=255, width=25)
l_mc_t = ScrollingLinePlot(row=1, col=2, title="Left Motor Controller Temp.", ymin=0, ymax=255, width=25, ylabel="deg F")
r_mc_t = ScrollingLinePlot(row=2, col=2, title="Right Motor Controller Temp.", ymin=0, ymax=255, width=25, ylabel="deg F")

accel = BarChart(row=1, col=3, title="Accel Pedal %", ymin=0, ymax=100, ylabel="%")
accel_err = BarChart(row=1, col=4, title="Accel Pedal Error", ymin=0, ymax=1, ylabel="", show_axes=False)

brake = BarChart(row=2, col=3, title="Brake Pedal %", ymin=0, ymax=100, ylabel="%")
brake_err = BarChart(row=2, col=4, title="Brake Pedal Error", ymin=0, ymax=1, ylabel="", show_axes=False)

data = [ 0 for i in range(SIZE) ];
while 1:
Expand All @@ -84,18 +88,32 @@ def car_monitor(arduino, log):
r_bat_t.update(data[2])
l_bat_soc.update(data[3])
r_bat_soc.update(data[4])
rpm.update(data[5])
rpm.update(data[5]/10.0)
l_motor_t.update(data[6])
r_motor_t.update(data[7])
l_mc_t.update(data[8])
r_mc_t.update(data[9])
accel.update(data[10])
brake.update(data[11])
accel_err.update(1,color = 'r' if data[11] else 'g')
brake.update(data[12])
brake_err.update(1,color = 'r' if data[13] else 'g')
lv_soc.update(data[14]/10.0)
liveplot_update(0.1)

if __name__ == "__main__":
arduino = Arduino(SERIAL_PORT)
log = open(LOG_FILE, 'a')
serial_port = ""
log_file = "TelemetryData.txt"

if len(sys.argv) == 2:
serial_port = sys.argv[1]
elif len(sys.argv) == 3:
serial_port = sys.argv[1]
log_file = sys.argv[2]
else:
print("Incorrect Argument Format\nUsage: python grapharduino.py serial_port [log file]")
sys.exit()
arduino = Arduino(serial_port)
log = open(log_file, 'a')
log.write(strftime(">> BEGIN LOG << %m/%d/%y at %I:%M %p\n"))
try:
car_monitor(arduino, log)
Expand Down
8 changes: 6 additions & 2 deletions liveplots.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ def update(self, val):
class BarChart:
""" Single bar in a plot that can show a
changing height value """
def __init__(self, row, col, rowspan=1, colspan=1, title="Bar Chart", ymin=0, ymax=100, ylabel="", color='r'):
def __init__(self, row, col, rowspan=1, colspan=1, title="Bar Chart", ymin=0, ymax=100, ylabel="", color='r', show_axes=True):
self.axis = plt.subplot2grid(window_size, (row,col),
rowspan=rowspan, colspan=colspan)
self.axis.set_title(title)
self.axis.set_ylabel(ylabel)
self.axis.xaxis.set_visible(False)
self.axis.yaxis.set_visible(show_axes)
self.axis.set_xlim(0, 10)
self.axis.set_ylim(ymin, ymax)
self.value = 0;
Expand All @@ -76,9 +77,12 @@ def __init__(self, row, col, rowspan=1, colspan=1, title="Bar Chart", ymin=0, ym
height = self.value,
width = 8,
color = color)[0]
self.color = color

def update(self, val):
def update(self, val, color=None):
self.value = val
if color is not None:
self.bar.set_color(color)
self.bar.set_height(self.value)

class Dial:
Expand Down

0 comments on commit aafe5f1

Please sign in to comment.