- Home
- Documentation
- ODB2 and CAN Acquisition Libraries
ODB2 and CAN Acquisition Libraries
The intention of this tutorial is to provide a quick method for programming the Arduino DUE CAN shield kit.
The ODB2 and CAN Acquisition Libraries libraries were designed with continuous message TX/RX (DAQ applications) in mind by implementing a periodic scheduler. The scheduler supports both native DUE CAN ports and allows for one to easily implement a “free-running raw” CAN protocol or perhaps something a bit more layered such as OBD2. An OBD2 implemenation is provided, and logged CAN traffic and screen data are provided in the repository (driveHome. files).
Installation
- What you need to do to get it working (assuming a windows installation):
- download Arduino IDE R 1.6.x http://www.arduino.cc/en/Main/Software
- download all of the files from the togglebit repository: Togglebit GitHub
- After IDE installation, create a new folder called “CAN” under a path C:\Program Files (x86)\Arduino\libraries\
- Next drop all files( .c,.h and *.ino) into the new folder (C:\Program Files (x86)\Arduino\libraries\CAN)
- Open Arduino IDE 1.6x to Tools->Boards->Boards Manager and download the Adruino SAM (32-bit ARM cortex-M3) boards hardware support kit
- Once installed, go to boards select the Arduino DUE board ad the target board.
- Opening any of the examples ( *.ino) files will create a new folder.
- You will now be able to verify, upload and tryout the examples!
Getting Started with Free Running CAN
To transmit a message five times a second:
```c++
//create the CANport acqisition schedulers
cAcquireCAN CANport0(CAN_PORT_0);
//define a CANFrame object
cCANFrame RAW_CAN_Frame1;
//start CAN ports, set the baud rate here
CANport0.initialize(_500K);
//then set the ID and transmission rate
RAW_CAN_Frame1.ID = 0x100;
RAW_CAN_Frame1.rate = _5Hz_Rate;
//add to the scheduler
CANport0.addMessage(&RAW_CAN_Frame1, TRANSMIT);
// call the scheduler in a 2mS timer iterrupt
CANport0.run(TIMER_2mS);
// manipulate CAN payload in loop() or wherever appropriate
RAW_CAN_Frame1.U.b[0] = i;
```
Getting Started with ODB2 PIDs
For a reference to PIDs, see: wikipedia.org
To get, for instance, the engine RPM:
```c++
//create the CANport acqisition schedulers
cAcquireCAN CANport0(CAN_PORT_0);
//define an OBDParameter object
cOBDParameter OBD_Speed("Speed ", " KPH" , SPEED , _8BITS, false, CURRENT, 1, 0, &CANport0);
//start CAN ports, set the baud rate here
CANport0.initialize(_500K);
// call the scheduler, preferable in an interrupt
CANport0.run(TIMER_2mS);
//show acquired data in loop() wherever appropriate
Serial.print(OBD_EngineSpeed.getName());
Serial.print(OBD_EngineSpeed.getData());
Serial.println(OBD_EngineSpeed.getUnits());
```
Some Tips and Considerations
- The first revision of code was developed for functionality and not speed. For example, there is only one CAN mailbox implemented. Many many speed efficiencies are yet to be found and optimized.
- The underlying CAN library provided here "due_can.*" may not be the latest contributions from the DUE forum.
- Yes, we've hijacked and modified the variants.h file - the OBD2 has only beent tested on one vehicle so far with 11-bit identifiers
- To create an OBD PID that does not yet exist see the relevant enums in the OBD2.h file.
- Thanks to collin80 for developing the original due_can.* libraries and ivanseidel for developoing the DUETimer libraries
- When in doubt, check your wiring and termination resistors ;)
Programming Arduino Getting Started with Sketches
by Simon Monk
Clear, easy-to-follow examples show you how to program Arduino with ease! "Programming Arduino: Getting Started with Sketches" helps you understand the software side of Arduino and explains how to write well-crafted Sketches (the name given to Arduino programs) using the C language of Arduino. This practical guide offers an unintimidating, concise approach for non-programmers that will get you up and running right away.
Programming Arduino: Getting Started with Sketches explains basic concepts and syntax of C with simple language and clear examples designed for absolute beginners - no prior knowledge of programming is required. It leads you from basic through to advanced C programming concepts and features dozens of specific examples that illustrate concepts and can be used as-is or modified to suit your purposes.
- All code from the book is available for download.
- Helps you develop working Sketches quickly.
Coverage includes: C Language Basics; Functions; Arrays, Strings; Input / Output; Standard Library Goodies; Storage; LCD Displays; Programming for the Web; Program Design; C++ and Library Writing