Library functions

Library constructor

To start using Waspmote CAN Bus library, an object from class WaspCAN must be created. This object, called CAN, is created inside the CAN Bus library and it is public to all libraries. It is used through this guide to show how the CAN Bus library works. When creating this constructor, all the variables are defined with an initial value by default.

Switching the module on

This function powers the CAN Bus module and configures the SPI bus. The CAN Bus module can be used only in the socket 0. This function is necessary to configure the module, so the CAN Bus module must be plugged before. In this function must be configured the speed communication. There are four possibilities, 125 Kbs, 250 Kbps, 500 Kbps and 1 Mbs. The most frequently used is 500 Kbps. To operate with the CAN Bus module, we must work with all nodes at the same rate.

Example of use:

// Setting up our devices and I/Os
void setup()
{
// Inits the USB
USB.ON();
delay(100);
// Let's open the bus. Remember the input parameter:
// 1000: 1Mbps
// 500: 500Kbps <--- Most frequently used
// 250: 250Kbp
// 125: 125Kbps
CAN.ON(1000);
}

You can see how to use this function in this example:

Switching the module off

Switches off the Can Bus module and stops sending data frames. This function will disconnect the power supply of the module so all data stored on the stack will be lost.

Example of use:

{
// Switches off the module
CAN.OFF();
delay(100);
}

Receiving data

We can receive CAN messages, filling the predefined message (messageCAN) with the CAN controller message.

{
//****************************************
// 1. Receive data
//****************************************

	if (CAN.messageAvailable() == 1)
	{
		// Read the last message received
		CAN.getMessage(&CAN.messageRx);
		// Print in the serial monitor the received message
		CAN.printMessage(&CAN.messageRx);
	}
}

You can see how to use this function in this example:

Sending data

The CAN Bus module supports CAN to exchange data with other devices. This profile allows to create connections to another device using the same profile (CAN connection). Some functions have been developed to handle the communication between two or more devices. We can send CAN messages, sending the message that we have defined.

Example of use:

{
//****************************************
// 2. Send data
//****************************************
// Insert the ID in the data structure
CAN.messageTx.id = OWNID;
// These fields include the data to send
CAN.messageTx.data[0] = 0;
CAN.messageTx.data[1] += 1;
CAN.messageTx.data[2] = 2;
CAN.messageTx.data[3] = 3;
CAN.messageTx.data[4] = 4;
CAN.messageTx.data[5] = 5;
CAN.messageTx.data[6] = 6;
CAN.messageTx.data[7] = 7;
// The length of the data structure
CAN.messageTx.header.length = 8;
// Send data
CAN.sendMessage(&CAN.messageTx);
}

You can see how to use this function in this example:

Printing data

We can print through the serial port the received message to view the data. This functions is very useful for debugging and code testing.

Example of use:

{
CAN.printMessage(&CAN.messageRx);
delay(10);
}

CAN in Automation

The Can Bus library defines the most important and used PIDs. The CAN Bus module can be used to request information from a vehicle.

Typically, an automotive technician will use PIDs with a proffesional scan tool connected to the vehicle's OBD-II connector. The scan tool sends PIDs to the vehicle's controller area network (CAN). A device on the bus recognizes the PID as one it is responsible for, and reports the value for that PID to the bus. The scan tool reads the response, and displays it. The user could use Waspmote to emulate this kind of scan tools.

Example of use:

{
// Read the value of vehicle speed
int vehicleSpeed = CAN.getVehicleSpeed();
// Print received data in the serial monitor
USB.print("Vehicle Speed: ");
USB.print(vehicleSpeed);
USB.println(" Km/h");
delay(1000);
}

Please note that each car manufacturer implements a different CAN Bus configuration, and each car has a different PID list. This information is usually difficult to access for security reasons.

You can see how to use this function in this example:

The user can also request additional PIDs that he can define in the library. On the other hand, the user can request PIDs manually with the use of the function CiARequest().

Example of use:

{
	int data;

	// Read the value of RPM if the engine
	CAN.CiARequest(myPID);

	if (CAN.messageRx.id == ID_RESPONSE)
	{
		// Insert the formula here
		data = uint16_t(CAN.messageRx.data[3]);
		CAN.printMessage(&CAN.messageRx);
	}

	// Print received data in the serial monitor
	USB.print("Returned data: ");
	USB.print(data);
	USB.print("\n");
	delay(1000);
}

You can see how to use this function in this example:

Last updated