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 Random movements stepper motor

Discussion in 'Forum and Website' started by Gert, Oct 24, 2023.

  1. Gert

    Gert New Member

    Joined:
    Sep 28, 2023
    Messages:
    2
    Balance:
    7Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    2DOF, Arduino
    Dear community

    I'm making a motion simulator prototype using an Arduino and a steppermotor.
    Whenever I startup 'Output Testing - Axis Testing' or Live for Speed, the motor starts moving randomly without responding to the sliders at all. This is because random values are being sent to the Arduino. If I check 'Output Testing - Virtual Axis' while playing LFS, everything looks normal.
    The code I use is a modified version of RCModelSimtools by eaorobbie. I use the same settings necessary for using said code.

    I can't seem to figure out where the random values come from, let alone how to make them stop interrupting with my prototype. It may have something to do with dotNET SDK. Since after I downloaded it, everything worked fine until I had a lunch break and came back. Without touching my computer or changing anything else, the behaviour became random once more.

    I don't know what to do, please enlighten me with your knowledge. I tried reinstalling Simtools and everything dotNET-related as well as turning off selective suspend USB.


    PS: This project is school related and should be finished by friday :/

    Schermopname (150).png Schermopname (154).png
    Code:
    #include <Servo.h>
    #include <Wire.h>
    #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>
    #define SCREEN_WIDTH 128
    #define SCREEN_HEIGHT 64
    
    Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
    
    #include <Servo.h>
    const int kActuatorCount = 2;                                                 
    const char kActuatorName[kActuatorCount] = { 'R', 'L' };
    
    const int spoel[kActuatorCount][4] = {{8, 9, 10, 11}, {3, 4, 5, 6}}; //Array containing pins from steppers (R, L)
    
    const int eindeloop = 2; //Reed relais
    bool home = LOW; //Wether or not motor R has been homed
    bool eindeloopBereikt = LOW; //Wether or not the reed relais has been reached, for safety and homing purposes
    int del = 1200; //Delay between turning on previous and next coil from stepper
    int delhome = 10000; //Delay between turning on previous and next coil from stepper during homing
    
    int actuatorPosition[kActuatorCount] = {13, 13}; //Initial position from steppers
    int currentPosition[kActuatorCount] = {0, 0}; //Current position from steppers
    const int kActuatorScale[kActuatorCount][2] = { { 0, 25 } ,                 
                                                    { 25, 0 }                 
                                                   };                         
    const char kEOL = '~';                                                   
    const int kMaxCharCount = 3;                                           
    
    int currentActuator = 0;                                                 
    int valueCharCount = 0;                                             
    
    enum TPortState { psReadActuator, psReadValue };
    TPortState currentState = psReadActuator; 
    
    void setup()
    {
      pinMode(eindeloop, INPUT_PULLUP);
    
      Serial.begin(115200);
    
      for (int i = 0; i < 4; i++)
      {
        pinMode(spoel[0][i], OUTPUT); //Pins motor R
        //pinMode(spoel[1][i], OUTPUT); //Pins motor L (not used)
    
      }
    
      attachInterrupt(digitalPinToInterrupt(eindeloop), MIEP, FALLING);
    
        display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
        display.setTextColor(SSD1306_WHITE);
        display.setTextSize(2);
        display.clearDisplay();
        display.print("Lopkip"); //Random first display value
        display.display(); 
    }
     
    void loop()
    {
       if (home == LOW)
       {
        digitalWrite(spoel[0][0], HIGH);
        delayMicroseconds(delhome);
        digitalWrite(spoel[0][3], LOW);
        delayMicroseconds(delhome);
        digitalWrite(spoel[0][1], HIGH);
        delayMicroseconds(delhome);
        digitalWrite(spoel[0][0], LOW);
        delayMicroseconds(delhome);
        digitalWrite(spoel[0][2], HIGH);
        delayMicroseconds(delhome);
        digitalWrite(spoel[0][1], LOW);
        delayMicroseconds(delhome);
        digitalWrite(spoel[0][3], HIGH);
        delayMicroseconds(delhome);
        digitalWrite(spoel[0][2], LOW);
        delayMicroseconds(delhome); 
    
        if (eindeloopBereikt == HIGH)
        {
          updateActuator(0);
          //updateActuator(1);
          digitalWrite(spoel[0][0], LOW); //Turns off coil, otherwise it stays excited
          digitalWrite(spoel[1][0], LOW);
          home = HIGH;
          eindeloopBereikt = LOW;
        }
      }
    }
    
    void MIEP() //Triggers when stepper reaches the reed relais (eindeloop)
    {
      eindeloopBereikt = 1;
    }
                                                                                                
    void serialEvent() {
        char tmpChar;                                                                                   
        int tmpValue;                                                                                   
        
        //Deel 1:
        while (Serial.available() && home == HIGH) { //Serial input & homed steppermotor
            
            if (currentState == psReadActuator) {                                     
                tmpChar = Serial.read();                                             
                for (int i = 0; i < kActuatorCount; i++) {                         
                    if (tmpChar == kActuatorName[i]) {
                        currentActuator = i;                        // remember which actuator we found
                        currentState = psReadValue;                 // start looking for the Actuator position
                        actuatorPosition[currentActuator] = 0;      // initialise the new position
                        valueCharCount = 0;                         // initialise number of value chars read in
                        break;
                    }
                }
            }
    
    //Deel 2:
    if (currentState == psReadValue) {
        while ((valueCharCount < kMaxCharCount) && Serial.available()) {
            tmpValue = Serial.read();
            if (tmpValue != kEOL) {
              tmpValue = tmpValue - 48;
              if ((tmpValue < 0) || (tmpValue > 9))
              tmpValue = 0;
                actuatorPosition[currentActuator] = actuatorPosition[currentActuator] * 10 + tmpValue;
                valueCharCount++;
            }
              else break;
        }
          if (tmpValue == kEOL || valueCharCount == kMaxCharCount) {
          actuatorPosition[currentActuator] = map(actuatorPosition[currentActuator], 0, 255, kActuatorScale[currentActuator][0], kActuatorScale[currentActuator][1]);
          updateActuator(currentActuator);
          display.clearDisplay();
          display.print(actuatorPosition[currentActuator]); //Display the values coming in, just for debug purposes
          display.display();
          currentState = psReadActuator;
          }
        }
      }
    }
    
    void updateActuator(int thisActuator) {
      int aantalStappen = currentPosition[thisActuator] - actuatorPosition[thisActuator]; //Amount of steps to take untill the wanted position is reached
    
        digitalWrite(spoel[0][0], LOW);
        digitalWrite(spoel[0][1], LOW);
        digitalWrite(spoel[0][2], LOW);
        digitalWrite(spoel[0][3], LOW);
    
      if (aantalStappen < 0)
      {
      for (int i = 0; i < abs(aantalStappen); i++)
      {
        digitalWrite(spoel[currentActuator][3], HIGH);
        delayMicroseconds(del);
        digitalWrite(spoel[currentActuator][0], LOW);
        delayMicroseconds(del);
        digitalWrite(spoel[currentActuator][2], HIGH);
        delayMicroseconds(del);
        digitalWrite(spoel[currentActuator][3], LOW);
        delayMicroseconds(del);
        digitalWrite(spoel[currentActuator][1], HIGH);
        delayMicroseconds(del);
        digitalWrite(spoel[currentActuator][2], LOW);
        delayMicroseconds(del);
        digitalWrite(spoel[currentActuator][0], HIGH);
        delayMicroseconds(del);
        digitalWrite(spoel[currentActuator][1], LOW);
        delayMicroseconds(del);     
      }
        currentPosition[thisActuator] = actuatorPosition[thisActuator];
      }
      else if (aantalStappen > 0)
      {
      for (int i = 0; i < abs(aantalStappen); i++)
      {
        digitalWrite(spoel[currentActuator][0], HIGH);
        delayMicroseconds(del);
        digitalWrite(spoel[currentActuator][3], LOW);
        delayMicroseconds(del);
        digitalWrite(spoel[currentActuator][1], HIGH);
        delayMicroseconds(del);
        digitalWrite(spoel[currentActuator][0], LOW);
        delayMicroseconds(del);
        digitalWrite(spoel[currentActuator][2], HIGH);
        delayMicroseconds(del);
        digitalWrite(spoel[currentActuator][1], LOW);
        delayMicroseconds(del);
        digitalWrite(spoel[currentActuator][3], HIGH);
        delayMicroseconds(del);
        digitalWrite(spoel[currentActuator][2], LOW);
        delayMicroseconds(del);   
      } 
        currentPosition[thisActuator] = actuatorPosition[thisActuator];
      }
      else //if the amount of steps to take to reach the wanted position equals zero, do nothing
      {
    
      } 
    }
  2. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    21,398
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    150,043Coins
    Ratings:
    +10,993 / 54 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
  3. Gert

    Gert New Member

    Joined:
    Sep 28, 2023
    Messages:
    2
    Balance:
    7Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    2DOF, Arduino
    Thank you for responding so quickly. I've tried lowering the percentages but sadly, the same problem occurs. If you'ld like to see the problem in action you can search for Kilimoenipa Pointser on youtube and watch the video called 'Simtools problem random data'.
    The OLED-screen shows the first couple of mapped positions (0-25 equivalent to 0-180°) being sent to the Arduino. As I've stated before, the setup did work for a bit. It even responded as one would expect while playing Live for Speed. Using a stepper is in fact more challenging than using a servo but I don't think this is relevant to the problem at hand.
  4. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    21,398
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    150,043Coins
    Ratings:
    +10,993 / 54 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
    @Lebois has experience with steppers, but eventually gave up on them and moved to servos, but may be able to lend some insights.