Hello!
I was wondering what the most barebones code for transmitting a command if a condition is met can be over the CAN BUS could be. Hypothetically I would want to send something along the lines of: “3D 11 04 00 C3” on the CAN BUS if the speedometer reads 20 km/h. I found this: #include “carloop/carloop.h”
Carloop carloop;
void setup()
{
carloop.begin();
}
void loop()
{
carloop.update();
CANMessage message;
if(carloop.can().receive(message)) {
// Do awesome things with message!
}
}
AND THIS:
Velocity: 0x3CA
Expressed in:[km] h
Frequency: 6.7 Hz
Minimum Timestamp: 100
But I think that I am missing something. Ideally I would want this to be standalone (so no serial printing required). What could I do?
P.S.
How does one turn 8 bit messages in hex numbers?
#include "application.h"
#include "carloop/carloop.h"
// Don't block the main program while connecting to WiFi/cellular.
// This way the main program runs on the Carloop even outside of WiFi range.
SYSTEM_THREAD(ENABLED);
// Tell the program which revision of Carloop you are using.
Carloop<CarloopRevision2> carloop;
void setup() {
// Configure the CAN bus speed for 500 kbps, the standard speed for the OBD-II port.
// Other common speeds are 250 kbps and 1 Mbps.
// If you don't call setCANSpeed, 500 kbps is used.
carloop.setCANSpeed(500000);
// Connect to the CAN bus
carloop.begin();
}
unsigned long lastTransmitTime = 0;
const unsigned long transmitInterval = 100; /* ms */
void loop() {
// Send a message at a regular time interval
unsigned long now = millis();
if (now - lastTransmitTime > transmitInterval) {
CANMessage message;
// A CAN message has an ID that identifies the content inside
message.id = 0x123;
// It can have from 0 to 8 data bytes
message.len = 8;
// Pass the data to be transmitted in the data array
message.data[0] = 10;
message.data[1] = 20;
message.data[2] = 30;
message.data[3] = 40;
// You can of course pass dynamic data
message.data[4] = (uint8_t)(now >> 24);
message.data[5] = (uint8_t)(now >> 16);
message.data[6] = (uint8_t)(now >> 8);
message.data[7] = (uint8_t)(now);
// Send the message on the bus!
carloop.can().transmit(message);
lastTransmitTime = now;
}
}
Find the latest version and instructions on how to flash it to your Carloop on GitHub.