Page 4 of 8 FirstFirst ... 23456 ... LastLast
Results 31 to 40 of 73

Thread: How to import data from wifi into arduino?

  1. #31
    Kart Driver
    Join Date
    Feb 2016
    Location
    ERR404
    Posts
    77
    Platform
    PS4
    I'm currently reading GameSessionState, RaceState and carFlags as bytes..

    The results are consistant, but don't mean anything.. i.e.

    On 'Practice Menu' - GSS= 1 RS= 0
    Press 'Start' and it starts loading the PitBox screen..

    Just before the screen appears GSS=17, RS=9 Just for a few packets

    Then change to GSS=18 RS=9 and stays like that, even after pressing 'Drive' and doesnt change until you cross the start line, when
    GSS=18 RS=2

    Restart Session = GSS=19 for a few packets, then back to 18..
    Replay GSS=21...

    Basically RaceState=2 is driving, but doesnt start until AFTER crossing the line, which is no good for a dash that needs revs, etc AS SOON AS the driving screen appears..


    But... I found that carFlags is a byte with BIT flags.. i.e. every BIT is a flag for something..

    // (Type#9) Car Flags (to be used with 'mCarFlags')
    enum
    {
    CAR_HEADLIGHT = (1<<0),
    CAR_ENGINE_ACTIVE = (1<<1),
    CAR_ENGINE_WARNING = (1<<2),
    CAR_SPEED_LIMITER = (1<<3),
    CAR_ABS = (1<<4),
    CAR_HANDBRAKE = (1<<5),



    So, I'm going to redo my values displayed as BITs, to see if it makes more sense...


    Back in a BIT..


    Luv
    Chrisie.. xx
    The following 2 users likes this Post: GForce, memoric77


  2. #32
    Kart Driver
    Join Date
    Feb 2016
    Location
    ERR404
    Posts
    77
    Platform
    PS4
    Also...

    'UnfilteredThrottle' is the direct button/pedal value 0-255, only active once driving (=0 when not, even if pressed)

    'Throttle' is the same as above BUT only works if the engine is running..



    DPad = 0
    Up =1
    Dn=2
    L=4
    R=8

    JoyPad = 0
    Triangle = 128
    Circle=32
    X=16
    Square=64



    The UDP stream starts from the main menu, sending Telemetry, and only stops, if you PAUSE the game..


    Luv
    Chrisie.. xx
    The following user likes this Post: GForce


  3. #33
    Superkart Pilot
    Join Date
    May 2015
    Location
    Sydney, NSW
    Posts
    391
    Platform
    PS4
    Wow! This is great work Chrisie.

    I'm in the office today so I'm just about to upload your code to this ESP8266 Shield and see what it does......

    Finger crossed.





    Quote Originally Posted by cyclicscooby View Post
    Also...

    'UnfilteredThrottle' is the direct button/pedal value 0-255, only active once driving (=0 when not, even if pressed)

    'Throttle' is the same as above BUT only works if the engine is running..



    DPad = 0
    Up =1
    Dn=2
    L=4
    R=8

    JoyPad = 0
    Triangle = 128
    Circle=32
    X=16
    Square=64



    The UDP stream starts from the main menu, sending Telemetry, and only stops, if you PAUSE the game..


    Luv
    Chrisie.. xx

  4. #34
    Kart Driver
    Join Date
    Feb 2016
    Location
    ERR404
    Posts
    77
    Platform
    PS4
    Hope it's OK Rot...

    Don't forget you need to move GPIO 0 to GND, BEFORE powerup, for the ESP to accept the upload..

    And.. Compile first, then knock ESP power OFF-ON and hit UPLOAD straight away..

    It works everytime for me..

    Once uploaded, it'll start running the code; you don't need to remove GPIO 0 from Gnd..


    Luv Chrisie.. xx
    The following user likes this Post: GForce


  5. #35
    Kart Driver
    Join Date
    Feb 2016
    Location
    ERR404
    Posts
    77
    Platform
    PS4
    Ok Guys.....

    Been in a very 'bit' and 'byte' place for a few days... Really taken me back to my MOD days, many moons ago...

    So, bare with, because i've changed the previous code quite a bit...

    AND... Maybe you can help me with these damn floats... But more on that later..



    First I displayed the 'bits' of GameSessionState (gameState now), RaceStateFlags (raceState) and CarFlags (carFlags)

    Then went thru the menu's and started driving etc, to see what happened..

    Sorry it the formatting goes wonky..

    ------------- gameState ----------- raceState ---------- carFlags

    Main Menu 00000001 ------------ 0 ------------ 0
    Sub Menus --------- SAME--------------

    Loading
    Practice 00010001 ---------- 00001001 ------------ 00000010 -- Just for a moment, before PitBox screen appears

    PitBox
    Practice 00010010 ---------- SAME --------------- SAME

    Drive->
    Before
    StartLine SAME ------------ SAME ----------------- 00001010

    After
    StartLine SAME ------------ 00000010 ------------ SAME


    If you have a 'Quick Race', Its the same except

    gameState byte 6 = 1 - i.e 0'1'010001 = RaceMode


    I've used these changes to decide if / when i'm driving, and when / what to pass on serially

    So, passing the whole packet serially, caused really bad latency, so i've split the data into smaller packets that only get sent when required.. These are 'included' in 'dataOut.h'

    They are-

    ConnectionInfo - 4 bytes - Sent ONLY during Wifi connection. So I can have an LED, and/or display SSID on a screen

    //char startOfPacket = < // 0
    //char ConnectionC0 // 1
    //byte ConnectionC0Num // 2
    //char endOfPacket = > // 3

    ConnectionC0Num : 0=NoConnect, 1=Attempting 1st SSID, 2=Attempting 2nd SSID, 3=Successfully Connected


    ParticipantInfo - Sent ONCE at the beginning of a race. (I race Solo, so don't care about other drivers)

    // P0 - PartInfo_0 CarName - Have to send in 3 packets,cuz serial.write will overflow 128 buffer
    byte P0[68];
    //char startOfPacket = < // 0
    //char PartInfoP0; // 1 // 0 = 'P'
    //byte PartInfoP0Num // 2 // 1 = '0'
    //char CarName[64]; // 3 // 3
    //char endOfPacket = > // 67
    // 68 byte

    //===============================
    // P1 - PartInfo_1 Class
    byte P1[68];
    //char startOfPacket = < // 0
    //char PartInfoP0; // 1 // 0 = 'P'
    //byte PartInfoP0Num // 2 // 1 = '1'
    //char CarClassName[64]; // 3 // 67
    //char endOfPacket = > // 67
    // 68 byte

    //===============================
    // P2 - PartInfo_2 Track
    byte P2[68];
    //char startOfPacket = < // 0
    //char PartInfoP0; // 1 // 0 = 'P'
    //byte PartInfoP0Num // 2 // 1 = '2'
    //char TrackLocation[64]; // 3 // 131
    //char endOfPacket = > // 67
    // 68 byte



    Dashboard Info - As it says, info for a dashboard (Got my 5" Rev Counter working on another chip, via a pot)

    D0 - DashSetup - 8 bytes - Min/Max data for that car - Sent ONCE after PartInfo
    byte D0[8];
    //char startOfPacket = < // 0
    //char Dash_D0; // 1 // 0 = 'D'
    //byte Dash_D0Num // 2 // 1 = '0'
    //byte LapsInEvent; // 3 // 11
    //byte FuelCapacity; // 4 // 111
    //unsigned int MaxRpm; // 5 // 126
    //char endOfPacket = > // 7


    D1 - Main Dash Info - 32 bytes - Sent EVERY packet, when driving
    byte D1[32];
    //char startOfPacket = < // 0
    //char Dash_D1; // 1 // 0 = 'D'
    //byte Dash_D1Num // 2 // 1 = '1'
    //byte PacketNum; // 3 // 2
    //byte GameState; // 4 // 3
    //byte RaceState; // 5 // 10
    //byte CarFlags; // 6 // 110
    //float CurrentTime; // 7 // 20
    //float Speed; // 11 // 120
    //unsigned int Rpm; // 15 // 124
    //byte GearNumGears; // 17 // 128
    //byte BoostAmount; // 18 // 129
    //float EngineSpeed; // 19 // 448
    //float EngineTorque; // 23 // 452
    //byte TyreGrip[4]; // 27 // 280
    //char endOfPacket = > // 31


    D2 - 2nd Dash Info - 26 bytes - Sent every 500Millis (for testing)
    byte D2[26];
    //char startOfPacket = < // 0
    //char Dash_D2; // 1 // 0 = 'D'
    //byte Dash_D2Num // 2 // 1 = '2'
    //int OilTempCelsius; // 3 // 100
    //unsigned int OilPressureKPa; // 5 // 102
    //int WaterTempCelsius; // 7 // 104
    //unsigned int WaterPressureKpa; // 9 // 106
    //unsigned int FuelPressureKpa; // 11 // 108
    //float FuelLevel; // 13 // 116
    //char EnforcedPitStopLap; // 17 // 130
    //byte CrashState; // 18 // 131
    //float OdometerKM; // 19 // 132
    //byte AeroDamage; // 23 // 456
    //byte EngineDamage; // 24 // 457
    //char endOfPacket = > // 25

    D3 - 3rd Dash Info - 16 bytes - Sent every 2000Millis (for testing)
    byte D3[16];
    //char startOfPacket = < // 0
    //char Dash_D3; // 1 // 0 = 'D'
    //byte Dash_D3Num // 2 // 1 = '3'
    //float BestLapTime; // 3 // 12
    //float LastLapTime; // 7 // 16
    //float PersonalFastestLapTime; // 11 // 40
    //char endOfPacket = > // 15


    There is also a D9 - DashChange packet, for 'OnChange' data, like joyPad.. Only sent then..

    And S1 for suspension, M1 for Motion

    But these last 3 are not coded for yet, just defined...

    NOTE- All testing at moment.. Nothing is set in stone, just seeing what does what for now..


    Also-- I've added a 2nd Wifi Network, so it tries the 1st network (5 times), then tries a 2nd network.. You may not need / be interested in that


    Luv
    Chrisie.. xx
    The following 2 users likes this Post: GForce, memoric77


  6. #36
    Kart Driver
    Join Date
    Feb 2016
    Location
    ERR404
    Posts
    77
    Platform
    PS4
    So here's the attached dataOut.h file (exactly the same as putting the fields in the top of the code, just separated to another Tab (makes debugging easier))

    Code:
    // Project Cars UDP data structure
    /*
    Output fields
    P0 = PartInfo - Sent ONLY once, at start of race.
    D1, D2, D3, D9, D0 = Dashboard - 1=Every Packet, 2= 1 in 5/10
                                     3= 1 in 50, 9=ChangeState, 0=Start only(P1,P2)
    S1 = Suspension Data - Sent EVERY packet
    
    M1,M2,MX, = Motion - XYZ data - 1=Every packet, 2= 1 in 5/10, 9=Change                                  
    
    */
    
    //char packetType; // Used to Write Packet Type Out
    char packetIsConnectInfo = 'C';
    char packetIsPartInfo = 'P';
    char packetIsDash = 'D';
    char packetIsSusp = 'S';
    char packetIsMotion = 'M';
    
    //char startOfPacket = '<';  // 60 = '<'
    //char endOfPacket = '>';  // 62 = '>'
    byte startOfPacket = 60;  // 60 = '<'
    byte endOfPacket = 62;  // 62 = '>'
    
    // Used to Write Packet Number Out
    byte packetNum0 = 0;   // Startup Packet - Only sent at start of Race
    byte packetNum1 = 1;   // Primary data stream. Sent Every packet
    byte packetNum2 = 2;   // Secondary stream. Sent 1 in 5/10 times
    byte packetNum3 = 3;   // 3rd stream. Sent 1 in 50/100 times
    byte packetNum9 = 9;   // Change - Sent on ChangeState
    // Connection Packet byte 2
    byte C0Disconnect = 0;
    byte C0Attempt1 = 1;
    byte C0Attempt2 = 2;
    byte C0Connect = 3;
     
    
    //===============================
    // C0 - Connection Info
    byte C0[4];
    //char startOfPacket = <             // 0
    //char ConnectionC0                  // 1
    //byte ConnectionC0Num               // 2
    //char endOfPacket = >               // 3
    // 4 bytes
    //===============================
    // P0 - PartInfo_0 CarName -  Have to send in 3 packets,cuz serial.write will overflow 128 buffer
    byte P0[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '0'
    //char  CarName[64];                 // 3   // 3
    //char endOfPacket = >               // 67 
    // 68 byte
    
    //===============================
    // P1 - PartInfo_1 Class
    byte P1[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '1'
    //char  CarClassName[64];            // 3   // 67
    //char endOfPacket = >               // 67
    // 68 byte
    
    //===============================
    // P2 - PartInfo_2 Track
    byte P2[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '2'
    //char  TrackLocation[64];           // 3   // 131
    //char endOfPacket = >               // 67
    // 68 byte
    
    //===============================
    // D0 - Dashboard_0 - Startup parameters - Send ONCE at start of race
    byte D0[8];
    //char startOfPacket = <             // 0
    //char  Dash_D0;                     // 1   // 0 = 'D'
    //byte  Dash_D0Num                   // 2   // 1 = '0'
    //byte LapsInEvent;                  // 3   // 11   
    //byte FuelCapacity;                 // 4   // 111  
    //unsigned int MaxRpm;               // 5   // 126  
    //char endOfPacket = >               // 7
    // 8 bytes
    
    //===============================
    // D1 - Dashboard_1
    byte D1[32];
    //char startOfPacket = <             // 0
    //char  Dash_D1;                     // 1   // 0 = 'D'
    //byte  Dash_D1Num                   // 2   // 1 = '1'
    //byte PacketNum;                    // 3   // 2    
    //byte GameState;                    // 4   // 3    
    //byte RaceState;                    // 5   // 10
    //byte CarFlags;                     // 6   // 110
    //float CurrentTime;                 // 7   // 20   
    //float Speed;                       // 11  // 120  
    //unsigned int Rpm;                  // 15  // 124  
    //byte GearNumGears;                 // 17  // 128  
    //byte BoostAmount;                  // 18  // 129  
    //float EngineSpeed;                 // 19  // 448  
    //float EngineTorque;                // 23  // 452  
    //byte TyreGrip[4];                  // 27  // 280  
    //char endOfPacket = >               // 31
    // 32 bytes
    
    //===============================
    // D2 - Dashboard_2
    byte D2[26];
    //char startOfPacket = <             // 0
    //char  Dash_D2;                     // 1   // 0 = 'D'
    //byte  Dash_D2Num                   // 2   // 1 = '2'
    //int OilTempCelsius;                // 3   // 100  
    //unsigned int OilPressureKPa;       // 5   // 102  
    //int WaterTempCelsius;              // 7   // 104  
    //unsigned int WaterPressureKpa;     // 9   // 106  
    //unsigned int FuelPressureKpa;      // 11   // 108  
    //float FuelLevel;                   // 13   // 116  
    //char EnforcedPitStopLap;           // 17   // 130  
    //byte CrashState;                   // 18   // 131  
    //float OdometerKM;                  // 19   // 132 
    //byte AeroDamage;                   // 23   // 456 
    //byte EngineDamage;                 // 24   // 457 
    //char endOfPacket = >               // 25
    // 26 bytes
    
    //===============================
    // D3 - Dashboard_3
    byte D3[16];
    //char startOfPacket = <             // 0 
    //char  Dash_D3;                     // 1   // 0 = 'D'
    //byte  Dash_D3Num                   // 2   // 1 = '3'
    //float BestLapTime;                 // 3   // 12   
    //float LastLapTime;                 // 7   // 16   
    //float PersonalFastestLapTime;      // 11  // 40   
    //char endOfPacket = >               // 15
    //16 bytes
    
    //===============================
    // D9 - Dashboard_9 - OnChange - Send if data changes
    byte D9[15];
    //char startOfPacket = <             // 0
    //char  Dash_D9;                     // 1   // 0 = 'D'
    //byte  Dash_D9Num                   // 2   // 1 = '9'
    //byte BrakeDamage[4];               // 3   // 320  
    //byte SuspensionDamage[4];          // 7   // 324  
    //byte DPad;                         // 11  // 1366     
    //unsigned int JoyPad;               // 12   // 96   
    //char endOfPacket = >               // 14
    // 15bytes
    
    //===============================
    // S1 - Suspension - Send EVERY packet
    byte S1[84];
    //char startOfPacket = <             // 0
    //char  Dash_S1;                     // 1   // 0 = 'S'
    //byte  Dash_S1Num                   // 2   // 1 = '1'
    //float TyreHeightAboveGround[4];    // 3   // 284  
    //float WheelLocalPositionY[4];      // 19  // 376  
    //float RideHeight[4];               // 35  // 392  
    //float SuspensionTravel[4];         // 51  // 408  
    //float SuspensionVelocity[4];       // 67  // 424  
    //char endOfPacket = >               // 83
    // 84 bytes
    
    //===============================
    // M1 - Motion - Send EVERY packet (WHEN READY i.e. building new seat)
    byte M1[124];
    //char startOfPacket = <             // 0
    //char  Dash_M1;                     // 1   // 0 = 'M'
    //byte  Dash_M1Num                   // 2   // 1 = '1'
    //float Orientation[3];              // 3   // 136  
    //float LocalVelocity[3];            // 15  // 148  
    //float WorldVelocity[3];            // 27  // 160  
    //float AngularVelocity[3];          // 39  // 172  
    //float LocalAcceleration[3];        // 51  // 184  
    //float WorldAcceleration[3];        // 63  // 196  
    //float ExtentsCentre[3];            // 75  // 208  
    //byte Terrain[4];                   // 87  // 224  
    //float TyreSlipSpeed[4];            // 91  // 260  
    //float TyreY[4];                    // 107 // 228  
    //char endOfPacket = >               // 123
    // 124 bytes
    
    
    //====================================================
    // Left over fields from orig 'Wanted'
    // Unfiltered input
    //byte UnfilteredThrottle;             // 6    - 4
    //byte UnfilteredBrake;                // 7    - 5
    //byte UnfilteredSteering;             // 8    - 6
    //byte UnfilteredClutch;               // 9    - 7
    // Flags
    //byte HighestFlag;                    // 98   - 28
    // Pit info
    //byte PitModeSchedule;             // 99   - 29
    // Car state
    //byte Brake;                       // 112  - 42
    //byte Throttle;                    // 113  - 43
    //byte Clutch;                      // 114  - 44
    //char Steering;                    // 115  - 45
    // Wheels / Tyres
    //byte TyreFlags[4];                // 220  - 152
    //float TyreRPS[4];                 // 244  - 176
    //float TyreLateralStiffness[4];    // 300  - 228
    //byte TyreWear[4];                 // 316  - 244
    //unsigned int AirPressure[4];      // 440  - 320
    // 339                                  // 1368 struct is padded to
    //===============================================================
    The following 2 users likes this Post: GForce, memoric77


  7. #37
    Kart Driver
    Join Date
    Feb 2016
    Location
    ERR404
    Posts
    77
    Platform
    PS4
    The code for the ESP...

    Code:
    /*
     =====================
     = Pcars_udp_rx_v5.3 =
     =====================
    
      Version Control
      ===============
      V1 - Copy of esp8266/WiFiscan & WiFiUDPsendrecievestring merged.. 
           scan for finding Hotspot, udp for UDP.
           Recieves packet of size 1367 (Pcars packet size)
      V1.1 Tidy code & reformat.
           Changed packet size from 255 to 1367.
           
     *V2 - Retrieve packets and move to struct for analysis.
      V2.1 Removed Arduino-dash code.. 
      V2.2 Decode ParticipantInfoString
           * NOTE - ESP 44% Full...      
      V3.0 Code cleaned up. All remains or original code removed (udp send, etc)
           Tried various way to get fastestlap. serial printed all steps, but always zero... 
      V3.1 De-code first part of Telemetry to see if gamestate or similar is used to detect if 
           actually driving, cuz telemetry stream is present all the time.. 
           Only need to write a packet IF driving... 
      V3.2 Add Multi Network option - For S5 AND Paul Hotspot
           Tried MultiAP, but had stack overflow..!!  
           My version tries S5 5 times, if still no connection, goto next network (Paul)
      V3.3 Prepare for serial connection to Teensy
           Remove ALL serial prints, and serial.write packetbuffer directl after read.     
      V4.0 Use SerialStream and Struct....!?!
    
      V5.0 Define smaller packets
           Splits data to smaller packets for Serial to Teensy.
           GameState and counts done here. 
           Type 0 = Startup ONLY - Just once at beginning of Race/PitBox
           Type 1 = Every Packet        
           Type 2 = 1 in 5/10 packets - depends on speed
           Type 3 = 1 on 20/50 packets - " ditto "
           Type 9 = OnChange - (Brk & Sus damage + Dpad & Joypad + ?flags)
      V5_1 Decide GameState
             If GameState == 17 OR 81
                Setup/Prepare for Driving
                Send P0 and D0
             Else if GameState == 18 & CF Bit 3 = 1
                Start Dash - Practice
             else if GameState == 82 
                Start Dash - Race      
    
           Added Packet type C for Connection only       
      V5_2 Testing and fixing Start/Stop data options.
           'raceStart' not setting 'needSetup', so had to add 'firstRun'
           Practice and Solo QuickRace working. Will probably need to add Career flags later
      V5_3_1
           'write' packets out.
           Connection
           PartInfo
           D0/D1/D2/D3
    
      
      Program Desciption
      ==================
      Connects to Hotspot (my S5 or Pauls). 
      Reads UDP stream on Port 5606. Port 5606 is the default port for Pcars.
      
      I/Os 
      ==== 
      Just ESP8266 at mo.
      Rx/Tx - serial (Tx > Tx / Rx > Rx on UNO) - Teensy probably opposite..
      ***  Move GPIO 0 to Gnd "BEFORE" rebooting, for flash mode.
      CH_PD & RST & GPIO2 pulled to VCC with 3k3.
    */
    
    //============================================================================
    //= Libraries
    //============================================================================
    // ESP8266 Wifi
    #include <ESP8266WiFi.h>
    
    // Standard Wifi UDP Lib
    #include <WiFiUdp.h>
    
    // Data from Pcars - Cut down to required fields
    //#include "Data_Struct_Cut.h"
    
    // Data for Serial - Cut down to required fields
    #include "data_out.h"
    
    //============================================================================
    //= Setup Definitions
    //============================================================================
    unsigned int localPort = 5606;        // Pcars UDP Port on all platforms
    
    //============================================================================
    //= Storage section
    //============================================================================
    // WiFi fields
    int status = WL_IDLE_STATUS;
    int tries = 0;
    
    char ssid[] = "S5 Hotspot";          //  your network SSID (name)
    char pass[] = "*******";          // your network password (use for WPA, or use as key for WEP)
    
    char ssid2[] = "Paul Hotspot";       //  your network SSID (name)
    char pass2[] = "*******";           // your network password (use for WPA, or use as key for WEP)
    
    // Incoming UDP packet fields
    byte packetBuffer[1367];             //buffer to hold incoming packet
    
    unsigned int PartInfo = 1347;        // Is PartInfo packetsize - packet size match
    unsigned int Telemetry = 1367;       // Is Telemetry packetsize
    
    //bool inMenu = true;
    bool inGame = false;
    bool practiceStart = false;
    bool raceStart = false;
    byte raceLoading = 81;
    bool needPartInfo = false;
    bool needSetup = false;
    bool firstRun = true;
    
    // Timers
    uint32_t D2oldtime = millis();
    uint32_t D2delay = 1000; // 3k for testing - set to 1000
    uint32_t D3oldtime = millis();
    uint32_t D3delay = 2010; // 3k for testing - set to 1000
    
    //============================================================================
    //= Setup objects
    //============================================================================
    
    // Create Udp object
    WiFiUDP Udp;
    
    //============================================================================
    //= Setup
    //============================================================================
    void setup() {
      //Initialize serial and wait for port to open:
      delay(2000);
      Serial.begin(115200);
      while (!Serial) {
        ; // wait for serial port to connect. Needed for Leonardo only
      }
      delay(1000);
    // Set OutBuffer Headers
      // Set 'startOfPacket' - Byte0
      C0[0] = P0[0] = P1[0] = P2[0] = D0[0] = D1[0] = D2[0] =  
      D3[0] = D9[0] =  S1[0] = M1[0] = startOfPacket;
      // Set Packet Type - Byte 1
      C0[1] = packetIsConnectInfo;
      P0[1] = P1[1] = P2[1] = packetIsPartInfo;
      D0[1] = D1[1] = D2[1] = D3[1] = D9[1] = packetIsDash;
      S1[1] = packetIsSusp;
      M1[1] = packetIsMotion;
      // Set Packet Number - Byte 2
      P0[2] = D0[2] = C0[2] = packetNum0;
      P1[2] = D1[2] = S1[2] = M1[2] = packetNum1;
      P2[2] = D2[2] = packetNum2;
      D3[2] = packetNum3;
      D9[2] = packetNum9;
      // Set 'endOfPacket' - Byte variable
      C0[3] = P0[67] = P1[67] = P2[67] = 
      D0[7] = D1[31] = D2[25] = D3[15] = D9[14] = S1[83] = M1[123] = endOfPacket;
    
    
      C0[1] = 'C';
      C0[2] = C0Disconnect;
      Serial.write(C0, 4);
      delay(500);
    
      // attempt to connect to Wifi network:
      while ( status != WL_CONNECTED) {
        C0[2] = C0Attempt1;
        Serial.write(C0, 4);
    //    Serial.print("Attempting to connect to SSID: "); Serial.println(ssid);
        status = WiFi.begin(ssid, pass);
        // wait for connection:
        delay(4000);
        tries++;
        if (tries == 6 ) {
    //      Serial.println("Failed to Connect to First Network, Trying Next");
          break;
        }
      }
      if (tries == 6 ) {
        while ( status != WL_CONNECTED) {
          C0[2] = C0Attempt2;
          Serial.write(C0, 4);
    //      Serial.print("Attempting to connect to SSID: "); Serial.println(ssid2);
          status = WiFi.begin(ssid2, pass2);
          // wait for connection:
          delay(4000);
        }
      }  
      
      // WiFi Successfully connected. Send connect packet
      C0[2] = C0Connect;
      Serial.write(C0, 4);
    
      // Start UDP
      Udp.begin(localPort);
    }
    
    //============================================================================
    //= Main Loop
    //============================================================================
    void loop() {
    
      if ( status != WL_CONNECTED) {
        //Send ConnectPacket =Disconnect
        C0[1] = C0Disconnect;
        Serial.write(C0, 4);
      }  
    
      // If data available, read a packet
      int packetSize = Udp.parsePacket();
      if (packetSize) {
        if (packetSize == Telemetry) {  // 
    //      Serial.println("***** Received Telemetry ***** ");
          Udp.read(packetBuffer, 1367);
    // debug
    //      PacketNum = packetBuffer[2]; //
    //      GameState = packetBuffer[3];   // 
    //      RaceState = packetBuffer[10];    //
    //      CarFlags = packetBuffer[110];
    //      int carbit3 = bitRead(packetBuffer[110], 3);
    //      Serial.print("TelemetryPacketNum : "); Serial.println(TelemetryPacketNum);
    //      Serial.print("GameSessionState : "); Serial.println(GameSessionState);
    //      Serial.print("RaceStateFlags : "); Serial.println(RaceStateFlags);
    //      Serial.print("CarFlags : "); Serial.println(CarFlags);
    //      Serial.print("carbit3 : "); Serial.println(carbit3);
    //      Serial.println(" ");
    // end debug
              
          // Set gameState flags
          if (packetBuffer[3] == 1) {  // In Menu - NOT driving
            inGame = false;
            practiceStart = false;
            raceStart = false;
            needSetup = true;
            needPartInfo = true;
            firstRun = true;
          }
          else {
            inGame = true;         // We are InGame
            if (firstRun) {
              needSetup = true; // Added because Race mode not sending D0.?
              firstRun = false;
            }
            if (packetBuffer[3] == 18 && bitRead(packetBuffer[110], 3) == 1) {
              practiceStart = true;
            }
            else { 
              practiceStart = false;
            }
            if (packetBuffer[3] == 82) {
              raceStart = true;
            }
            else {
              raceStart = false;
            }
          }
          // Now Flags are set. Decide when to send data, ONLY when driving
          if (inGame) {
            if (practiceStart || raceStart) {
              if (needSetup) {
                needPartInfo = true;
                sendDashSetup();      // packet D0
                needSetup = false;             
              }
              sendDash(); // This will decide if it sends D1/2/3/9
    //          sendSuspension(); // sends S1
    //          sendMotion(); // sends M1
            }
          }
        } // end of If Telemetry
    
        else if (packetSize == PartInfo) { // Packet IS PartInfo
    //      Serial.println("***** Received PartInfo ***** ===  = = = = = = = = = = = = = = = = = = =");
          Udp.read(packetBuffer, 1347);
          if (needPartInfo) {
            sendPartInfo();
            needPartInfo = false;    
          }
        } // END OF IF PACKET T or P
       
      } // END OF IF PACKET AVAILABLE
    } // END OF MAIN LOOP
    
    
    //============================================================================
    //=  Send DashSetup D0 packet                                                 
    //============================================================================
    void sendDashSetup() {
    //  Serial.println("*************************************  Sending DashSetup D0");
      // Prepare D0
      D0[3] = packetBuffer[11];     // LapsInEvent
      D0[4] = packetBuffer[111];    // FuelCapacity
      D0[5] = packetBuffer[126];    // MaxRpm 1st
      D0[6] = packetBuffer[127];    // MaxRpm 2nd  
    
      // Send D0
      delay(1);
      Serial.write(D0, 8);
    }
    
    //============================================================================
    //=  Send a Dash 1/2/3/9 packet -                                                 
    //============================================================================
    void sendDash() {
    //  Serial.println("*************************************  Sending Dash D1");
      // Prepare D1 Dash packet
      D1[3] = packetBuffer[2];      // PacketNo
      D1[4] = packetBuffer[3];      // GameState
      D1[5] = packetBuffer[10];     // RaceState
      D1[6] = packetBuffer[110];    // CarFlags  
      D1[7] = packetBuffer[20];     // CurrentTime 1st
      D1[8] = packetBuffer[21];     // CurrentTime 2nd
      D1[9] = packetBuffer[22];     // CurrentTime 3rd
      D1[10] = packetBuffer[23];    // CurrentTime 4th
      D1[11] = packetBuffer[120];   // Speed 1st
      D1[12] = packetBuffer[121];   // Speed 2nd 
      D1[13] = packetBuffer[122];   // Speed 3rd 
      D1[14] = packetBuffer[123];   // Speed 4th  
      D1[15] = packetBuffer[124];   // Rpm 1st   
      D1[16] = packetBuffer[125];   // Rpm 2nd
      D1[17] = packetBuffer[128];   // GearNumGears 
      D1[18] = packetBuffer[129];   // Boost 
      D1[19] = packetBuffer[448];   // EngineSpeed 1st   
      D1[20] = packetBuffer[449];   // EngineSpeed 2nd  
      D1[21] = packetBuffer[450];   // EngineSpeed 3rd
      D1[22] = packetBuffer[451];   // EngineSpeed 4th
      D1[23] = packetBuffer[452];   // Torque 1st 
      D1[24] = packetBuffer[453];   // Torque 2nd  
      D1[25] = packetBuffer[454];   // Torque 3rd  
      D1[26] = packetBuffer[455];   // Torque 4th
      D1[27] = packetBuffer[280];   // TyreGrip[0] 
      D1[28] = packetBuffer[281];   // TyreGrip[1]
      D1[29] = packetBuffer[282];   // TyreGrip[2]  
      D1[30] = packetBuffer[283];   // TyreGrip[3]  
    
      // Send D1
      delay(1);
      Serial.write(D1, 32);
    
      // D2 Dash packet Timer  
      unsigned long currentMillisD2 = millis();
      if (currentMillisD2 - D2oldtime > D2delay) {
        D2oldtime = currentMillisD2;
    //    Serial.println("*************************************                 Sending Dash D2");
        // Prepare D2 Dash packet
        D2[3] = packetBuffer[100];      // OilTemp 1st
        D2[4] = packetBuffer[101];      // OilTemp 2nd
        D2[5] = packetBuffer[102];     // OilPress 1st
        D2[6] = packetBuffer[103];    // OilPress 2nd  
        D2[7] = packetBuffer[104];     // WaterTemp 1st
        D2[8] = packetBuffer[105];     // Watertemp 2nd
        D2[9] = packetBuffer[106];     // WaterPress 1st
        D2[10] = packetBuffer[107];    // WaterPress 2nd
        D2[11] = packetBuffer[108];   // FuelPress 1st
        D2[12] = packetBuffer[109];   // FuelPress 2nd 
        D2[13] = packetBuffer[116];   // FuelLevel 1st 
        D2[14] = packetBuffer[117];   // FuelLevel 2nd  
        D2[15] = packetBuffer[118];   // FuelLevel 3rd   
        D2[16] = packetBuffer[119];   // FuelLevel 4th
        D2[17] = packetBuffer[130];   // EnforcedPit 
        D2[18] = packetBuffer[131];   // CrashState 
        D2[19] = packetBuffer[132];   // OdoKM 1st   
        D2[20] = packetBuffer[133];   // OdoKM 2nd  
        D2[21] = packetBuffer[134];   // OdoKM 3rd
        D2[22] = packetBuffer[135];   // OdoKM 4th
        D2[23] = packetBuffer[456];   // AeroDamage 
        D2[24] = packetBuffer[457];   // EngineDamage  
      
        // Send D2
        delay(1);
        Serial.write(D2, 26);
      }
    
      // D3 Dash packet Timer  
      unsigned long currentMillisD3 = millis();
      if (currentMillisD3 - D3oldtime > D3delay) {
        D3oldtime = currentMillisD3;
    //    Serial.println("*************************************                                   Sending Dash D3");
        // Prepare D3 Dash packet
        D3[3] = packetBuffer[12];      // BestLapTime 1st
        D3[4] = packetBuffer[13];      // BestLapTime 2nd
        D3[5] = packetBuffer[14];      // BestLapTime 3rd
        D3[6] = packetBuffer[15];      // BestLapTime 4th  
        D3[7] = packetBuffer[16];      // LastLapTime 1st
        D3[8] = packetBuffer[17];      // LastLapTime 2nd
        D3[9] = packetBuffer[18];      // LastLapTime 3rd
        D3[10] = packetBuffer[19];     // LastLapTime 4th
        D3[11] = packetBuffer[40];     // FastestLapTime 1st
        D3[12] = packetBuffer[41];     // FastestLapTime 2nd 
        D3[13] = packetBuffer[42];     // FastestLapTime 3rd 
        D3[14] = packetBuffer[43];     // FastestLapTime 4th  
    
        // Send D3
        delay(1);
        Serial.write(D3, 16);
      }
    
    }
    
    //============================================================================
    //=  Send a P0, P1 and P2 packet - PartInfo                                                
    //============================================================================
    void sendPartInfo() {
    //  Serial.println("*************************************************************========================  Sending PartInfo P0");
      if (inGame) {
      // P0 - CarName. in[3] out[3]
        for (int incount = 3; incount < 67; incount++) {
          P0[incount] = packetBuffer[incount];
        }  
        Serial.write(P0, 68);
    
      // P1 - Class. in[67] out[3]
        for (int incount = 3; incount < 67; incount++) {
          P1[incount] = packetBuffer[(incount+64)];
        }  
        Serial.write(P1, 68);
      
      // P2 - Track. in[131] out[3]
        for (int incount = 3; incount < 67; incount++) {
          P2[incount] = packetBuffer[(incount+128)];
        }  
        Serial.write(P2, 68);
      } 
    }
    
    
    // END OF PROGRAM

    I've left the '//' serial.print debugging in there, so you can a) see my thinking, b) use them yourself
    And.. As before, you need to replace my network SSID and Password, with your own...

    Luv
    Chrisie.. xx
    The following 2 users likes this Post: GForce, memoric77


  8. #38
    Kart Driver
    Join Date
    Feb 2016
    Location
    ERR404
    Posts
    77
    Platform
    PS4
    Now... Lets see if we can get our heads around the floats...

    Here's the Teensy code for receiving the packets from the ESP.. NOTE- Its broken into .h and .ino's for east reading.. If you copy this, just put each of the extra .h and .ino's into the same folder as the sketch..

    Main Program - Teensy_Rx_V5_3.ino

    Code:
    /*
     ======================================
     = Teensy RX V5.3 (Pcars_udp_rx_v5.3) =
     ======================================
    
      Version Control
      ===============
      V1 - Copy of serialEvent example, but completely changed.
      V4.01 - Waits for 'startofpacket', then reads next byte (hijacked)
              Then, now on byte 2, reads fixed length buffer into 
              temp buffer (one less than original, cuz its read twice
              Then.. Moves temp buffer to full buffer
      V5.3 - Use ReadbyteUntil        
    
    
            
      Program Desciption
      ==================
      Reads Serial2 for startofpacket ("<")
      Moves next 1367 bytes to inBuffer, unless EOF (">")
      Once a full packet is received
        Test for packet type using hijacked byte [0].
        "P" = PartInfo
        "T" = Telemetry
      
      I/Os 
      ==== 
      ESP8266 incoming on Serial2 (Tx=10,Rx=9).
    
    */
    
    //============================================================================
    //= Libraries
    //============================================================================
    // Included so 'byte' stops getting 'does not name a type' errors - (Only required because i'm using a Teensy)
    #include <arduino.h>
    
    // Serial Data in - Cut down to required fields
    #include "Data_in.h"
    
    // Data Extract - fields that Data-in is moved to
    #include "Data_Extract.h"
    
    // Extra flags not received. For Dashboard setup/control
    #include "extra_flags.h"
    
    //============================================================================
    //= Setup Definitions
    //============================================================================
      // None yet
    
    //============================================================================
    //= Storage section
    //============================================================================
    // Incoming Buffer
    byte inBuffer[128];          //buffer to hold incoming packet
    byte inByte;
    char inChar;
    
    bool newPacket = false;
    
    
    //============================================================================
    //= Setup objects
    //============================================================================
      // None yet
    
    //============================================================================
    //= Setup
    //============================================================================
    void setup() {
      // initialize serial: For Serial monitor
      Serial.begin(115200);
      delay(1000);
      // initialize serial2: Incoming Data
      Serial2.begin(115200);
      delay(1000);
    
      Serial.println("***code starting");
    
    }
    
    //===============
    // first attempt at a union
    union {
      byte uBytes[4];
      float uFloat;
      long uLong;
    } uData;
    
    union {
      byte sBytes[4];
      float sFloat;
      long sLong;
    } sData;
    
    
    //============================================================================
    //= Main Loop
    //============================================================================
    void loop() {
    // Input serial information:
      if (Serial2.available() > 0){
        Serial2.readBytesUntil(endOfPacket, inBuffer, 128);
        inChar = char(inBuffer[1]);
        Serial.print(inChar);
        Serial.println(inBuffer[2]);
        switch (inChar) {
          case 'C':
            unpackConnectInfo();
            // 
            break;
          case 'P': // Received a PartInfo packet
            if (inBuffer[2] < 3) { // Stop spurious P97 packet??
              unpackPartInfo(); 
              clearInBuffer();
            }
            break;
          case 'D':
            unpackDash();
            clearInBuffer();
            break;
          case 'S':
            unpackSusp();
            break;
          case 'M':
            unpackMotion();
            break;
        }        
        // clear inBuffer 'PacketType' - see if it stops spurious 8097 packet
    //    inBuffer[1] = 0;
    //    inBuffer[2] = 0;
      }
    }    
        
    
    //============================================================================
    //= Unpack Dashboard
    //============================================================================
    void unpackDash() {
    
      switch (inBuffer[2]) { // packetNum
        case 0: // Setup D0
          Serial.println("");
          LapsInEvent = inBuffer[3];
          FuelCapacity = inBuffer[4];
          MaxRpm = word(inBuffer[6], inBuffer[5]);
          Serial.print("LapsInEvent : "); Serial.println(LapsInEvent);
          Serial.print("FuelCapacity : "); Serial.println(FuelCapacity);
          Serial.print("MaxRpm : "); Serial.println(MaxRpm);
          Serial.println("");
          break;
        case 1: // Main D1
          Serial.println("++");
          PacketNum = inBuffer[3];
          GameState = inBuffer[4];
          RaceState = inBuffer[5];
          CarFlags = inBuffer[6];
          //Set Flags from Bits..
          if (bitRead(CarFlags, 0)) cfLights = true;
          else cfLights = false;
          if (bitRead(CarFlags, 4)) cfAbs = true;
          else cfAbs = false;
          if (bitRead(CarFlags, 6)) cfStab = true;
          else cfStab = false;
          if (bitRead(CarFlags, 7)) cfTrac = true;
          else cfTrac = false;
          Rpm = word(inBuffer[16], inBuffer[15]);
          GearNumGears = inBuffer[17];
          currentGear = GearNumGears & 0xf; // & GearNumGears with 00001111(15) to get value of Low4 bits 
          maxGears = GearNumGears >> 4;     // Shift GearNumGears 4 bits Right. Shift out lower 4, leaving only Top 4 bits
         
          BoostAmount = inBuffer[18];
          TyreGrip[0] = inBuffer[27];      
          TyreGrip[1] = inBuffer[28];      
          TyreGrip[2] = inBuffer[29];      
          TyreGrip[3] = inBuffer[30];      
    
    //debug time
    //      Serial.print("inBuffer[7] : "); Serial.print(inBuffer[7],DEC);
    //      Serial.print(" ");Serial.println(inBuffer[7],BIN);
    //      Serial.print("inBuffer[8] : "); Serial.print(inBuffer[8],DEC);
    //      Serial.print(" ");Serial.println(inBuffer[8],BIN);
    //      Serial.print("inBuffer[9] : "); Serial.print(inBuffer[9],DEC);
    //      Serial.print(" ");Serial.println(inBuffer[9],BIN);
    //      Serial.print("inBuffer[10] : "); Serial.print(inBuffer[10],DEC);
    //      Serial.print(" ");Serial.println(inBuffer[10],BIN);
    
          // Move 4 float fields
          CurrentTime = (inBuffer[10]) << 24 | (inBuffer[9] & 0xff) << 16 | (inBuffer[8] & 0xff) << 8 | (inBuffer[7] & 0xff);
          Speed = (inBuffer[14]) << 24 | (inBuffer[13] & 0xff) << 16 | (inBuffer[12] & 0xff) << 8 | (inBuffer[11] & 0xff);
          EngineSpeed = (inBuffer[22]) << 24 | (inBuffer[21] & 0xff) << 16 | (inBuffer[20] & 0xff) << 8 | (inBuffer[19] & 0xff);
          EngineTorque = (inBuffer[26]) << 24 | (inBuffer[25] & 0xff) << 16 | (inBuffer[24] & 0xff) << 8 | (inBuffer[23] & 0xff);
    // debug speed
          Serial.print("inBuffer[11] : "); Serial.print(inBuffer[11],DEC);
          Serial.print(" ");Serial.println(inBuffer[11],BIN);
          Serial.print("inBuffer[12] : "); Serial.print(inBuffer[12],DEC);
          Serial.print(" ");Serial.println(inBuffer[12],BIN);
          Serial.print("inBuffer[13] : "); Serial.print(inBuffer[13],DEC);
          Serial.print(" ");Serial.println(inBuffer[13],BIN);
          Serial.print("inBuffer[14] : "); Serial.print(inBuffer[14],DEC);
          Serial.print(" ");Serial.println(inBuffer[14],BIN);
    
    // reverse the bits in bytes 11-14, then feed to union
          byte byte11Rev = 0;
          if (bitRead(inBuffer[11], 0)) bitSet(byte11Rev, 7);
          if (bitRead(inBuffer[11], 1)) bitSet(byte11Rev, 6);
          if (bitRead(inBuffer[11], 2)) bitSet(byte11Rev, 5);
          if (bitRead(inBuffer[11], 3)) bitSet(byte11Rev, 4);
          if (bitRead(inBuffer[11], 4)) bitSet(byte11Rev, 3);
          if (bitRead(inBuffer[11], 5)) bitSet(byte11Rev, 2);
          if (bitRead(inBuffer[11], 6)) bitSet(byte11Rev, 1);
          if (bitRead(inBuffer[11], 7)) bitSet(byte11Rev, 0);
          byte byte12Rev = 0;
          if (bitRead(inBuffer[12], 0)) bitSet(byte12Rev, 7);
          if (bitRead(inBuffer[12], 1)) bitSet(byte12Rev, 6);
          if (bitRead(inBuffer[12], 2)) bitSet(byte12Rev, 5);
          if (bitRead(inBuffer[12], 3)) bitSet(byte12Rev, 4);
          if (bitRead(inBuffer[12], 4)) bitSet(byte12Rev, 3);
          if (bitRead(inBuffer[12], 5)) bitSet(byte12Rev, 2);
          if (bitRead(inBuffer[12], 6)) bitSet(byte12Rev, 1);
          if (bitRead(inBuffer[12], 7)) bitSet(byte12Rev, 0);
          byte byte13Rev = 0;
          if (bitRead(inBuffer[13], 0)) bitSet(byte13Rev, 7);
          if (bitRead(inBuffer[13], 1)) bitSet(byte13Rev, 6);
          if (bitRead(inBuffer[13], 2)) bitSet(byte13Rev, 5);
          if (bitRead(inBuffer[13], 3)) bitSet(byte13Rev, 4);
          if (bitRead(inBuffer[13], 4)) bitSet(byte13Rev, 3);
          if (bitRead(inBuffer[13], 5)) bitSet(byte13Rev, 2);
          if (bitRead(inBuffer[13], 6)) bitSet(byte13Rev, 1);
          if (bitRead(inBuffer[13], 7)) bitSet(byte13Rev, 0);
          byte byte14Rev = 0;
          if (bitRead(inBuffer[14], 0)) bitSet(byte14Rev, 7);
          if (bitRead(inBuffer[14], 1)) bitSet(byte14Rev, 6);
          if (bitRead(inBuffer[14], 2)) bitSet(byte14Rev, 5);
          if (bitRead(inBuffer[14], 3)) bitSet(byte14Rev, 4);
          if (bitRead(inBuffer[14], 4)) bitSet(byte14Rev, 3);
          if (bitRead(inBuffer[14], 5)) bitSet(byte14Rev, 2);
          if (bitRead(inBuffer[14], 6)) bitSet(byte14Rev, 1);
          if (bitRead(inBuffer[14], 7)) bitSet(byte14Rev, 0);
    
          
          Serial.print("byte11Rev      : "); Serial.println(byte11Rev,BIN);
          Serial.print("byte12Rev      : "); Serial.println(byte12Rev,BIN);
          Serial.print("byte13Rev      : "); Serial.println(byte13Rev,BIN);
          Serial.print("byte14Rev      : "); Serial.println(byte14Rev,BIN);
    
          
          uData.uBytes[0] = inBuffer[11];
          uData.uBytes[1] = inBuffer[12];
          uData.uBytes[2] = inBuffer[13];
          uData.uBytes[3] = inBuffer[14];
    
          sData.sBytes[0] = inBuffer[19];
          sData.sBytes[1] = inBuffer[20];
          sData.sBytes[2] = inBuffer[21];
          sData.sBytes[3] = inBuffer[22];
    
    
    //      uData.uBytes[0] = byte14Rev;
    //      uData.uBytes[1] = byte13Rev;
    //      uData.uBytes[2] = byte12Rev;
    //      uData.uBytes[3] = byte11Rev;
    
          Serial.print("uBytes  [0]-[11] : "); Serial.println(uData.uBytes[0]);
          Serial.print("uBytes  [1]-[12] : "); Serial.println(uData.uBytes[1]);
          Serial.print("uBytes  [2]-[13] : "); Serial.println(uData.uBytes[2]);
          Serial.print("uBytes  [3]-[14] : "); Serial.println(uData.uBytes[3]);
          Serial.print("uFloat      : "); Serial.println(uData.uFloat,3);
          Serial.print("uLong       : "); Serial.println(uData.uLong);
    
          Serial.print("sBytes  [0]-[19] : "); Serial.println(sData.sBytes[0]);
          Serial.print("sBytes  [1]-[20] : "); Serial.println(sData.sBytes[1]);
          Serial.print("sBytes  [2]-[21] : "); Serial.println(sData.sBytes[2]);
          Serial.print("sBytes  [3]-[22] : "); Serial.println(sData.sBytes[3]);
          Serial.print("sfloat      : "); Serial.println(sData.sFloat,3);
          Serial.print("sLong       : "); Serial.println(sData.sLong);
    
    // debug
          Serial.print("PacketNum : "); Serial.println(PacketNum);
    //      Serial.print("GameState : "); Serial.println(GameState);
    //      Serial.print("RaceState : "); Serial.println(RaceState);
    //      Serial.print("CarFlags : "); Serial.println(CarFlags);
    
          if (cfAbs) Serial.println("                                 ABS");
          if (cfStab) Serial.println("                                         Stability");
          if (cfTrac) Serial.println("                                                      Traction Control");
          
          Serial.print("Rpm : "); Serial.println(Rpm);
    //      Serial.print("GearNumGears : "); Serial.println(GearNumGears);
    //      Serial.print("BoostAmount : "); Serial.println(BoostAmount);
    //      Serial.print("TyreGrip[0] : "); Serial.println(TyreGrip[0]);
    //      Serial.print("TyreGrip[1] : "); Serial.println(TyreGrip[1]);
    //      Serial.print("TyreGrip[2] : "); Serial.println(TyreGrip[2]);
    //      Serial.print("TyreGrip[3] : "); Serial.println(TyreGrip[3]);
          Serial.print("CurrentTime : "); Serial.println(CurrentTime,3);
          Serial.print("Speed : "); Serial.println(Speed,3);
          Serial.print("EngineSpeed : "); Serial.println(EngineSpeed,3);
          Serial.print("EngineTorque : "); Serial.println(EngineTorque,3);
          Serial.print("maxGears : "); Serial.println(maxGears);
          Serial.print("currentGear : "); Serial.println(currentGear);
    
          
          
          Serial.println("");
          break;
    //    case 2:
    //      Serial.println("++++Received D2 ");
    //      break;
    //    case 3:
    //      Serial.println("++++Received D3 ");
    //      break;
      }
    }
    
    
    
    
    //============================================================================
    //= Unpack Suspension
    //============================================================================
    void unpackSusp() {
    
    
    }
    
    //============================================================================
    //= Unpack Motion
    //============================================================================
    void unpackMotion() {
    
    
    }
    
    //============================================================================
    //= Unpack Motion
    //============================================================================
    void clearInBuffer() {
      for (int countCLR = 0; countCLR < 128; countCLR++) {
        inBuffer[countCLR] = 0;
      }
    
    }
    
    // END OF CODE ================================================
    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  
    //  = = = = = = = = = = = = = = = = = = = = = = = = = = = = =




    I've left a whole heap of debugging serial prints in, all for the float fields...

    The data that is a 'byte' is just copied byte-byte and works fine..
    Ints are combined from 2 bytes, and work fine...

    Floats are 4 bytes and are NOT fine...

    I've tried

    float = byte0, byte1, byte2, byte3.

    float = byte3, byte2, byte1, byte0.

    float = byte1, byte0, byte3, byte2.

    float = byte3, byte2, byte1, byte0.

    THEN - reversed the bits in each byte ( 00010011 = 11001000) - BigEndian/LittleEndian..?????

    and tried it all again...

    Lots of rubbish looking results..


    On a plus note, I have found and used a 'Union' which looks like it could be very useful, once its sorted... Note how it's defined BETWEEN setup and loop.. I've never done that before, but it works..
    Last edited by cyclicscooby; 24-02-2016 at 15:12.
    The following user likes this Post: GForce


  9. #39
    Kart Driver
    Join Date
    Feb 2016
    Location
    ERR404
    Posts
    77
    Platform
    PS4
    Data_Extract.h - To put extracted data into

    Code:
    //===============================
    // C0 - Connection Info
    //byte C0[4];
    //char startOfPacket = <             // 0
    //char ConnectionC0                  // 1
    //byte ConnectionC0Num               // 2
    //char endOfPacket = >               // 3
    // 4 bytes
    //===============================
    // P0 - PartInfo_0 CarName -  Have to send in 3 packets,cuz serial.write will overflow 128 buffer
    //byte P0[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '0'
    char  CarName[64];                 // 3   // 3
    //char endOfPacket = >               // 67 
    // 68 byte
    
    //===============================
    // P1 - PartInfo_1 Class
    //byte P1[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '1'
    char  CarClassName[64];            // 3   // 67
    //char endOfPacket = >               // 67
    // 68 byte
    
    //===============================
    // P2 - PartInfo_2 Track
    //byte P2[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '2'
    char  TrackLocation[64];           // 3   // 131
    //char endOfPacket = >               // 67
    // 68 byte
    
    //===============================
    // D0 - Dashboard_0 - Startup parameters - Send ONCE at start of race
    //byte D0[8];
    //char startOfPacket = <             // 0
    //char  Dash_D0;                     // 1   // 0 = 'D'
    //byte  Dash_D0Num                   // 2   // 1 = '0'
    byte LapsInEvent;                  // 3   // 11   
    byte FuelCapacity;                 // 4   // 111  
    unsigned int MaxRpm;               // 5   // 126  
    //char endOfPacket = >               // 7
    // 8 bytes
    
    //===============================
    // D1 - Dashboard_1
    //byte D1[32];
    //char startOfPacket = <             // 0
    //char  Dash_D1;                     // 1   // 0 = 'D'
    //byte  Dash_D1Num                   // 2   // 1 = '1'
    byte PacketNum;                    // 3   // 2    
    byte GameState;                    // 4   // 3    
    byte RaceState;                    // 5   // 10
    byte CarFlags;                     // 6   // 110
    float CurrentTime;                 // 7   // 20   
    float Speed;                       // 11  // 120  
    unsigned int Rpm;                  // 15  // 124  
    byte GearNumGears;                 // 17  // 128  
    byte BoostAmount;                  // 18  // 129  
    float EngineSpeed;                 // 19  // 448  
    float EngineTorque;                // 23  // 452  
    byte TyreGrip[4];                  // 27  // 280  
    //char endOfPacket = >               // 31
    // 32 bytes
    
    //===============================
    // D2 - Dashboard_2
    //byte D2[26];
    //char startOfPacket = <             // 0
    //char  Dash_D2;                     // 1   // 0 = 'D'
    //byte  Dash_D2Num                   // 2   // 1 = '2'
    int OilTempCelsius;                // 3   // 100  
    unsigned int OilPressureKPa;       // 5   // 102  
    int WaterTempCelsius;              // 7   // 104  
    unsigned int WaterPressureKpa;     // 9   // 106  
    unsigned int FuelPressureKpa;      // 11   // 108  
    float FuelLevel;                   // 13   // 116  
    char EnforcedPitStopLap;           // 17   // 130  
    byte CrashState;                   // 18   // 131  
    float OdometerKM;                  // 19   // 132 
    byte AeroDamage;                   // 23   // 456 
    byte EngineDamage;                 // 24   // 457 
    //char endOfPacket = >               // 25
    // 26 bytes
    
    //===============================
    // D3 - Dashboard_3
    //byte D3[16];
    //char startOfPacket = <             // 0 
    //char  Dash_D3;                     // 1   // 0 = 'D'
    //byte  Dash_D3Num                   // 2   // 1 = '3'
    float BestLapTime;                 // 3   // 12   
    float LastLapTime;                 // 7   // 16   
    float PersonalFastestLapTime;      // 11  // 40   
    //char endOfPacket = >               // 15
    //16 bytes
    
    //===============================
    // D9 - Dashboard_9 - OnChange - Send if data changes
    //byte D9[15];
    //char startOfPacket = <             // 0
    //char  Dash_D9;                     // 1   // 0 = 'D'
    //byte  Dash_D9Num                   // 2   // 1 = '9'
    byte BrakeDamage[4];               // 3   // 320  
    byte SuspensionDamage[4];          // 7   // 324  
    byte DPad;                         // 11  // 1366     
    unsigned int JoyPad;               // 12   // 96   
    //char endOfPacket = >               // 14
    // 15bytes
    
    //===============================
    // S1 - Suspension - Send EVERY packet
    //byte S1[84];
    //char startOfPacket = <             // 0
    //char  Dash_S1;                     // 1   // 0 = 'S'
    //byte  Dash_S1Num                   // 2   // 1 = '1'
    float TyreHeightAboveGround[4];    // 3   // 284  
    float WheelLocalPositionY[4];      // 19  // 376  
    float RideHeight[4];               // 35  // 392  
    float SuspensionTravel[4];         // 51  // 408  
    float SuspensionVelocity[4];       // 67  // 424  
    //char endOfPacket = >               // 83
    // 84 bytes
    
    //===============================
    // M1 - Motion - Send EVERY packet (WHEN READY i.e. building new seat)
    //byte M1[124];
    //char startOfPacket = <             // 0
    //char  Dash_M1;                     // 1   // 0 = 'M'
    //byte  Dash_M1Num                   // 2   // 1 = '1'
    float Orientation[3];              // 3   // 136  
    float LocalVelocity[3];            // 15  // 148  
    float WorldVelocity[3];            // 27  // 160  
    float AngularVelocity[3];          // 39  // 172  
    float LocalAcceleration[3];        // 51  // 184  
    float WorldAcceleration[3];        // 63  // 196  
    float ExtentsCentre[3];            // 75  // 208  
    byte Terrain[4];                   // 87  // 224  
    float TyreSlipSpeed[4];            // 91  // 260  
    float TyreY[4];                    // 107 // 228  
    //char endOfPacket = >               // 123
    // 124 bytes

    Data_in.h - Incoming packet definitions

    Code:
    // Project Cars UDP data structure
    /*
    Output fields
    P0 = PartInfo - Sent ONLY once, at start of race.
    D1, D2, D3, D9, D0 = Dashboard - 1=Every Packet, 2= 1 in 5/10
                                     3= 1 in 50, 9=ChangeState, 0=Start only(P1,P2)
    S1 = Suspension Data - Sent EVERY packet
    
    M1,M2,MX, = Motion - XYZ data - 1=Every packet, 2= 1 in 5/10, 9=Change                                  
    
    */
    
    //char packetType; // Used to Write Packet Type Out
    char packetIsConnectInfo = 'C';
    char packetIsPartInfo = 'P';
    char packetIsDash = 'D';
    char packetIsSusp = 'S';
    char packetIsMotion = 'M';
    
    //char startOfPacket = '<';  // 60 = '<'
    //char endOfPacket = '>';  // 62 = '>'
    byte startOfPacket = 60;  // 60 = '<'
    byte endOfPacket = 62;  // 62 = '>'
    
    // Used to Write Packet Number Out
    byte packetNum0 = 0;   // Startup Packet - Only sent at start of Race
    byte packetNum1 = 1;   // Primary data stream. Sent Every packet
    byte packetNum2 = 2;   // Secondary stream. Sent 1 in 5/10 times
    byte packetNum3 = 3;   // 3rd stream. Sent 1 in 50/100 times
    byte packetNum9 = 9;   // Change - Sent on ChangeState
    // Connection Packet byte 2
    byte C0Disconnect = 0;
    byte C0Attempt1 = 1;
    byte C0Attempt2 = 2;
    byte C0Connect = 3;
     
    
    //===============================
    // C0 - Connection Info
    byte C0[4];
    //char startOfPacket = <             // 0
    //char ConnectionC0                  // 1
    //byte ConnectionC0Num               // 2
    //char endOfPacket = >               // 3
    // 4 bytes
    //===============================
    // P0 - PartInfo_0 CarName -  Have to send in 3 packets,cuz serial.write will overflow 128 buffer
    byte P0[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '0'
    //char  CarName[64];                 // 3   // 3
    //char endOfPacket = >               // 67 
    // 68 byte
    
    //===============================
    // P1 - PartInfo_1 Class
    byte P1[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '1'
    //char  CarClassName[64];            // 3   // 67
    //char endOfPacket = >               // 67
    // 68 byte
    
    //===============================
    // P2 - PartInfo_2 Track
    byte P2[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '2'
    //char  TrackLocation[64];           // 3   // 131
    //char endOfPacket = >               // 67
    // 68 byte
    
    //===============================
    // D0 - Dashboard_0 - Startup parameters - Send ONCE at start of race
    byte D0[8];
    //char startOfPacket = <             // 0
    //char  Dash_D0;                     // 1   // 0 = 'D'
    //byte  Dash_D0Num                   // 2   // 1 = '0'
    //byte LapsInEvent;                  // 3   // 11   
    //byte FuelCapacity;                 // 4   // 111  
    //unsigned int MaxRpm;               // 5   // 126  
    //char endOfPacket = >               // 7
    // 8 bytes
    
    //===============================
    // D1 - Dashboard_1
    byte D1[32];
    //char startOfPacket = <             // 0
    //char  Dash_D1;                     // 1   // 0 = 'D'
    //byte  Dash_D1Num                   // 2   // 1 = '1'
    //byte PacketNum;                    // 3   // 2    
    //byte GameState;                    // 4   // 3    
    //byte RaceState;                    // 5   // 10
    //byte CarFlags;                     // 6   // 110
    //float CurrentTime;                 // 7   // 20   
    //float Speed;                       // 11  // 120  
    //unsigned int Rpm;                  // 15  // 124  
    //byte GearNumGears;                 // 17  // 128  
    //byte BoostAmount;                  // 18  // 129  
    //float EngineSpeed;                 // 19  // 448  
    //float EngineTorque;                // 23  // 452  
    //byte TyreGrip[4];                  // 27  // 280  
    //char endOfPacket = >               // 31
    // 32 bytes
    
    //===============================
    // D2 - Dashboard_2
    byte D2[26];
    //char startOfPacket = <             // 0
    //char  Dash_D2;                     // 1   // 0 = 'D'
    //byte  Dash_D2Num                   // 2   // 1 = '2'
    //int OilTempCelsius;                // 3   // 100  
    //unsigned int OilPressureKPa;       // 5   // 102  
    //int WaterTempCelsius;              // 7   // 104  
    //unsigned int WaterPressureKpa;     // 9   // 106  
    //unsigned int FuelPressureKpa;      // 11   // 108  
    //float FuelLevel;                   // 13   // 116  
    //char EnforcedPitStopLap;           // 17   // 130  
    //byte CrashState;                   // 18   // 131  
    //float OdometerKM;                  // 19   // 132 
    //byte AeroDamage;                   // 23   // 456 
    //byte EngineDamage;                 // 24   // 457 
    //char endOfPacket = >               // 25
    // 26 bytes
    
    //===============================
    // D3 - Dashboard_3
    byte D3[16];
    //char startOfPacket = <             // 0 
    //char  Dash_D3;                     // 1   // 0 = 'D'
    //byte  Dash_D3Num                   // 2   // 1 = '3'
    //float BestLapTime;                 // 3   // 12   
    //float LastLapTime;                 // 7   // 16   
    //float PersonalFastestLapTime;      // 11  // 40   
    //char endOfPacket = >               // 15
    //16 bytes
    
    //===============================
    // D9 - Dashboard_9 - OnChange - Send if data changes
    byte D9[15];
    //char startOfPacket = <             // 0
    //char  Dash_D9;                     // 1   // 0 = 'D'
    //byte  Dash_D9Num                   // 2   // 1 = '9'
    //byte BrakeDamage[4];               // 3   // 320  
    //byte SuspensionDamage[4];          // 7   // 324  
    //byte DPad;                         // 11  // 1366     
    //unsigned int JoyPad;               // 12   // 96   
    //char endOfPacket = >               // 14
    // 15bytes
    
    //===============================
    // S1 - Suspension - Send EVERY packet
    byte S1[84];
    //char startOfPacket = <             // 0
    //char  Dash_S1;                     // 1   // 0 = 'S'
    //byte  Dash_S1Num                   // 2   // 1 = '1'
    //float TyreHeightAboveGround[4];    // 3   // 284  
    //float WheelLocalPositionY[4];      // 19  // 376  
    //float RideHeight[4];               // 35  // 392  
    //float SuspensionTravel[4];         // 51  // 408  
    //float SuspensionVelocity[4];       // 67  // 424  
    //char endOfPacket = >               // 83
    // 84 bytes
    
    //===============================
    // M1 - Motion - Send EVERY packet (WHEN READY i.e. building new seat)
    byte M1[124];
    //char startOfPacket = <             // 0
    //char  Dash_M1;                     // 1   // 0 = 'M'
    //byte  Dash_M1Num                   // 2   // 1 = '1'
    //float Orientation[3];              // 3   // 136  
    //float LocalVelocity[3];            // 15  // 148  
    //float WorldVelocity[3];            // 27  // 160  
    //float AngularVelocity[3];          // 39  // 172  
    //float LocalAcceleration[3];        // 51  // 184  
    //float WorldAcceleration[3];        // 63  // 196  
    //float ExtentsCentre[3];            // 75  // 208  
    //byte Terrain[4];                   // 87  // 224  
    //float TyreSlipSpeed[4];            // 91  // 260  
    //float TyreY[4];                    // 107 // 228  
    //char endOfPacket = >               // 123
    // 124 bytes
    extra_flags.h

    Code:
    //=================================
    // DashBoard Extra flags. Not received, just for processing here
    bool ignitionOn = false;    // Read RPM - If last rpm was 0, now >0  Ign ON
    bool startUp = false;       // Use to run dash startup seq
    bool lightsOn = false;      // read from carFlags
    bool absOn = false;         // read from carFlags - 3 bits in carFlags change  
    bool tractionOn = false;    // read from carFlags - find out which is which
    bool stabilityOn = false;   // read from carFlags - if its there..
    
    // RPM = int, enginespeed = float. Not sure which yet
    //float lastRPM = 0;          // Hold last RPM value
    //int lastRPM = 0;          // Hold last RPM value
    
    //=================================
    // wifiLED status. No connect=Off, Attempt=Flash, Connected=Solid
    bool wifiLEDattempt = false; // WiFi Status - attempting - Flash
    bool wifiLEDconnect = false; // WiFi Status - connected - Solid
    bool connectS5 = false;
    bool connectPaul = false;
    
    long currentTimeLong = 0;
    long speedLong = 0;
    long engineSpeedLong = 0;
    long engineTorqueLong = 0;
    
    byte maxGears;
    byte currentGear;
    
    bool cfLights = false;
    bool cfAbs = false;
    bool cfStab = false;
    bool cfTrac = false;

    You could easily combine the first two, indeed they are mostly the same thing, but with different fields '//' out.

    I've done it this way for now, to keep 'data hunting' easy..
    Last edited by cyclicscooby; 24-02-2016 at 14:39.
    The following user likes this Post: GForce


  10. #40
    Kart Driver
    Join Date
    Feb 2016
    Location
    ERR404
    Posts
    77
    Platform
    PS4
    I know there are a few fields defined that aren't being used, etc, it's a 'work in progress' version..


    The following ino's are just called procedures - i.e. 'readPartInfo();' that are moved to a separate tab. You can keep them as separate tabs, by putting them in the same folder as the main sketch, OR, just paste them into the bottom of the main sketch, it's the same thing..


    rx_Connect.ino (just 'unpackConnectInfo();' )

    Code:
    //============================================================================
    //= Unpack Connection Info
    //============================================================================
    // Future- Add LED to Teensy or Display on screen
    void unpackConnectInfo() {
       
    //  Serial.print(inBuffer[1]); Serial.println(inBuffer[2]);
    
      // If NO connection (byte 2 == 0)
      if (inBuffer[2] == 0) {
        Serial.println("No Connection");
        connectS5 = false;
        connectPaul = false;
      }
    
      // If Attempting SSID 1 (byte 2 == 1)
      else if (inBuffer[2] == 1) {
        Serial.println("Attempting to connect to S5 HotSpot");
        connectS5 = true; // Set 
        connectPaul = false;
      }
    
      // If Attempting SSID 2 (byte 2 == 2)
      else if (inBuffer[2] == 2) {
        Serial.println("Attempting to connect to Paul HotSpot");
        connectS5 = false;
        connectPaul = true;
      }
      // If Connection Successful (byte 2 == 3) use connect flags to show which
      else if (inBuffer[2] == 3) {
        if (connectS5) Serial.println("Successfully Connected to S5 HotSpot");
        if (connectPaul) Serial.println("Successfully Connected to Paul HotSpot");
      }
        
    } // END OF unpackConnectInfo
    rx_PartInfo.ino

    Code:
    //============================================================================
    //= Unpack PartInfo
    //============================================================================
    void unpackPartInfo() {
    
      // debug - print buffer code
      Serial.print(inBuffer[1]); Serial.println(inBuffer[2]);
    
      switch (inBuffer[2]) {
        case 0:
          for (int count = 0; count < 64; count++) {
            CarName[count] = inBuffer[(count+3)];
          }
          Serial.print("CarName : "); Serial.println(CarName);
          break;
        case 1:
          for (int count = 0; count < 64; count++) {
            CarClassName[count] = inBuffer[(count+3)];
          }
          Serial.print("CarClassName : "); Serial.println(CarClassName);
          break;
        case 2:
          for (int count = 0; count < 64; count++) {
            TrackLocation[count] = inBuffer[(count+3)];
          }
          Serial.print("TrackLocation : "); Serial.println(TrackLocation);
          break;
      }  
    }  // END OF unpackPartInfo


    Thats it so far...

    If you run this and watch the serial during a drive, see if you can make sense of the binary version of the 4bytes of float...?!?!?

    As I said, 'bytes' and 'ints' work fine, and can easily be de-cyphered by watching the binary, but the floats appear to be random..


    I am successfully reading bytes infront AND AFTER the floats, so the serial packet bit is working, otherwise the data behind the floats would also be corrupted/wrong, BUT it's ok, so it must be down to the order of the bits and bytes.. !?!?


    I've spent the last 3-4 days JUST messing with the floats... AAARRGGHHH..!!


    I'm going to concentrate on coding the other data and packets for now, cuz my head is fried...


    Luv
    Chrisie.. xx
    The following user likes this Post: GForce


Similar Threads

  1. [PC/XB1/PS4] vrHive - Dashes/Telemetry/Live Data/Arduino/Client-Server/+more
    By MikeyTT in forum Third-Party Apps & API Discussion
    Replies: 761
    Last Post: 11-10-2019, 11:42
  2. Arduino Dashboard
    By saibot852 in forum General Discussion
    Replies: 219
    Last Post: 04-09-2017, 04:41
  3. Import 3D model in game?
    By choptider in forum PC - Technical Help & Support
    Replies: 4
    Last Post: 25-11-2015, 02:54
  4. Logic3 TopDrive GT racing wheel not detected (Wifi dongle)
    By markieg in forum PC - Technical Help & Support
    Replies: 0
    Last Post: 09-10-2015, 12:12
  5. calling all arduino nuts
    By brooksy in forum General Discussion
    Replies: 2
    Last Post: 25-05-2015, 09:35

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •