New!!OBD2 Com with Xsim3 and Arduino-New!!

Dashboard, gauge projects etc.
Please use the image gallery for your pictures, a short tutorial can be found here.
The first image in the first post will be shown in the project gallery.

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby yokoyoko » Mon 23. Sep 2013, 20:07

Thx mreiner!
User avatar
yokoyoko
 
Posts: 392
Images: 28
Joined: Tue 7. Aug 2012, 03:16
Location: Germany / Bad Eilsen
Has thanked: 33 times
Been thanked: 13 times

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby sirnoname » Tue 24. Sep 2013, 14:39

Hi vicpopo, I have changed the 1500 in the first post. Plz update your other uploads for newcomers ...
If a answer is correct or did help you for a solution, please use the solve button.
User avatar
sirnoname
Site Admin
 
Posts: 1829
Images: 45
Joined: Thu 1. Sep 2011, 22:02
Location: Munich, Germany
Has thanked: 35 times
Been thanked: 128 times

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby vicpopo » Tue 24. Sep 2013, 20:25

Hi ,

I removed the files on the first topic page , I have to change the timeout from 500 to 1500 .Will soon upload the files modified.
User avatar
vicpopo
 
Posts: 645
Joined: Fri 20. Apr 2012, 18:04
Location: Strasbourg France
Has thanked: 39 times
Been thanked: 80 times

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby sirnoname » Tue 24. Sep 2013, 20:44

Make it sense to make it possible with a extra OBD2 command to get the latest joy button of the selected joystick (0 to 31).
You may then remove the buttons and can control the display with joystick?

For example:
request from arduino -> get 0 = no button pressed
request from arduino -> get 1 = button 0 is pressed
request from arduino -> get 2 = button 1 is pressed
request from arduino -> get 3 = button 0 and button 1 is pressed

With bitmask.

OR
request from arduino -> get 0 = no button pressed
request from arduino -> get 1 = button 0 was pressed
request from arduino -> get 2 = button 1 was pressed
Wich means that it was hold down, the release will not be sent. This means you get how often the button was set and the extractor will handle down and up events (no program part for arduino).


Or are all wheel buttons used?
If a answer is correct or did help you for a solution, please use the solve button.
User avatar
sirnoname
Site Admin
 
Posts: 1829
Images: 45
Joined: Thu 1. Sep 2011, 22:02
Location: Munich, Germany
Has thanked: 35 times
Been thanked: 128 times

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby sirnoname » Wed 25. Sep 2013, 23:49

Good news, the disconnect if you get into a game and out of a game is found. A simple cleanup of the values inside the comport buffers.
I changes the front code, of first page, for the arduino and the extractor code.

Added fuel and joybutton support to 3.0.2.6:

//E4 Current fuel (not elm327 complaint)
//E5 Current joy button state 0-7 with bit mask (not elm327 complaint)
//E6 Current joy button state 8-15 with bit mask (not elm327 complaint)
//E7 Current joy button state 16-23 with bit mask (not elm327 complaint)
//E8 Current joy button state 24-31 with bit mask (not elm327 complaint)


Our fuel is not available with standard OBD2 codes because it cannot be displayed in percent (no maximal load is available). E4 will show the same value as with the converter input setup.
You can get the current status of a selected joystick and use its buttons to control your arduino stuff instead of mount bad reachable buttons.
The first 32 buttons of a joystick are supported. Start the test tablet or a game to test the code below with play game.

Example for joystick button detection you can load from below code window. The Arduino LED (Output 13) will show Light if connected. If you press the Joybutton Number 2 (Count from 0) it will be dark until you release the joybutton.

