Learning Center
Plans & pricing Sign in
Sign Out
Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>



  • pg 1
									DEFAUX Romaric

Tutor: R.Baran


I - Starting point
        1) HAMBURG and the Fachhochschule
II - The project
        1) Objectives
        2) Choices of development
                a) Presentation of the USB port
                b) Presentation of DirectX
                c) Presentation of GTK+
                d) Presentation of the CAN (Controller Area Network) bus
        3) Realization
                a) Reading of the force feedback wheel
                b) Data sending by CAN bus
                c) User interface
III - Conclusion
        1) Work in team
        2) Data-processing knowledge


I thank Mr. Baran, my tutor, for confidence that it granted to me by leaving me the free field for
the programming of this interesting project.
I also thank the members for my working group, for the assistances that they provided to me.

Le projet de fin d’année, que j’ai du réaliser dans le cadre de mon stage Erasmus à Hambourg,
était de réaliser un programme en visual C++ sous Windows qui lit un volant à retour de force,
qui exploite ces données, puis qui envoie les informations utiles au contrôle du camion par bus
CAN. Pour ce faire j'ai du commencer par apprendre les bases de la programmation sous visual
C++, comment utiliser l'api directX, GTK+ et l'api du bus. Les premiers essais étaient des
programmes simples comme afficher une fenêtre ou lire la souris. Après réalisation de ces
programmes je suis passé à la réalisation du programme principal, qui permet de déplacer le
camion en temps réel par l'intermédiaire du volant, avec une interface graphique affichant les
données utiles et une petite simulation de déplacement du camion.

My end of year project in the context of the Erasmus practice in Hamburg was to realise a visual
C++ program under Windows which read a force feedback wheel, exploit the received datas, and
send the datas to control the truck by CAN bus. For this I have started by learning the bases of the
programming under visual C++, how to use the DirectX API, GTK+ and the bus CAN API. The
first tests were simple programs like display a window or reading the mouse. After realization of
these programs I passed to the realization of the main program, which makes it possible to move
the truck in real time by means of the wheel, with a graphic interface displaying the useful data
and a small simulation of movements of the truck.

The CAN bus is used more and more in the car industry. But before use it in cars, it is interesting
to test it on models to find new uses and improve what already exists.
I - Starting point

       1) HAMBURG and the Fachhochschule

My end of year practice was made in Hamburg during the case of an Erasmus practice. The place
of the practice was the HAW Hamburg (Hochschule für Angewandte Wissenschaften Hamburg ,
Hamburg University of Applied Sciences ). With its 1.8 million inhabitants Hamburg is the
second largest German city after Berlin and with its nine institutions of higher education and
68,000 students it is one of the most important study centers in Germany. Approximately ten
percent of the students at Hamburg's universities come from outside Germany. As a major
international port Hamburg's economic focus is on trade; Hamburg is a city with modern industry
and with 3,300 media companies Germany's media centre. Hamburg is a very green city and its
large and small parks offer ample opportunity for leisure and its lake in the centre of the city the
chance to row or sail.

And in the evenings you have an enormous variety of theatres, musicals, pubs and nightclubs to
choose from. As a major port international relations have always played an important role in
Hamburg's way of life. Hamburg University of Applied Sciences reflects this international
outlook. Of the 13,400 students who study here approximately 2,000 students come from outside
Germany, representing over 100 nations from all over the world. Hamburg University of Applied
Sciences is the second largest institution of higher education in Hamburg. Its thirteen departments
offer a large variety of modern and practice-oriented degree programs - engineering, life sciences,
business, building, design, information and social studies.

My practice was in the 7 floor of the building of Fachbereich Elektrotechnik / Informatik, the
building who is the most focusing in computer science and programming. The main purpose of
my floor was to use the computer with robot and other mechanical thing.
II - The project

       1) Objectives

The purpose of the work is a program which manages a force feedback wheel et sends the
processed data. The application will offer an interface which displays data and allows sending or
not data, to use or not the force feedback, etc.

       2) Choices of development

The program is to create with visual c++ under Windows XP.
We can distinguish some parts in the project:
- read a force feedback wheel plugs in the USB port and processes on received data
- send data by CAN bus
- User interface

To read the wheel I use the API Direct X 9, GTK+ for the interface and to manage the CAN bus
the API provided with de CAN bus.
               a) Presentation of the USB port

The USB bus (Universal Serial Bus, in French Bus série universel) is based on serial type
interface. However it is an input-output interface faster than standards serial ports. The
architecture adopted for this type of port is serial for two principal reasons:

       the serial architecture gives the possibility to use a higher frequency than a parallel
        architecture (with a parallel architecture, a high-flow bits causes errors) ;

       the serial wires are cheaper than parallel wires.

