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

Question Simtools doesn't work with arduino anymore

Discussion in 'DIY Motion Simulator Building Q&A / FAQ' started by Lebois, Aug 8, 2020.

  1. Lebois

    Lebois (maybe I am wrong, but who knows...)

    Joined:
    Dec 10, 2018
    Messages:
    350
    Occupation:
    Math teacher
    Location:
    France
    Balance:
    2,544Coins
    Ratings:
    +206 / 2 / -0
    My Motion Simulator:
    2DOF
    Hi !

    I am rebuilding 2DOF my motion simulator. It uses an Arduino Uno, but it doesn't work. I am using the same code as last year, and the motor doesn't move. I use Simtools v2.41 (Simtools output : P<Axis1a><Axis2a>, Data bits : 10 bits, Parity : None, stop bits : 1)

    I am using two stepper motors. The code is the following :
    It works as follow :

    SimtoolsReader(); //Get the pitch and roll target from Simtools
    CommandWorker(); //Convert the position targets to pulse numbers (m1stepsToGo and m2stepsToGo
    moveMotor(m1stepsToGo,m2stepsToGo); (move the motors, at each step, we change m1Position and m2Position to keep trace of the position.)

    moveMotor works, the arduino board works (I have another code to manually control the motors), the motors works.

    I think the problem is between Simtools and Arduino...


    Code:
    int numAccelSteps = 10; // 100 is a half turn of a 200 step mmotor
    unsigned long slowMicrosBetweenSteps = 200; // microseconds
    unsigned long fastMicrosBetweenSteps = 100;
    #define directionDelay 10
    
    #define StepPin1  2 //Step or pulse pin, check how DirectionManager is written.
    #define DirPin1   3
    
    #define StepPin2  5
    #define DirPin2   6
    
    int totalStep = 9060;   //number of pulse to go from 0 to max travel
    #include <math.h>
    int commandbuffer[4] = {0};    // To stock the serial datas in the good order.
    
    unsigned int pitchTarget, rollTarget; // The pitch and roll positions we want to reach
    unsigned int m1Position = 0, m2Position = 0;
    int m1stepsToGo=0;
    int m2stepsToGo=0;
    
    unsigned long curMicros;
    unsigned long prevStepMicros = 0;
    unsigned long stepIntervalMicros;
    unsigned long stepAdjustmentMicros;
    
    int c = 0;
    int  dir1 = 1, dir2 = 1;         //Will be used to set the motors direction
    byte dirChange = 0;
    
    void setup() {
      Serial.begin(9600);    //To communicate with Simtools or Monitor
      delay(1000);    //safety delay to flash the code
      pinMode(StepPin1, OUTPUT);
      pinMode(DirPin1, OUTPUT);
      pinMode(StopPin1, INPUT_PULLUP);
      pinMode(StepPin2, OUTPUT);
      pinMode(DirPin2, OUTPUT);
      pinMode(StopPin2, INPUT_PULLUP);
      stepAdjustmentMicros = (slowMicrosBetweenSteps - fastMicrosBetweenSteps) / numAccelSteps;
      stepIntervalMicros = slowMicrosBetweenSteps;
    //  moveMotor();
    
    }
    
    void loop() {
      SimtoolsReader();                     //Get the datas from Simtools
      CommandWorker();                      //Convert the position targets to pulse number
    //  LimitManager();                       // Check if targets are out of reach or not
      moveMotor(m1stepsToGo,m2stepsToGo);
    }
    
    void CommandWorker() {
     m1stepsToGo = pitchTarget - ((m1Position + m2Position) / 2) - ((m1Position + m2Position) / 2) + rollTarget;
     m2stepsToGo = pitchTarget - ((m1Position + m2Position) / 2) + ((m1Position + m2Position) / 2) - rollTarget;
    }
    
    void SimtoolsReader() {       // This function is the work of Sirnoname
      // Simtools output : P<Axis1a><Axis2a>, Data bits : 10 bits, Parity : None, stop bits : 1
      int buffer           =  0 ;    // It takes the value of the serial data
      int buffercount      = -1 ;    // To count where we are in the serial datas
    
      while (Serial.available())
      {
        if (buffercount == -1)
        {
          buffer = Serial.read();
          if (buffer != 'P') {
            buffercount = -1; // "P" is the marquer. If we read P, the next data is pitch
          } else {
            buffercount = 0;
          }
        }
        else   //  if(buffercount>=0)
        {
          buffer = Serial.read();
          commandbuffer[buffercount] = buffer; // The first value next to "P" is saved in commandbuffer in the place "buffercount"
          buffercount++;
          if (buffercount > 3)
          {
            pitchTarget = map((commandbuffer[0]) * 256 + commandbuffer[1], 0, 1023, 0, totalStep);
            rollTarget = map((commandbuffer[2]) * 256 + commandbuffer[3], 0, 1023, 0, totalStep);
            buffercount = -1; // Re-initialize buffercount.  pow(2, precision)
            break;
          }
        }
      }
    }
    
    void directionManager(int step1, int step2) {
      if ((step1 < 0) && (dir1 = 1)) {
        digitalWrite(DirPin1, LOW);
        dir1 = -1;
        dirChange = 1;
        m1stepsToGo = abs(m1stepsToGo);
      }
    
      if ((step1 > 0) && (dir1 = -1)) {
        digitalWrite(DirPin1, HIGH);
        dir1 = 1;
        dirChange = 1;
      }
    
      if ((step2 < 0) && (dir2 = 1)) {
        digitalWrite(DirPin2, LOW);
        dir2 = -1;
        dirChange = 1;
        m2stepsToGo = abs(m2stepsToGo);
      }
    
      if ((step2 > 0) && (dir2 = -1)) {
        digitalWrite(DirPin2, HIGH);
        dir2 = 1;
        dirChange = 1;
      }
    
      if (dirChange = 1) {
        delayMicroseconds(directionDelay);
        dirChange = 0;
      }
    }
    
    void LimitManager() {
      if ( (m1stepsToGo + m1Position > totalStep)  ||  (m1stepsToGo + m1Position) < 50) {
        m1stepsToGo = 0;
      }
      if ( (m2stepsToGo + m2Position > totalStep)  ||  (m2stepsToGo + m2Position) < 50) {
        m2stepsToGo = 0;
      }
    }
    
    void moveMotor(int step1, int step2) {
      directionManager(step1, step2);
      prevStepMicros = micros();
      while (max(step1, step2) > 0) {
        if (micros() - prevStepMicros >= stepIntervalMicros) {
          prevStepMicros += stepIntervalMicros;
          singleStep(step1,step2);
         
          step1 --;
          step2 --;
         
          if (max(step1, step2) <= numAccelSteps) {
            if (stepIntervalMicros < slowMicrosBetweenSteps) {
              stepIntervalMicros += stepAdjustmentMicros;
            }
          }
          else {
            if (stepIntervalMicros > fastMicrosBetweenSteps) {
              stepIntervalMicros -= stepAdjustmentMicros;
            }
          }
        }
      }
    
    }
    
    void singleStep(int step1, int step2) {
      if ((step1 > 0) && (step2 > 0)) {
        digitalWrite(StepPin1, HIGH);
        digitalWrite(StepPin2, HIGH);
        digitalWrite(StepPin1, LOW);
        digitalWrite(StepPin2, LOW);
        m1Position += dir1;
        m2Position += dir2;
    
      }
      else if (step1 > 0) {
        digitalWrite(StepPin1, HIGH);
        digitalWrite(StepPin1, LOW);
        m1Position += dir1;
      }
     
      else {
        digitalWrite(StepPin2, HIGH);
        digitalWrite(StepPin2, LOW);
        m2Position += dir2;
      }
     
    }
  2. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,234
    Location:
    Marseille - FRANCE
    Balance:
    20,890Coins
    Ratings:
    +2,083 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
    > Data bits : 10 bits, Parity : None, stop bits : 1)
    usually, it's 8 bits... are you sure of this setup?
  3. Lebois

    Lebois (maybe I am wrong, but who knows...)

    Joined:
    Dec 10, 2018
    Messages:
    350
    Occupation:
    Math teacher
    Location:
    France
    Balance:
    2,544Coins
    Ratings:
    +206 / 2 / -0
    My Motion Simulator:
    2DOF
    data bits : 8
    Output range : 10

    I tried two different computers, and two arduino boards (Uno and Leonardo...)
  4. Lebois

    Lebois (maybe I am wrong, but who knows...)

    Joined:
    Dec 10, 2018
    Messages:
    350
    Occupation:
    Math teacher
    Location:
    France
    Balance:
    2,544Coins
    Ratings:
    +206 / 2 / -0
    My Motion Simulator:
    2DOF
    I am trying to find where the problem is with an LED. It looks like the problem is in the SerialReader function...

    It reads the "P", but doesn't get further...

    Code:
    void SimtoolsReader() {       // This function is the work of Sirnoname
      // Simtools output : P<Axis1a><Axis2a>, Data bits : 10 bits, Parity : None, stop bits : 1
      int buffer           =  0 ;    // It takes the value of the serial data
      int buffercount      = -1 ;    // To count where we are in the serial datas
    
      while (Serial.available())
      {
        if (buffercount == -1){
          buffer = Serial.read();
          if (buffer != 'P') {
            buffercount = -1;           // "P" is the marquer. If we read P, the next data is pitch
          }
          else {
            buffercount = 0; // WORKS UNTIL THIS LINE
    
          }
        }
        else{   //  if(buffercount>=0) // DON'T GET THERE... 
          buffer = Serial.read();
          commandbuffer[buffercount] = buffer; // The first value next to "P" is saved in commandbuffer in the place "buffercount"
          buffercount++;
          if (buffercount > 3)
          {
            
            pitchTarget = map((commandbuffer[0]) * 256 + commandbuffer[1], 0, 1023, 0, totalStep);
            rollTarget = map((commandbuffer[2]) * 256 + commandbuffer[3], 0, 1023, 0, totalStep);
            buffercount = -1; // Re-initialize buffercount.  pow(2, precision)
            
            break;
          }
        }
      }
    }
  5. Lebois

    Lebois (maybe I am wrong, but who knows...)

    Joined:
    Dec 10, 2018
    Messages:
    350
    Occupation:
    Math teacher
    Location:
    France
    Balance:
    2,544Coins
    Ratings:
    +206 / 2 / -0
    My Motion Simulator:
    2DOF
    Well I tested mutliple cables, it didn't change the problem :/
  6. Lebois

    Lebois (maybe I am wrong, but who knows...)

    Joined:
    Dec 10, 2018
    Messages:
    350
    Occupation:
    Math teacher
    Location:
    France
    Balance:
    2,544Coins
    Ratings:
    +206 / 2 / -0
    My Motion Simulator:
    2DOF
    I use the following code to show what simtools send, it gives me 451 and 195. Sometimes, the values change to 20481 and 1 :/ (This is the third computer on which I try...)

    pitchTarget = (commandbuffer[0]) * 256 + commandbuffer[1];
    rollTarget = (commandbuffer[2]) * 256 + commandbuffer[3];
    lcd.print(pitchTarget);
    lcd.setCursor(1,1);
    lcd.print(commandbuffer[1]);

    Attached Files:

  7. Lebois

    Lebois (maybe I am wrong, but who knows...)

    Joined:
    Dec 10, 2018
    Messages:
    350
    Occupation:
    Math teacher
    Location:
    France
    Balance:
    2,544Coins
    Ratings:
    +206 / 2 / -0
    My Motion Simulator:
    2DOF
    Ok, some things are working better know.
    The arduino displays the value received on a LCD screen, it helps...

    Otherwise, values go from 0 to 963 instead of 0 to 1023...someone knows why ?
  8. Lebois

    Lebois (maybe I am wrong, but who knows...)

    Joined:
    Dec 10, 2018
    Messages:
    350
    Occupation:
    Math teacher
    Location:
    France
    Balance:
    2,544Coins
    Ratings:
    +206 / 2 / -0
    My Motion Simulator:
    2DOF
    Update : the problem was coming from the arduino uno... I changed to an leonardo and it works...