Code: Select all
    //  X-Sim Sample code for receiving OBD2 values from the extractor without using the converter
    //  OBD2 is industry standard, X-Sim will additionally expand the OBD2 PID with gear and other values
    //
    //  Usage:
    //  Arduino has to be connected to a free USB port on the computer where the extractor is running
    //  Open the extractor and open the settings menu, there you have to select the OBD2 menu
    //  Add now your arduino comport to the OBD2 list.
    //  After closing the dialog you will see the arduino LED will be enabled which represents receiving data.
    //  After closing the extractor application the LED will switch off.
    //  Pressing joystick button 2 (count from 0) will disable the LED for a short time
    //  Use this code to insert your own display code at the fitting positions
    //  Copyright 2013 Martin Wiedenbauer, X-Sim.de
    //
    //  References:
    //  http://en.wikipedia.org/wiki/OBD-II_PIDs
    //  http://elmelectronics.com/DSheets/ELM327DS.pdf
    //  http://www.x-sim.de


    bool extractordetected=false;   //Will get updated from last positive or negative receive
    int receivebuffer[20]={0};      //Receive buffer

    void setup()
    {
       pinMode(13, OUTPUT); //Arduino UNO LED off
       digitalWrite(13, LOW);
       Serial.begin(9600);

       //Do here stuff to init display and zero to default



    }

    int HexToInt(int c)
    {
       if (c >= '0' && c <= '9')
       {
          return c - '0';
       }
       else if (c >= 'a' && c <= 'f')
       {
          return c - 'a' + 10;
       }
       else if (c >= 'A' && c <= 'F')
       {
          return c - 'A' + 10;
       }
       else
       {
          return -1; // getting here is bad: it means the character was invalid
       }
    }

    int ParseReceiveBuffer(int commandhighbyte, int commandlowbyte, int receivelength)
    {
       //First character is removed of the receivebuffer string and must not be verified again
       if( receivebuffer[0]=='1' && receivebuffer[2]==commandhighbyte && receivebuffer[3]==commandlowbyte )
       {
          //Parse 2 byte values on position 5 and 6 in the buffer string
          if(receivelength==7)
          {
             int highresult=HexToInt(receivebuffer[5]);
             int lowresult =HexToInt(receivebuffer[6]);
             if(highresult==-1 || lowresult==-1){return -1;}
             return ((16*highresult) + lowresult);
          }
          //Parse 4 byte values on position 5,6,8 and 9 in the buffer string
          if(receivelength==10)
          {
             int tophighresult=HexToInt(receivebuffer[5]);
             int toplowresult =HexToInt(receivebuffer[6]);
             int highresult=HexToInt(receivebuffer[8]);
             int lowresult =HexToInt(receivebuffer[9]);
             if(tophighresult==-1 || toplowresult==-1 || highresult==-1 || lowresult==-1){return -1;}
             return ((4096*tophighresult) + (256*toplowresult) + (16*highresult) + lowresult);
          }
       }
       return -1; //Something is wrong with the returned OBD2 echo command byte
    }

    //This function will wait for the first character in receivetrigger and will parse the result
    int ReceiveValueWithTimeout(int receivetrigger, int commandhighbyte, int commandlowbyte, int receivelength)
    {
       int arduinoserialbuffer=0;
       int buffercount=-1;
       for(int z=0; z < 1500; z++) //1500ms timeout should be enough
       {
          while(Serial.available())
          {
             if(buffercount==-1)
             {
                arduinoserialbuffer = Serial.read();
                if(arduinoserialbuffer != receivetrigger) //Wait until the trigger is reached, ignore echo, first character is not in the buffer
                {
                   buffercount=-1;
                }
                else
                {
                   buffercount=0;
                }
             }
             else
             {
                arduinoserialbuffer = Serial.read();
                receivebuffer[buffercount]=arduinoserialbuffer;
                buffercount++;
                if(buffercount > receivelength) //buffer has now waitlen character length
                {
                   return ParseReceiveBuffer(commandhighbyte, commandlowbyte, receivelength);
                   buffercount=-1;
                }
             }
          }
          delay(1);
       }
       return -1;
    }
   
    void ClearReceiveBuffer()
    {
      while(Serial.available())
      {
          Serial.read();
      }
    }

    void SendEchoDisabled() //not used here and a part of the OBD2 ELM327 specifications, as INFO
    {
       //ATE0 Echo disabled
       Serial.write('A');
       Serial.write('T');
       Serial.write('E');
       Serial.write('0');
       Serial.write('\r');
    }

    //010c Request RPM, remember:  OBD2 RPM is ((A*256)+B)/4
    int GetOBD2RpmValue()
    {
       //010c
       ClearReceiveBuffer();
       Serial.write('0');
       Serial.write('1');
       Serial.write('0');
       Serial.write('c');
       Serial.write('\r');
       return ReceiveValueWithTimeout('4','0','C',10);
    }

    //010d Request speed in kmh
    int GetOBD2SpeedValue()
    {
       //010d
       ClearReceiveBuffer();
       Serial.write('0');
       Serial.write('1');
       Serial.write('0');
       Serial.write('d');
       Serial.write('\r');
       return ReceiveValueWithTimeout('4','0','D',7);
    }

    //01e0 Request gear number
    int GetOBD2GearValue()
    {
       //01e0
       ClearReceiveBuffer();
       Serial.write('0');
       Serial.write('1');
       Serial.write('e');
       Serial.write('0');
       Serial.write('\r');
       return ReceiveValueWithTimeout('4','E','0',7);
    }
   
    //01e5 Request current joystick button status 0 to 7
    int GetOBD2JoyStatus0to7()
    {
       //01e5
       ClearReceiveBuffer();
       Serial.write('0');
       Serial.write('1');
       Serial.write('e');
       Serial.write('5');
       Serial.write('\r');
       return ReceiveValueWithTimeout('4','E','5',7);
    }
   
    //01e6 Request current joystick button status 8 to 15
    int GetOBD2JoyStatus8to15()
    {
       //01e6
       ClearReceiveBuffer();
       Serial.write('0');
       Serial.write('1');
       Serial.write('e');
       Serial.write('6');
       Serial.write('\r');
       return ReceiveValueWithTimeout('4','E','6',7);
    }
   
    //01e7 Request current joystick button status 16 to 23
    int GetOBD2JoyStatus16to23()
    {
       //01e7
       ClearReceiveBuffer();
       Serial.write('0');
       Serial.write('1');
       Serial.write('e');
       Serial.write('7');
       Serial.write('\r');
       return ReceiveValueWithTimeout('4','E','7',7);
    }
   
    //01e8 Request current joystick button status 24 to 31
    int GetOBD2JoyStatus24to31()
    {
       //01e8
       ClearReceiveBuffer();
       Serial.write('0');
       Serial.write('1');
       Serial.write('e');
       Serial.write('8');
       Serial.write('\r');
       return ReceiveValueWithTimeout('4','E','8',7);
    }

    void loop()
    {
       int speed=0;
       int rpm=0;
       int gear=GetOBD2GearValue();
       int joystatus=0;
       while(gear >= 0)
       {
          if(extractordetected==false)
          {
             SendEchoDisabled();   //Will cause a OK\r as answer from the extractor, ignored in this code
             extractordetected=true;
             digitalWrite(13, HIGH); //We have connection and all values are ok, turn on the LED
          }
          //Read all values
          gear = GetOBD2GearValue(); //Notice: offset +1 because of reverse gear
          speed= GetOBD2SpeedValue();
          rpm  = GetOBD2RpmValue();
          rpm=rpm/4 ; //remember OBD2 RPM is ((A*256)+B)/4
          joystatus = GetOBD2JoyStatus0to7();  //Warning: X-Sim 3.0.2.6 needed as minimum
          if(gear!=-1 && speed!=-1 && rpm!=-1 && joystatus!=-1)
          {
             //Show the button status if pressed (dark=pressed, light=not pressed)
             if(joystatus==4) //Joystick bit 100 = Button 2 counting from 0
             {
               digitalWrite(13, LOW);
             }
             else
             {
               digitalWrite(13, HIGH);
             }
          }
       }
       extractordetected=false;
       digitalWrite(13, LOW); //No connection, turn off the LED

       delay(2000); //No serial connection, poll serial port all two seconds + readtimeout until a running extractor is detected
    }