But the best advantage of the USB port is incontestably the Hot plug and play. Thus, we are not
obliged to turn off the computer to plug the peripherals. During the connection of the peripheral
on the host, the host detects the new element. At this time, the computer sends a signal to
initialize the peripheral. Then the peripheral is supplied with electricity and had temporarily a
default address (address 0). The following stage is giving a permanent address. It is called
enumeration procedure. To do that, the computer tests the peripherals already plugged in order to
know their address and so, can give one address to the new peripheral, which gives his
identification in return. The host, having all the necessary characteristics, can load the adapted
               b) Presentation of DirectX

DirectX is a group of drivers useful for a lot of function. It gathers all the drivers and associate to
them the same way of programming. So Direct X represents an interface between the drivers and
the programs which uses it. Thus, we are not obliged to consider all the existing drivers and to
see to it that working with each of them. We have just to program with the Direct X routines, and
it is DirectX which takes care to operate each elements called by the program.

The drivers gathered by DirectX are sorted in different categories:

      Direct3D: 3D graphic acceleration

      DirectDraw: 2D graphic acceleration (vectorial drawings)

      DirectVideo: Acceleration of the video sequences

      DirectSound: Driving the sounds cards

      DirectInput: Driving the games controllers (joystick, joypad, ...)

      DirectPlay: Management of the data circulating through a network (for network games)

Each new version of DirectX has new drivers added, and new functions groups.

DirectInput manage data exchanges between the peripheral (joystick, joypad, mouse, wheel,
controller with force feedback ...) and the program. The management of the force feedback is
available in the versions of DirectX 5.0 and more.
              c) Presentation of GTK+

GTK+ is a group of libraries used to create very easily graphic interfaces (GUI Graphical User
Interface). At the beginning, GTK+ was developed to help in the conception of the image
processing software GIMP (GNU Image Manipulation Program). But now, the application area is
wider, used in a lot of different project. For example, the environment GNOME (GNU Network
Object Model Environment) is based on GTK+.

Using GTK+ to create GUI is really interesting for several reasons :

      GTK+ is under the GNU LGPL license. So GTK+ is free, and we can modify and use it as
       we want, without any financial obligation. GTK+ exists and works under many platforms :
       UNIX-like, Windows, BeOS;

      GTK+ is compatible with a lot of programming languages: C, C++, Ada, Perl, Python,
       PHP and more.
               d) Presentation of the CAN (Controller Area Network) bus

At the beginning, the CAN bus was created by and for the car industry (Robert BOSCH GmbH,
1983), to give a solution for the intern communication in the cars. The CAN bus was quickly
used in many domains of the industry. Moreover, the CAN bus is a very cheap universal
component, because it is standardized.
Main characteristics of the CAN bus:
- Control in real time distributed allows, with a high security level.
- Conceived with requirements of robustness, reliability, simplicity and standardization suitable
for the car industry.
- A bus for the particularly severe environments in temperature and electromagnetic radiation and
- Economic because of the low costs of the elements which it connects within a vehicle.
- Standardized, flexible and modular.
- Messages up to 8 bytes of data, protected by a checksum (checksum) coded on 15 bits.
- Able to function up to 1 Mbit/s.
       3) Realization

At the beginning, the application start two processes which communicate by the global variables.
In one hand, a process managing the wheel, the force feedback, and the window containing the
animations, in the other hand a process managing the graphic interface and the CAN bus.

               a) Reading of the force feedback wheel

                       [1] Use of DirectX

When the software start, it seeks if a force feedback is connected. If no peripheral is detected, an
error message appears to inform the user that a wheel is necessary to the operation of the program,
and the software stop when "OK" is clicked. If a wheel is detected, the software continues
normally (the interface is displayed...). If the wheel is disconnected during the use of the software,
the same error message appears and the program stops after validation of the user.

Initialization of the wheel:

We initialize the wheel in calling the function InitDirectInput, which :
- Creates a DirectInput object with the function DirectInput8Create
- Finds the connected joystick(s) with the callback function EnumDevices (if no joystick is found,
return an error)
- Initializes the format of the received data with the function SetDataFormat
- Sets the cooperative level with the function SetCooperativeLevel (here set exclusive |
background in order to read the wheel when the software is in background)
- Initializes the axes of the joystick with the callback function EnumObjects
- If the joystick is with force feedback:
       - Initializes the force feedback with the function CreateEffect, here we set a null force and
we centered the wheel on his axe
Update the wheel position:

This update is done 50 times a second with a timer which call the function UpdateInputState.
This function:
- Checks if the wheel is connected. The test is
       if ( NULL == g_pJoystick ) return S_OK;
