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

feat: changing speed and rpm to floats #163

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 10 additions & 17 deletions arduino/speed_receive/speed_receive.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// Channel 0 SPI_CS Pin: BCM 8
// Channel 1 SPI_CS Pin: BCM 7
// Interupt Pin: BCM25
const int SPI_CS_PIN = BCM8;
const int SPI_CS_PIN = BCM8;
const int CAN_INT_PIN = BCM25;
#else

Expand All @@ -28,21 +28,20 @@ const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
#endif


#ifdef CAN_2518FD
#include "mcp2518fd_can.h"
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
#endif

#ifdef CAN_2515
#include "mcp2515_can.h"
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
#endif

void setup() {
SERIAL_PORT_MONITOR.begin(9600);

while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_8MHz)) { // init can bus : baudrate = 500k
while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_8MHz)) { // init can bus : baudrate = 500k
SERIAL_PORT_MONITOR.println("CAN init fail, retry...");
delay(100);
}
Expand All @@ -53,29 +52,23 @@ void loop() {
unsigned char len = 0;
unsigned char buf[8];

if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if data coming
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if data coming
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf

unsigned long canId = CAN.getCanId();

SERIAL_PORT_MONITOR.println("-----------------------------");
SERIAL_PORT_MONITOR.print("Get data from ID: 0x");
SERIAL_PORT_MONITOR.println(canId, HEX);

// SPEED
byte speed = buf[0];
byte distanceUnit = buf[1];

// RPM
byte lowByte = buf[2];
byte highByte = buf[3];
word rpm = word(highByte, lowByte);
float speed;
float rpm;
memcpy(&speed, buf, sizeof(speed)); // Retrieve speed
memcpy(&rpm, buf + 4, sizeof(rpm)); // Retrieve rpm (offset by 4)

SERIAL_PORT_MONITOR.print("SPEED: ");
SERIAL_PORT_MONITOR.print(speed);
SERIAL_PORT_MONITOR.print("\t");
SERIAL_PORT_MONITOR.print(distanceUnit == KMPH_SETTING ? "KMPH" : "MPH");
SERIAL_PORT_MONITOR.print("\t");
SERIAL_PORT_MONITOR.print("RPM: ");
SERIAL_PORT_MONITOR.print(rpm);
SERIAL_PORT_MONITOR.println();
Expand Down
Loading
Loading