If a answer is correct or did help you for a solution, please use the solve button.
User avatar
sirnoname
Site Admin
 
Posts: 1829
Images: 45
Joined: Thu 1. Sep 2011, 22:02
Location: Munich, Germany
Has thanked: 35 times
Been thanked: 128 times

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby mreiner » Thu 26. Sep 2013, 08:25

Thanks sirnoname

That sounds great .I'll give it a try.

But I've run into another problem *
I'm not much of a programmer, but as far as i understand, the rpm is read in an unsigned int and then divided by 4. That means maximum rpm value of 16383, which is not enough for example for F1 cars. (tested with the Marussia in rFactor2).
Is there any possibility to circumvent this issue?

kr Martin
mreiner
 
Posts: 32
Images: 22
Joined: Wed 15. May 2013, 08:53
Location: Lower Austria / Europe
Has thanked: 1 time
Been thanked: 15 times

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby sliversgyj » Thu 26. Sep 2013, 08:34

sirnoname wrote:Make it sense to make it possible with a extra OBD2 command to get the latest joy button of the selected joystick (0 to 31).
You may then remove the buttons and can control the display with joystick?

For example:
request from arduino -> get 0 = no button pressed
request from arduino -> get 1 = button 0 is pressed
request from arduino -> get 2 = button 1 is pressed
request from arduino -> get 3 = button 0 and button 1 is pressed