- Recovers the wheel state with the function GetDeviceState
- Updates the global variables (speed, direction ...)
- Displays the updated data in a window with the functions wsprintf and SetWindowText

- If the force feedback is started, we call the function SetDeviceForcesX which:
       - Sets a new force eff to the left if the last was to the right, or to the right if the last was to
the left and dependent on the speed
       - Starts the effect with the function SetParameters

Reset the force feedback:

This function is called when the user disables the force feedback. It set a null force to avoid to
block the wheel in the last force set. This function:
       - Sets a null force and center the wheel
       - Starts the effect with the function SetParameters
Free the wheel:

When the software is closed we must free the resources used by the DirectInput object.

       - the function SAFE_RELEASE is used with each DirectInput object (the joystick, the
force feedback, ...)

                       [2] Received data processing

The data about the wheel are received 30 times a second, for a good fluidity. This frequency can
be modified. These data are :
- the position of the wheel (set from -1000 to +1000, from left to right)
- the position of the acceleration and footbrake pedals (set from -1000 to +1000, -1000 is when
the footbrake pedal is pushed and the acceleration pedal is released, and +1000 the opposite)
- the position of the buttons (pushed or released)
These data are displayed in a windows for the user.

It is important to manage these data in order to create a realistic virtual movement. It was
necessary to integrate the notion of time. Indeed, if for example the speed of the truck is only
proportional to the position of the accelerator pedal at one precise moment, the truck can reach
150 km/h in a quarter of second (the time to push the pedal) !
A system of speeds management (reverse gear, first, second, third) was thus set up. Each speed is
configurable with: starting minimum speed (we cannot pass the third if we are at 5 km/h), a
maximum speed (in first we cannot go to 200 km/h)...
In the same way to turn, rotation is according to the rate of travel and the position of the wheel.
               b) Data sending by CAN bus

Once the program is launched, the user has the possibility to activate the data sending by CAN
bus. All the function are in the CanBus.h file.

The initialization of the CAN bus is done when we called the function CAN_Initialize, which :
- Enables the memory access to the card with the function INIPC_initialize_board.
- Loads and resets the firmware on the interface of the CAN bus (CANPC_reset_board).
- Then initializes the bus with the CAN_initialize_bus function, which :
       - Terminates a possible bus operation and places the CAN chips into "reset" status
      - Defines the bit timing (baud rate) (CANPC_initialize_chip and CANPC_initialize_chip2)
       - Initializes the acceptance filter of the CAN controller (CANPC_set_acceptance and
       - Ensures compatibility to other CAN interfaces with different CAN controllers
(CANPC_set_mode and CANPC_set_mode2)
       - Defines the setting of the OCR (Output Control Register) (CANPC_set_output_control
and CANPC_set_output_control2)
- Initializes the FIFO mode (First In First Out) with the function CAN_initialize_FIFO which :
       - Activates the FIFO operation of the interface (CANPC_enable_fifo)
       -     Provides     API      compatibility    to    the    CAN-AC2        ISA     interface
       - Enables the report of successful transmit jobs in FIFO mode to the PC application
(CANPC_enable_fifo_transmit_ack and CANPC_enable_fifo_transmit_ack2)
- Puts the CAN controller of both CAN channels into operational mode (CANPC_start_chip)
Data sending:
When the data sending is activated, the program sends the data 100 times a second by the bus.
The data are sent per package of 29 bits with the function CANPC_send_data.

The two first bytes code the wheel position (from 1845 to 1155, from left to right), the next the
speed (from 0 to 150) and the fourth the speed gear (first, second, or third are the only gear
allowed for the truck).

Data receiving:
We listen to at the same frequency the arrival of data. The data which arrive are either a package
of 29 bit, or an acknowledgement. We utilize for that the CANPC_read_ac function. Here the
truck does not return any data (no acknowledgment), but this function could be useful later.
                      c) User interface

The graphic interface is the only link between the software and a normal user. So it is an
important part of the software. Here it is a window:

The buttons make it possible to use or not the force feedback, to display/hide a trace of the
movement of the arrow, to send/stop the data sending by CAN bus.
                              [1] Pictures loading

The first function called is loading the pictures for the animation in good structures, creating the
buffers, etc. It use :
- gdk_pixbuf_new_from_file, create a buffer from a picture file (bmp, png, gif...)
- gdk_pixbuf_new, create an empty buffer with specified properties
- gtk_image_new_from_pixbuf, create a picture from a buffer
Then the window for the graphic interface is created with the function InitWindows.

                              [2] Creation of windows, buttons and text area

We create first the main window with the function gtk_window_new and set it with the functions
gtk_window_set_title      (name   the   window),     gtk_window_set_resizable      (true    or     false),
g_signal_connect (connect signals to this window, for example the signal of destruction).

