1. Do not share user accounts! Any account that is shared by another person will be blocked and closed. This means: we will close not only the account that is shared, but also the main account of the user who uses another person's account. We have the ability to detect account sharing, so please do not try to cheat the system. This action will take place on 04/18/2023. Read all forum rules.
    Dismiss Notice
  2. For downloading SimTools plugins you need a Download Package. Get it with virtual coins that you receive for forum activity or Buy Download Package - We have a zero Spam tolerance so read our forum rules first.

    Buy Now a Download Plan!
  3. Do not try to cheat our system and do not post an unnecessary amount of useless posts only to earn credits here. We have a zero spam tolerance policy and this will cause a ban of your user account. Otherwise we wish you a pleasant stay here! Read the forum rules
  4. We have a few rules which you need to read and accept before posting anything here! Following these rules will keep the forum clean and your stay pleasant. Do not follow these rules can lead to permanent exclusion from this website: Read the forum rules.
    Are you a company? Read our company rules

Request (Feature) Need working code fot FlyPT with single arduino for assetto corsa competizione

Discussion in 'SimTools DIY Version' started by Shashank Patil, Apr 21, 2024.

  1. Shashank Patil

    Shashank Patil New Member

    Joined:
    Dec 7, 2022
    Messages:
    21
    Balance:
    95Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    3DOF, AC motor, Arduino, Motion platform, 6DOF
    hello, ive been working on a servo powered rig model and tried testing assetto using flypt. Either I'm stuck at a power or a code problem. i use a 5v 2a adapter for mg995, mg996r motors, connected them and tried testing using the following code. it gives a late and limited response as shown. set the parameters correctly at flypt


    #include <Servo.h>
    String receivedData = ""; // Variable to store received data
    String interpretedData = ""; // Variable to store interpreted data
    const int DATA_LENGTH = 71; // Number of characters to read
    String dataParts[6]; // Array to store split data
    int a, b, c, d, e, f; // Variables to store split parts as integers
    Servo servoA, servoB, servoC, servoD, servoE, servoF; // Define 6 servo objects
    void setup() {
    Serial.begin(9600); // Initialize serial communication
    servoA.attach(3); // Attach servo A to pin 2
    servoB.attach(5); // Attach servo B to pin 3
    servoC.attach(6); // Attach servo C to pin 4
    servoD.attach(9); // Attach servo D to pin 5
    servoE.attach(10); // Attach servo E to pin 6
    servoF.attach(11); // Attach servo F to pin 7
    }
    void loop() {
    // Check if data is available to read from serial port
    if (Serial.available() > 0) {
    char incomingChar = Serial.read(); // Read incoming character
    // Check if the incoming character is a digit or a space
    if (isdigit(incomingChar) || incomingChar == ' ') {
    receivedData += incomingChar; // Append the character to receivedData
    // Check if receivedData has reached the desired length
    if (receivedData.length() >= DATA_LENGTH) {
    // Interpret the received data
    for (int i = 0; i < receivedData.length(); i += 4) {
    String numString = receivedData.substring(i, i + 3);
    int num = numString.toInt();
    interpretedData += char(num);
    }

    // Remove spaces
    interpretedData.replace(" ", "");

    // Split interpreted data into groups of 3 characters
    int startIndex = 0;
    for (int i = 0; i < 6; ++i) {
    dataParts = interpretedData.substring(startIndex, startIndex + 3);
    startIndex += 3;
    }

    // Convert split parts from string to integer
    a = dataParts[0].toInt();
    b = dataParts[1].toInt();
    c = dataParts[2].toInt();
    d = dataParts[3].toInt();
    e = dataParts[4].toInt();
    f = dataParts[5].toInt();

    // Print the split data to Serial monitor
    Serial.print("a: ");
    Serial.println(a);
    Serial.print("b: ");
    Serial.println(b);
    Serial.print("c: ");
    Serial.println(c);
    Serial.print("d: ");
    Serial.println(d);
    Serial.print("e: ");
    Serial.println(e);
    Serial.print("f: ");
    Serial.println(f);

    // Map the values to the servo range and write to servo motors
    servoA.write(map(a, 0, 1023, 0, 180));
    servoB.write(map(b, 0, 1023, 0, 180));
    servoC.write(map(c, 0, 1023, 0, 180));
    servoD.write(map(d, 0, 1023, 0, 180));
    servoE.write(map(e, 0, 1023, 0, 180));
    servoF.write(map(f, 0, 1023, 0, 180));

    // Reset receivedData, interpretedData, and dataParts for the next iteration
    receivedData = "";
    interpretedData = "";
    for (int i = 0; i < 6; ++i) {
    dataParts = "";
    }
    }
    }
    }

    // Add delay to allow servos to reach their positions
    //delay(100);
    } Screenshot (8).png Servo circuit final.png
  2. GWiz

    GWiz Active Member

    Joined:
    May 12, 2019
    Messages:
    186
    Occupation:
    Dentist
    Location:
    Aberdeenshire, Scotland
    Balance:
    1,498Coins
    Ratings:
    +120 / 0 / -0
    My Motion Simulator:
    DC motor, Arduino, 6DOF
    I've found the following code to work well with FlyPT and be both efficient and relatively easy to understand/adapt:

    Code:
    //********************************************************************************************
    // MM_stewart_test4.ino
    // 6 x RC Model Servos, as used in minature Stewart platform
    // Arduino code written by MadMat for testing 6DOF interface.
    // Use this code to recieve data from SimTools or FlyPT
    //
    // Add info: details about data format
    //           details about servo data
    //
    //********************************************************************************************
    
    #include <Servo.h>
    Servo servo[6];                                 // 6 element servo array, variable name is "servo". Can be any valid variable name.
    int i, j, k;
    int rawServoData[6];                            // 6 element array to hold raw servo data as read from sim.
    int servoData[6];                               // 6 element array to hold servo positoin data ready to write to servos.
    char fromSim;                                   // Data from sim in format: A<Axis1a>B<Axis2a>C<Axis3a>D<Axis4a>E<Axis5a>F<Axis6a>X (eg. A127B127C127D127E127F127X)
    char wasteData[99];                             // when parseInt'íng it was found that the data stream sometimes contained floating points. This wasteData char is a buffer to collect
    
    void setup()
    {
      for (i = 0; i <= 5; i++) {                    // loop to set up 6 servos
        servo[i].attach(i + 4);                     // attach servos 0 - 5 to pins 4 - 9
        servo[i].write(90);                         // set each servo to mid point (90°)
      }
    
      Serial.begin(57600);                          // opens serial port at a baud rate of 57600
      Serial.println("MM_stewart_test4");           // report the current loaded software
    }
    
    
    
    void loop() {
    
      while (Serial.available() > 0) {                // wait for serial data available
        Read_Servo_Data();                            // read the serial data.
        Map_Servo_Data();                             // Function to turn 'rawServoData[]' values into values suitable for your servos.
        Set_Servos2();                                // Function to write the posiditon data 'servoData' to each servo.
        //}
    
      }
      //Serial.println("nothing to see here");
    }
    
    void Read_Servo_Data() {
    
      Serial.readBytesUntil('A', wasteData, 99);      // read data until 'A' is read. Throw the rest away.
      rawServoData[0] = Serial.parseInt();            // read the next bytes of numerical data in the buffer and treat them as an integer to be stored in integer array 'rawServoData[]'.
      Serial.readBytesUntil('B', wasteData, 18);      // This function could be handled in a loop but testing revealed a slightly shorter processing time using this method.
      //Serial.print(rawServoData[0]);
      rawServoData[1] = Serial.parseInt();         
      Serial.readBytesUntil('C', wasteData, 18);
      //Serial.print(rawServoData[1]);
      rawServoData[2] = Serial.parseInt();
      Serial.readBytesUntil('D', wasteData, 18);
      //Serial.print(rawServoData[2]);
      rawServoData[3] = Serial.parseInt();
      Serial.readBytesUntil('E', wasteData, 18);
      //Serial.print(rawServoData[3]);
      rawServoData[4] = Serial.parseInt();
      Serial.readBytesUntil('F', wasteData, 18);
      //Serial.print(rawServoData[4]);
      rawServoData[5] = Serial.parseInt();
      Serial.readBytesUntil('X', wasteData, 18);
      //Serial.print(rawServoData[5]);
    
    }
    
    void Map_Servo_Data() {
      servoData[0] = map(rawServoData[0], 0, 1023, 545, 2398); // Map the 'rawServoData[]' values into values suitable for your servos. Can be into °rotation or pwm values
      servoData[1] = map(rawServoData[1], 0, 1023, 545, 2398); // This function could be handled in a loop but testing revealed a slightly shorter processing time using this method.
      servoData[2] = map(rawServoData[2], 0, 1023, 545, 2398);
      servoData[3] = map(rawServoData[3], 0, 1023, 545, 2398);    // you can also use the min/max values output from "MM_stewart_find_servo_ranges2.ino" here to fine tune.
      servoData[4] = map(rawServoData[4], 0, 1023, 545, 2398);
      servoData[5] = map(rawServoData[5], 0, 1023, 545, 2398);
    
    }
    
    void Set_Servos1() {
      for (j = 0; j <= 5; j++) {                                  // Method 1 (slightly slower):
        servo[j].write(servoData[j]);                             // loop to write the scaled values to each servo.
      }
    }
    
    void Set_Servos2() {
      servo[0].write(servoData[0]);                               // Method 2 (slightly faster):
      servo[1].write(servoData[1]);                               // Write the scaled values to each servo. No loop.
      servo[2].write(servoData[2]);
      servo[3].write(servoData[3]);
      servo[4].write(servoData[4]);
      servo[5].write(servoData[5]);
    }
    
    Use this for the setup data in FlyPT:

    Screenshot 2024-04-22 185019.png
    • Agree Agree x 1
    • Useful Useful x 1
  3. Shashank Patil

    Shashank Patil New Member

    Joined:
    Dec 7, 2022
    Messages:
    21
    Balance:
    95Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    3DOF, AC motor, Arduino, Motion platform, 6DOF
    Worked like a charm ! though need a bit of tweaking on my hardware tho, the servos sometime lock off
    thanks a ton
    • Like Like x 1