With bitmask.

OR
request from arduino -> get 0 = no button pressed
request from arduino -> get 1 = button 0 was pressed
request from arduino -> get 2 = button 1 was pressed
Wich means that it was hold down, the release will not be sent. This means you get how often the button was set and the extractor will handle down and up events (no program part for arduino).


Or are all wheel buttons used?

Hi,
I have an opposite requirement,Is it possible to press the joystick key by ardunino through OBD2 command?
If not,is there any other way to do that,For I want to make the dashboard on my wheel,I want to move some joystick key also to my wheel.
Thanks!
sliversgyj
X-Sim Supporter
 
Posts: 37
Images: 34
Joined: Sun 3. Mar 2013, 13:00
Has thanked: 2 times
Been thanked: 2 times

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby sirnoname » Thu 26. Sep 2013, 12:51

There are different ways for input manipulations (Hooks, Keyboard events, DX commands) but also many against this because of cheating.
You need to send all three options because every game has another input reader connected to only one of this interfaces.

In case you need keyboard input with arduino you use native solutions instead:
http://arduino.cc/en/Reference/MouseKeyboard
This is more easier than software workarounds.
http://www.circuitsathome.com/mcu/hid-j ... ode-sample
In both cases the serial converter near the USB connector get reprogrammed as mouse, keyboard or joystick.
There are several projects in the net.
It is possible to make a descriptor with component devices (a type of internal HUB), this means arduino will get detected as mouse, keyboard and serial converter in one and the same device.
If a answer is correct or did help you for a solution, please use the solve button.
User avatar
sirnoname
Site Admin
 
Posts: 1829
Images: 45
Joined: Thu 1. Sep 2011, 22:02
Location: Munich, Germany
Has thanked: 35 times
Been thanked: 128 times

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby sirnoname » Thu 26. Sep 2013, 13:05

That means maximum rpm value of 16383, which is not enough for example for F1 cars.

Yes, speed and RPM get another OBD code in the next release.

Remember: if you buy a OBD ELM 327 Interface with serial output you can connect your arduino gauge system to your car if it owns a OBD2 connector.
In this case you need to send the Echo Off command like in the current code and the ELM 327 car-autodetection command. Using OBD2 while driving is not allowed in some countries.
If a answer is correct or did help you for a solution, please use the solve button.
User avatar
sirnoname
Site Admin
 
Posts: 1829
Images: 45
Joined: Thu 1. Sep 2011, 22:02
Location: Munich, Germany
Has thanked: 35 times
Been thanked: 128 times

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby sliversgyj » Fri 27. Sep 2013, 15:05

sirnoname wrote:There are different ways for input manipulations (Hooks, Keyboard events, DX commands) but also many against this because of cheating.
You need to send all three options because every game has another input reader connected to only one of this interfaces.

In case you need keyboard input with arduino you use native solutions instead:
http://arduino.cc/en/Reference/MouseKeyboard
This is more easier than software workarounds.
http://www.circuitsathome.com/mcu/hid-j ... ode-sample
In both cases the serial converter near the USB connector get reprogrammed as mouse, keyboard or joystick.
There are several projects in the net.
It is possible to make a descriptor with component devices (a type of internal HUB), this means arduino will get detected as mouse, keyboard and serial converter in one and the same device.

thanks I will have a try.
sliversgyj
X-Sim Supporter
 
Posts: 37
Images: 34
Joined: Sun 3. Mar 2013, 13:00
Has thanked: 2 times
Been thanked: 2 times

PreviousNext

Return to Peripheral Projects

Who is online

Users browsing this forum: No registered users and 1 guest