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

Updated project to latest Arduino sketch specification #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Slcanuino

This is an Arduino sketch which makes a CAN-BUS shield into a CAN-USB adapter for Linux SocketCAN(can-utils). Library files under Canbus folder originaly comes from 'CAN-BUS ECU Reader demo sketch v4' on skpang and were modified. The files should be copied under ~/Arduino/libraries/ to compile the sketch file:'slcan.ino'.
This is an Arduino sketch which makes a CAN-BUS shield into a CAN-USB adapter for Linux SocketCAN(can-utils). Library files under Canbus folder originaly comes from 'CAN-BUS ECU Reader demo sketch v4' on skpang and were modified.

http://skpang.co.uk/catalog/arduino-canbus-shield-with-usd-card-holder-p-706.html

Expand Down Expand Up @@ -30,7 +30,7 @@ or just install can-utils package.
$ sudo apt install can-utils

## Setup
Please replace ttyUSB with ttyACM in case of using Arduino Uno.
Please replace ttyUSB0 with ttyACM0 in case of using Arduino Uno.

$ sudo slcan_attach -f -s6 -o /dev/ttyUSB0
$ sudo slcand -S 1000000 ttyUSB0 can0
Expand Down
10 changes: 5 additions & 5 deletions slcan.ino → slcanuino.ino
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <mcp2515_defs.h>
#include <mcp2515.h>
#include <defaults.h>
#include <global.h>
#include <Canbus.h>
#include "src/canbus/mcp2515_defs.h"
#include "src/canbus/mcp2515.h"
#include "src/canbus/defaults.h"
#include "src/canbus/global.h"
#include "src/canbus/Canbus.h"

#define LED_OPEN 7
#define LED_ERR 8
Expand Down
89 changes: 42 additions & 47 deletions Canbus/Canbus.cpp → src/canbus/Canbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,58 +150,53 @@ char CanbusClass::ecu_req(unsigned char pid, char *buffer)
while(timeout < 4000)
{
timeout++;
if (mcp2515_check_message())
if (mcp2515_check_message())
{
if (mcp2515_get_message(&message))
{
if((message.id == PID_REPLY) && (message.data[2] == pid)) // Check message is the reply and its the right PID
{

if (mcp2515_get_message(&message))
{
if((message.id == PID_REPLY) && (message.data[2] == pid)) // Check message is the reply and its the right PID
{
switch(message.data[2])
{ /* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs */
case ENGINE_RPM: // ((A*256)+B)/4 [RPM]
engine_data = ((message.data[3]*256) + message.data[4])/4;
sprintf(buffer,"%d rpm ",(int) engine_data);
break;

case ENGINE_COOLANT_TEMP: // A-40 [degree C]
engine_data = message.data[3] - 40;
sprintf(buffer,"%d degC",(int) engine_data);

break;

case VEHICLE_SPEED: // A [km]
engine_data = message.data[3];
sprintf(buffer,"%d km ",(int) engine_data);

break;

case MAF_SENSOR: // ((256*A)+B) / 100 [g/s]
engine_data = ((message.data[3]*256) + message.data[4])/100;
sprintf(buffer,"%d g/s",(int) engine_data);

break;

case O2_VOLTAGE: // A * 0.005 (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc)
engine_data = message.data[3]*0.005;
sprintf(buffer,"%d v",(int) engine_data);

case THROTTLE: // Throttle Position
engine_data = (message.data[3]*100)/255;
sprintf(buffer,"%d %% ",(int) engine_data);
break;

}
message_ok = 1;
}

switch(message.data[2])
{
/* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs */
case ENGINE_RPM: // ((A*256)+B)/4 [RPM]
engine_data = ((message.data[3]*256) + message.data[4])/4;
sprintf(buffer,"%d rpm ",(int) engine_data);
break;

case ENGINE_COOLANT_TEMP: // A-40 [degree C]
engine_data = message.data[3] - 40;
sprintf(buffer,"%d degC",(int) engine_data);
break;

case VEHICLE_SPEED: // A [km]
engine_data = message.data[3];
sprintf(buffer,"%d km ",(int) engine_data);
break;

case MAF_SENSOR: // ((256*A)+B) / 100 [g/s]
engine_data = ((message.data[3]*256) + message.data[4])/100;
sprintf(buffer,"%d g/s",(int) engine_data);
break;

case O2_VOLTAGE: // A * 0.005 (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc)
engine_data = message.data[3]*0.005;
sprintf(buffer,"%d v",(int) engine_data);
break;

case THROTTLE: // Throttle Position
engine_data = (message.data[3]*100)/255;
sprintf(buffer,"%d %% ",(int) engine_data);
break;

}
message_ok = 1;
}
if(message_ok == 1) return 1;
}
}
}


return 0;
return message_ok;
}


Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading