Implementing The CANopen Protocol and A New Interface With Communication Via Bluetooth in The BioBike
Implementing The CANopen Protocol and A New Interface With Communication Via Bluetooth in The BioBike
of Mechatronics and
Medical Engineering
Project Report
Vedant Prusty
Intern/Praktikum, May-July 2014
[email protected]
Dept. of Mechatronics
Manipal Institute of Technology,
Manipal 576104; INDIA.
Betreuer:
Prof. Dr. Ing Rainer Brucher
Dipl. Ing Volker Schilling-Kstle
1|Page
Implementing CANopen in a new Interface in the BioBike Hochschule Ulm July 2014
Contents
Introduction........................................................................................................................................ 3
Existing Structure: ............................................................................................................................... 3
Aim: ..................................................................................................................................................... 5
The CANopen Protocol:....................................................................................................................... 5
Device model ...................................................................................................................................... 5
Signal Characteristics ...................................................................................................................... 6
Message formats............................................................................................................................. 6
Implementing CANopen in the BioBike: ............................................................................................. 8
Project BB_Motor: .......................................................................................................................... 8
Project CANopenCom.X ................................................................................................................ 11
BioBike Qt Project ...................................................................................................................... 14
End Position Switches: ...................................................................................................................... 17
Other Safety features for preventing motor overrun: .................................................................. 18
Optical Encoder at the Pedal (Drehzahl) system: ............................................................................. 18
Connection assignment on PCB .................................................................................................... 19
Power Module: ................................................................................................................................. 19
Designing and assembling the new Interface PCB: ........................................................................... 20
The New PCB: ................................................................................................................................ 21
CAN: .............................................................................................................................................. 21
Pedal System (Drehzahl): .............................................................................................................. 22
Power: ........................................................................................................................................... 22
Bluetooth: ..................................................................................................................................... 22
LED: ............................................................................................................................................... 22
Casing: ........................................................................................................................................... 23
Future Work and Prospects: ............................................................................................................. 24
Bibliography & Reference: ................................................................................................................ 24
Acknowledgements:.......................................................................................................................... 25
Introduction
The BioBike being developed at Hochschule Ulm is a device which can be used to assess a
drivers performance. It employs various modules including assessing and controlling seat
and handlebar position and height, the angular velocity of the pedal, power brakes, etc.
Additionally, the project holds great prospects in the field of physical rehabilitation, given the
systems ability to analyze human performance and respond accordingly. This project BioBike
has the objective of developing a special test bench for bikers, covering biomechanics,
effective pedaling and optimal power consumption.
The BioBike project which began in 1994 has been divided into a no. of Bachelor projects
and has been worked upon continuously. The Aim of this author has been to implement the
CANopen communication protocol (replacing simple serial communication) to communicate
with the various motors in the device. The second goal was to replace the existing
independent hard wired connections from the various modules of the BioBike to the PC with
a single master PCB which is able to accept all data from these modules and transmit it to the
PC via Bluetooth.
Existing Structure:
The BioBike consists of 4 motors controlling the following:
These motors are controlled by microcontrollers. Potentiometers which movie parallel to the
above components are able to return an analog value to the microcontroller, which uses and ADC to
convert this to a value signifying the position of the component. The individual microcontrollers then
send back this information to the Master Microcontroller which in turn is connected to the PC. The
user can send commands for movement of various parts through a user interface in the PC made
using Qt.
Qt uses standard C++ but makes extensive use of a special code generator (called the Meta Object
Compiler, or mac) together with several macros to enrich the language.
The Qt interface sends back information to the Master microcontroller for controlling individual
motors.
The Microcontrollers to control the motors, the interface, etc. are all programmed using MPLAB X.
MPLAB Integrated Development Environment (IDE) is a free, integrated toolset for the
development of embedded applications on Microchip's PIC and dsPIC microcontrollers. In addition to
its predecessor's functionalities and compatibility with Microchip's existing development tools, the
new MPLAB X IDE utilizes many NetBeans features allowing for user-interface improvements and
performance upgrades.
Besides this, the BioBike consists of various other components including the Power Brake,
the Pedal Force analysis system, the Pedal Angular Velocity Analysis System. These systems send
back data independently to the PC via hard wire connections.
Aim:
The Aim of this project has been to replace the existing individual connections from the
various components to the PC with a single microcontroller attached to the central stationery
module. This microcontroller is mounted on a PCB and transmits signals coming from all
independent motor and Pedal devices to the PC via a Bluetooth module.
This removes the requirement of proximity required by limitations of the hard wire connection to
the PC.
The other goal of this project has been to replace the existing Serial Communication protocol being
used to communicate with the motors with the CANopen protocol.
CANopen is a communication protocol and device profile specification for embedded systems used in
automation. In terms of the OSI model, CANopen implements the layers above and including the
network layer.
Device model
Every CANopen device implements certain standard features in its controlling software.
A communication unit implements the protocols for messaging with the other nodes in the
network
Starting and resetting the device is controlled via a state machine. It must contain the states
Initialization, Pre-operational, Operational and Stopped. The transitions between states are
made by issuing a network management (NMT) communication object to the device.
The object dictionary is an array of variables with a 16-bit index. Additionally, each variable
can have an 8-bit subindex. The variables can be used to configure the device and reflect its
environment, i.e. contain measurement data.
The application part of the device actually performs the desired function of the device, after
the state machine is set to the operational state. The application is configured by variables in
the object dictionary and the data are sent and received through the communication layer.
CANopen devices must have an object dictionary, which is used for configuration and communication
with the device. An entry in the object dictionary is defined by:
Signal Characteristics
CAN may be implemented over a number of physical media so long as the drivers are open-
collector and each node can hear itself and others while transmitting (this is necessary for its
message priority and error handling mechanisms). The most common media is a twisted pair 5v
differential signal which will allow operations in high noise environments and with the right drivers
will work even if one of the wires is open circuit. A number of transceiver chips are available the
most popular probably being the Philips 82C251 as well as the TJA1040.
Message formats
The CAN protocol uses a modified version of the Carrier Sense Multiple Access/Collision
Avoidance (CSMA/CA) technique used on Ethernet. Should two messages determine that they are
both trying to send at the same time then instead of both backing off and re-trying later as is done
with Ethernet, in the CAN scheme, the transmitters detect which message has the highest priority
and only the lower priority message gets delayed. This means that a high priority message is sure of
getting through.
These are the normal message frames used to carry data. They contain the following fields (this is
a simplified description as the controller takes care of the detail)
The way in which message collision is avoided is that each node as it transmits its MID looks on the
bus to see what everyone else is seeing. If it is in conflict with a higher priority message identifier
(one with a lower number) then the higher priority messages bit will hold the signal down (a zero bit
is said to be dominant) and the lower priority node will stop transmitting.
Remote Frames
These are frames that are used to request that a particular message be put on the network - of
course a node somewhere on the network has to be set up to recognize the request, get the data
and put out a Message frame. This mechanism is used in polled networks.
With the new CANopen protocol implemented, it is expected that adding other modules such as
sensors and actuators for ECG, pulse oximetry, ergometrics, etc. in the future is made easier due to
the standard communication protocol.
The changes done on the microcontroller project through MPLAB were on the project
named CANOpenCom.X. This program controls the master microcontroller. Also, the BB_Motor
program was worked upon, which controls individual motor controllers.
In Qt, the PC interface program under the project name Bio Bike was modified. This included
adding new commands for movements, editing movement control accordingly to implement
CANopen, and editing the user interface window.
The codes for the above are explained briefly below. The complete code is attached with this
documentation.
Project BB_Motor:
This project holds the code for the individual motor microcontrollers. It defines device ID for
the node in the CAN bus, and protocols for building, sending and receiving CANopen messages over
the network.
Two new status flags were added. newPosition and isMoving. This was implemented so that the
controller returns the value of the potentiometer under following conditions:
While starting to move the motor, the isMoving flag is set to 1, and is set to 0 every time the motor
is halted.
In the bbMotor_main.c file, a check is made to see if newPosition flag is activated. Accordingly, a
process data object or PDO is built and sent over the CAN network to the PC. The flag newPosition is
then reset to 0.
The main() function checks for motor status before sending out_msg
Project CANopenCom.X
This project holds the code for the master microcontroller installed on the new Interface,
which is able to communicate via Bluetooth with the PC. The project is essentially a standard CAN
communication project used at HS-ULM, which has been modified to suit the needs and
requirements of the BioBike.
In the CANcomm.h header file, various CAN message command IDs have been defined, including
definitions for CAN message in, out, device, version requests, etc. the on and off states of the LED
meant to display CAN message data activity on the Interface board have also been defined here.
In the can.c source file, the writeCAN_BO() function is used to write the CAN message into the
transmit latch from the out_msg variable.
(in_msg and out_msg are variables of the user defined structure type CAN_MSG, defined in can.h
which hold the incoming and outgoing CAN messages respectively. They are defined in the standard
can.c. the can.h and can.c files are used commonly on all devices in the BioBike. )
In the CANcomm_main.c source file, the buffers BUF0 and BUF1 of the microcontrollers are checked
for incoming odd and even addressed CAN messages, and overflow in the presence of the messages
using RXB0CON.RXFUL and RXB1CON.RXFUL. at this time, after the PC_inPck (which is used to send
the incoming CAN message to the PC) is built, the mTOG_LED is used to toggle the CAN data LED.
It is to be noted here that as explained above in the BB_Motor project, initial traffic was
detected on startup of uCs. As soon as a connection was made to power, the master microcontroller
started receiving and transmitting CAN messages from the motor controllers to the PC. This led to
interference with the initial messages of device ID and device recognition by the Qt program. (It was
overwhelmed by CAN messages of potentiometer values, and the program had a tendency to hang).
To prevent this, besides initializing isMoving flag to 0 in the BB_Motor project, a check is made to
see if device has been started (errors.not_started flag should be 0) (i.e. a connection has been
established with PC). Only then does it the program allow receiving and transmitting the messages
from the motor controllers.
BioBike Qt Project
The old User Interface in the Qt program.
The present User Interface with a text widget for Target Position
The BioBikeCAN.h header file in the Qt project has definitions for various CAN message commands,
controls, status, operations and device IDs; corresponding to the same definitions on the
microcontrollers.
The BioBike_main.cpp connects various buttons on the UI to functions. It also defines functions for
connecting to the remote interface through the COM ports of the PC.
It is able to build CAN messages for UP/Down and Forward/Backward controls entered through
buttons. It is able to process incoming CAN messages and analyze potentiometer data, in order to
display the Seat or Handlebar position on sliders on the UI.
The on_btnStopAll() function is called on clicking the Emergency Stop All button. In turn, it
calls the STOP functions of all the motors consecutively. The function is meant as a safety and
proved useful while running tests in the course of the project.
The biobike.ui file holds the UI. As explained earlier, it has basic buttons for motor control inside a
central widget. Vertical and horizontal sliders display positions for the 4 motors based on
potentiometer reading. There is also a slot for entering absolute target position for a specific motor.
The radio button for the desired motor is selected, the value is entered and the GoTo button is
clicked.
The on_btnGoTo function decides the direction in which the motor must
move based on potentiometer reading and the value entered by user.
(It is to be noted that even though provisions are made in the embedded program to receive target
positions for motors, this is not implemented. In this project, the PC or Qt program sends a motor
PLUS or MINUS message based on present position of the potentiometer (in the incoming CAN
message) and target position entered in the UI. As soon as the required position is reached, the
program sends a HALT message to the motor.)
When a CAN message is received by the PC, the sender is identified via
cobID, the display slider is set to the incoming potentiometer value, i.e.
potival, and then it is checked with target value entered by user to
determine if motor should be stopped.
The crank or pedal of the BioBike uses an Optical Encoder or Winkelgeber to analyze information
regarding the angular velocity, angular acceleration, and direction of rotation of the pedal. Based on
this, the Power Brake may be actuated as necessary.
The existing system on the BioBike is the result of another bachelor Project which installed the
GA210 Optical Absolute Encoder from IVO. It is a singleturn encoder with 10 parallel outputs. (Later
in this document, it is explained how the 10 data bits are connected to the master interface on the
PCB.) The encoder has a 5V DC input.
For this purpose, the Female 23pin Sub D coming from the Encoder was replaced with a 15pin
Female SubD connector (optimizing the use of available pins) , and this was connected to the
Master Interface PCB. (The Interface has a 16 pin connector on the PCB, which is connected to a
Male 15 pin connector on the casing) Other than the 10 data pins, GND and power connections, pins
for rotation direction (pin 14) etc. were also connected. The ENABLE (pin 13) (active LOW) and
STORE (pin16) were grounded.
On the PCB, pins 15, 13 and 16 from 16 pin connector are grounded. Pin 11 is connected to 5V
supply. Accordingly, the connections in the sub D need to be modified in a future project which
integrates the angular velocity data acquisition into the Master Interface.
The microcontroller on this new PCB is designed to handle data for CAN communication, the optical
encoder as well as the power brake, as explained later.
1 violet 20 / G0 RA0
2 white/brown 21 / G1 RA1
3 white/green 22 / G2 RA2
4 white/yellow 23 / G3 RA3
5 white/gray 24 / G4 RA5
6 white/pink 25 / G5 RA7
7 white/blue 26 / G6 RA6
8 white/red 27 / G7 RC0
9 white/black 28 / G8 RC1
10 brown/green 29 / G9 RC2
11 green/yellow NULL +5V
12 blue GND RC3
13 yellow ENABLE GND
14 brown UP/DOWN RC4
15 red UB GND
16 pink STORE GND
TK PS 2518-8f-to
Power Module:
The BioBike is powered by an
independent Power Module, the SP320-
24 from MeanWell. The module takes 88
to 264V AC and gives and output of 24V
through parallel connections. This
module was put inside a casing (TK PS 2518-8f-to). Holes were drilled in the casing for Power input,
and supply to motors and the Power Brake.
Independent connections coming from the BioBike to the PC make the system bulky and
complicated. Moreover, this does not allow handling all the various signals at a time. There was a
need to implement a single transmission device. This gave two major advantages, the BioBike system
could be controlled via Bluetooth at a distance, and all the different signals from the various
components of the device could be handled at a time.
CAN:
The new system to be implemented was to have Bluetooth Communication with PC via
Bluetooth. There are two CAN ports on the new PCB, one for the Power brake and one for the Motor
Controllers. The PC sends information to the microcontroller via Bluetooth. The microcontroller then
transmits the CAN message via the dedicated CAN pins available on the PIC 18.
Since the nodes or devices in a CAN network are able to identify whether the message is meant for
them or not (using the message address and device ID), only a single CAN message needs to be
generated in a common communication line used by the Motor controllers and the Power brake.
Power:
The Input Power from the existing Power
Port was 24V. This level had to be converted
to 5V and 3.3V in two phases using Voltage
regulators, resistors and capacitors. The 5V
is used by the main PIC master
Microcontroller. The 3.3V by the Bluetooth
Module on the PCB.
Bluetooth:
The Bluetooth module uses a WT12-A
chip from BlueGiga technologies. It comes
as a readymade module from HS Ulm.
Therefore, a jumper port with relevant
connections for Tx (transmit) and Rx
(Receive) was made on the PCB.
The WT12-A Bluetooth chip
LEDs:
3 LED indicators are used for indicating the following
The decision to include LEDs was important. In the initial assembly stage, the LEDs proved
useful in detecting power fluctuations in the board, and thereby helped in troubleshooting
connection problems. The yellow LED is programmed to toggle every time a CAN message is
received by the Interface. Hence, it is an indication of the amount of traffic in the CAN bus,
which was reduced overtime by changing the embedded programs algorithm for sending and
receiving CAN messages. Since the main power module does not have LEDs on the casing, the
LED on this PCB also serves to indicate Power Supply to the whole BioBike.
Casing:
A Hammond 1554C casing with transparent cover was used to house the
interface. Two holes were drilled on the right side for CAN and power
lines, while a SUB D 15 Male connector is mounted on the lower side
for connection to the SUB D Female coming from the Optical Encoder of
Pedals.
The new PCB interface has been manufactured and installed, but it is yet to be programmed
with codes for Power Brake and the Optical Encoder from Pedal. This will involve taking
microcontroller codes from all the other independent modules and compiling them into one
project in MPLAB and Qt.
The 4 pin power-cum-CAN input to the Interface needs to be realized. This will require
implementing CANopen protocol in the Power Brake, and adding it to the CAN network. (As
of now, of the 4 pins, only 2 pins are connected and supply power to the Interface module)
As mentioned earlier, the BioBike is in continuous development. Since the CANopen protocol
has been implemented, communication with new modules is much easier and standardized
now.
Sensors are yet to be built into the BioBike. Pulse sensors, EMG signal sensor modules, etc.
can be added to better analyze driver performance.
To check proximity between the Handlebar and the Central stationery module, limits can be
implemented through functions in the Qt PC program. This will prevent possible collision
between these modules.
Implementation of the use of actual_position and target _position variables in
microcontrollers so that motor uC can directly receive target position and control motors
accordingly. Similarly, the status variables for denoting end position reached can be
implemented into code.
An innovative step in the project would be to be able to store person-specific-values for the
positions off various motors in .txt or similar file formats. The file can then be read and the
values can be entered automatically by the program as target positions for the various
motors.
Matthias Ebenhoch, Tim Stricker, Erik Weber (Summersemester 2013) Vorgabe eines
Fahrprofils mittels einer Bremmsteuerung BRAKE POWER
Pascal Goll, Willi Konrad (June 2013) (Drehzahlerfassung B 13) Erfassung der Drehzahl
mit Absolut-Drhgeber ber CAN-Bus mit PIC Mikrocontrollersystem PEDAL
Eva Witzel, Gabriel Scheremet, Manuel Glashauser (Summersemester 2013) Mobiler
Ladungsverstrker fr Pedal-Kraftmessung zur Tritttechnik-Erfassung, Technische
Documentation PEDAL FORCE
Volker (May 2014) Assembler & C mit MPLAB-X (Einstieg in die Programmierung von
PIC18Fxxxx Mikrocontrollern)
COMSOL CAN- a brief tutorial for Embedded Engineers (web)
Sirius microSystems MPLAB tutorial
Acknowledgements:
This project would not have been possible without the support (even before the internship)
and encouragement of Prof. Dr. Rainer Brucher, under whose guidance the whole internship
was planned and carried out.
My sincere thanks to Volker Schilling-Kstle, for his constant guidance and help at every step
of this project; and for showing me that there are always different possibilities!