Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAN training #2

Merged
merged 26 commits into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix RGB LED code
  • Loading branch information
ducky64 committed Mar 6, 2017
commit e60527b5905483a89def7550dd179cfe89683c03
2 changes: 2 additions & 0 deletions lab2.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ can.write(CANMessage(0x42));
Have your edge detection code execute the above on a button press, and you should be done. Feel free to compare against the [solution](solutions/lab2.2.cpp), too.

## Lab 2.3: CAN Messages with Data
When the master node receives a CAN message with ID 0x41, it will pulse its white LED with a length specified by the data field, in milliseconds. The blink length is the first 16-bit integer in the payload, in [big-endian (network order)](https://en.wikipedia.org/wiki/Endianness#Networking) format.

**Objective**: When the button is pressed on your BRAIN, have it transmit a message that triggers a blink on the remote master node at some interesting amount of time (not 250ms as the last lab).

## Lab 2.4: Receiving CAN Messages
Receive a regularly-sent message with a particular ID which sets the RGB LED hue and intensity, allowing all the bus lights to be synchronized.
Expand Down
29 changes: 29 additions & 0 deletions solutions/lab2.4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "mbed.h"

#include "ledutils.h"

RGBPwmOut rgbLed(P0_5, P0_6, P0_7);

DigitalOut led1(P0_3);
DigitalOut led2(P0_9);

DigitalIn btn(P0_4);

RawSerial serial(P0_8, NC, 115200);

CAN can(P0_28, P0_29);

int main() {
// Initialize CAN controller at 1 Mbaud
can.frequency(1000000);

while (true) {
CANMessage msg;
while (can.read(msg)) {
if (msg.id == 0x43) {
uint16_t hue = (msg.data[0] << 8) | (msg.data[1] << 0);
rgbLed.hsv_uint16(hue, 65535, 65535);
}
}
}
}
12 changes: 6 additions & 6 deletions solutions/lab2master.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ const uint16_t RGB_LED_UPDATE_MS = 20; // a respectable 50 Hz

int main() {
// LED Blink State
int32_t blinkLengthMs;
int32_t blinkLengthMs = 0;
Timer ledTimer; // used to track blink length
ledTimer.start();

bool lastButton = true;
Timer buttonDebounceTimer;

ledR.period_us(500);
ledG.period_us(500);
ledB.period_us(500);

uint16_t hue = 0;
Timer rgbLedTimer;
rgbLedTimer.start();
Expand All @@ -42,8 +38,12 @@ int main() {
while (can.read(msg)) {
if (msg.id == 0x42) {
led2 = 1;
ledTimer.reset();
blinkLengthMs = 250;
} else if (msg.id == 0x41) {
led2 = 1;
ledTimer.reset();
blinkLengthMs = (msg.data[0] << 8) | msg.data[1];
}
}

Expand Down Expand Up @@ -77,7 +77,7 @@ int main() {
hue += RGB_LED_UPDATE_MS * 10;
hue = hue % 36000; // 360 degree * 100

rgbLed.hsv_uint16(hue, 65535, 65535);
rgbLed.hsv_uint16(hue, 65535, 32767);

// Update CAN with new hue.
uint8_t data[2];
Expand Down
4 changes: 2 additions & 2 deletions src/ledutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ void RGBPwmOut::hsv_uint16(uint16_t h_cdeg, uint16_t s, uint16_t v) {

// Write outputs.
ledR.pulsewidth_us((uint32_t)r * PWM_PERIOD_US / 65535);
ledG.pulsewidth_us((uint32_t)r * PWM_PERIOD_US / 65535);
ledB.pulsewidth_us((uint32_t)r * PWM_PERIOD_US / 65535);
ledG.pulsewidth_us((uint32_t)g * PWM_PERIOD_US / 65535);
ledB.pulsewidth_us((uint32_t)b * PWM_PERIOD_US / 65535);
}
5 changes: 4 additions & 1 deletion src/ledutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <stdint.h>

#include "mbed.h"

/* Converts H (degrees, [0, 360)), S, V (in [0, 1]), to R, G, B (all in [0, 1])
*/
void hsv_to_rgb_float(float h_deg, float s, float v,
Expand All @@ -20,6 +22,7 @@ void hsv_to_rgb_uint16(uint16_t h_cdeg, uint16_t s, uint16_t v,
/* Common-anode RGB LED controlled by PWM, with fixed-point options.
*/
class RGBPwmOut {
public:
RGBPwmOut(PinName pinR, PinName pinG, PinName pinB):
ledR(pinR), ledG(pinG), ledB(pinB) {
ledR.period_us(PWM_PERIOD_US);
Expand All @@ -43,7 +46,7 @@ class RGBPwmOut {

PwmOut ledR;
PwmOut ledG;
PwmOut b;
PwmOut ledB;
};

#endif /* LEDUTILS_H_ */