The creation of buttons is very easy too. For standard buttons (for example the button "quit") we
use     the    function    gtk_button_new_from_stock,         for   the   others      the        function
gtk_toggle_button_new_with_label. The parameter of this function are the name to display on
button. Then we connect the signals to this buttons, which are approximately the functions called
when we click on.
For the text, we must first create the space where the text will be displayed, with the function
gtk_text_view_new, then create a link to the text buffer with gtk_text_view_get_buffer. We
change the text in the buffer to change the text displayed.

The organization of the different parts of the windows is manage with the function gtk_table_new,
who divides the window in a grid, after it only remains to attach the various parts to the good
position with the function gtk_container_add.

Finally we set up the timer, one which calls the update of animation (here 20 times a second), and
the other which sends the datas by the CAN bus (100 times a second) with the function
                               [3] Animations

20 times a second the animations are updated. There is :
- the animation of the arrow

- the wheel
- the speed

- the dial speed

                              a) Presentation of some functions

The file gdk_extend.h contains some reusable functions.
gdk_put_pixel function :
Put a pixel in the buffer, with the position and color chooses.
gdk_draw_line function:
Draw a line with the 2 extreme points.
gdk_draw_rectangle function:
Draw a rectangle with the 2 extreme points.
gdk_rotate_buffer function:
Make a rotation of the angle requested on the buffer requested.
                               b) Use

The principle is always the same. First the picture is processed in the buffer with the previous and
others functions, then we display the buffer.

Animation of the arrow:
We initialize the buffer with a black background with the gdk_pixbuf_copy_area function.
Then we create the trace with the gdk_put_pixel function. We add it to the buffer only if it is
Finally we add the rotated arrow with gdk_rotate_buffer function, and the red frame with the
gdk_draw_rectangle function.
Once the buffer ready, we display the picture with the gtk_image_set_from_pixbuf function.

Animation of the wheel:
We initialize the buffer with the rotated wheel. The degree of rotation is the same as the real
Then we add the frame.
And finally we display the buffer.

Animation of the speed:
We use two pictures. One whose color is dark and the other whose color shines.
We draw the dark picture in the buffer, and we add over a part (dependent speed) that whose
color shines with the gdk_pixbuf_composite function.

Animation of the dial speed:
We put the background of the dial in the buffer.
Then we add the line showing the speed, whose angle is calculated compared to speed, with the
gdk_draw_line function.
III - Conclusion

       1) Work in team

During my formation in the IUT (Institut Universitaire Technologique), sometimes I worked in
team on projects gave by our teachers. But in this project, it was different because we had to
choose the way of developments. In this case, the communication is really important, it is
important to understand the role of the different part of the project. Because the different part of
the project made by different people must be compatible. For example the data format I had to
send by CAN bus must be the data waited by the truck receiver, which was programmed by
someone else.
Work in team is good to find solutions. Generally, when I don't know how to solve some
problems, someone can help, or the contrary, when I know something I can explain to help
someone else. Sharing knowledge makes it possible to progress faster.
       2) Data-processing knowledge

I discovered how to use CAN bus, GTK+, Direct X.
To learn, I tested first with small programs, to understand precisely the mechanisms, how to use
the libraries. And after having understood the main part of the mechanisms, I programmed what
was useful for the project.
The first thing to understand was Visual C++. So, I made programs which displayed only a
window, and after I changed parameters to see what happened.
After I had to read the wheel. I had no idea how to do this. I read a lot of documentations on
internet, about USB port. I found nothing really useful. I asked how to do that, and one proposed
me to use DirectX. I installed it, and after having solved problems (to link a library, to configure
the compiler...) began to learn. The first program I did was to read the mouse using DirectX
functions. I displayed the position of the mouse and the state of the buttons (clicked/released).
After to program the wheel was easy because the functions of the library are similar.
Then, to create the graphic interface I chose to use GTK+. I could have use DirectX but I
preferred to discover something new. I found on internet good documentations about how to use
it, with a lot of concrete example.
Then, the problem I had was to mix this two parts. Because to use DirectX we need a Windows
window, and not a GTK window. The solution I found is to launch two different process, it was
the easiest solution to implement.
For the CAN bus, all the functions are explained in the documentation. There is some diagrams
which describe the principal uses mode.
Finally I integrated the CAN bus management in the main program.

It was for me the first experience like that, I mean leave my home in order to work in a foreign
country and I really like this practice. This practice was interesting, and I had learned a lot, in a
friendly environment. Now I can program software or games under Windows.
And Hamburg is really a nice city, and the people are nice. I improved my English and my
German thanks to the friends I met here.

To top