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

Resolved Stepper moves randomly

Discussion in 'SimTools DIY Version' started by Tintin Boulsard, Feb 20, 2024.

  1. Tintin Boulsard

    Tintin Boulsard Member

    Joined:
    Dec 23, 2022
    Messages:
    40
    Location:
    France
    Balance:
    152Coins
    Ratings:
    +17 / 0 / -0
    My Motion Simulator:
    3DOF, Arduino
    Hello,
    I'm building a 3DOF moton simulator based on actuators. I use 8,5NM stepper motors driven by a HBS86H driver, and controlled by an Arduino Uno R3. I modified a code written by @eaorobbie, so it can work with my steppers :
    Code:
    #include <BasicStepperDriver.h>
    
    //------------------------- Can be modified --------------------------//
    #define STEP 3
    #define DIR 4
    #define RPM 120
    #define MICROSTEPS 16
    #define MOTOR_STEPS 200
    //#define DEBUG 1
    //--------------------------------------------------------------------//
    
    BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP);
    
    const int kActuatorCount = 1;  //Number of actuators
    const char kActuatorName[kActuatorCount] = {'R'};
    const int kActuatorScale[kActuatorCount][2] = { { -10000, 10000 } };
    const char kEOL = '~';
    const int kMaxCharCount = 3;
    int actuatorPosition[kActuatorCount] = {0};
    int actuatorPosition2[kActuatorCount] = {0};
    int currentActuator;
    int valueCharCount = 0;
    int remember[kActuatorCount] = {0};
    enum TPortState { psReadActuator, psReadValue };
    TPortState currentState = psReadActuator;
    
    void setup(){
      Serial.begin(9600);
      stepper.begin(RPM, MICROSTEPS);
      }
    
    void loop()
    {
    }
    
    void serialEvent(){
        char tmpChar;
        int tmpValue;
    
        while (Serial.available()) {
            if (currentState == psReadActuator) {
                tmpChar = Serial.read();
     
    #ifdef DEBUG           
    Serial.print("read in ");           
    Serial.println(tmpChar);           
    #endif
    
                for (int i = 0; i < kActuatorCount; i++) {
                    if (tmpChar == kActuatorName[i]) {
    
    #ifdef DEBUG           
    Serial.print("which is actuator ");           
    Serial.println(i);           
    #endif
    
                        currentActuator = i;
                        currentState = psReadValue;             
                        actuatorPosition[currentActuator] = 0;
                        valueCharCount = 0;         
                        break;
                    }
                }
            }
          
            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) {
    
    #ifdef DEBUG           
    Serial.print("read in ");           
    Serial.println(actuatorPosition[currentActuator]);           
    #endif
    
    
                    actuatorPosition[currentActuator] = map(actuatorPosition[currentActuator], 0, 255, kActuatorScale[currentActuator][0], kActuatorScale[currentActuator][1]);
    #ifdef DEBUG           
    Serial.print("scaled to ");           
    Serial.println(actuatorPosition[currentActuator]);   
    #endif
    
    actuatorPosition2[currentActuator] = actuatorPosition[currentActuator] - remember[currentActuator];
    remember[currentActuator] = actuatorPosition[currentActuator];
                    updateActuator(currentActuator);
                    currentState = psReadActuator;
                }
            }
        }
    
        
    }
    
    
    void updateActuator(int thisActuator) {
        stepper.move(actuatorPosition2[thisActuator]);
    }
    
    It works perfectly in the Serial Monitor, but unfortunately, the motor rotates randomly when testing in Simtools (Output Testing).

    Here are my settings :
    [​IMG]

    Any help would be appreciated,
    Best regards,
    Tintin Boulsard
  2. yobuddy

    yobuddy Well-Known Member Staff Member Moderator SimAxe Beta Tester SimTools Developer Gold Contributor

    Joined:
    Feb 9, 2007
    Messages:
    5,282
    Occupation:
    Computer Technician
    Location:
    Portland, Oregon - USA
    Balance:
    49,198Coins
    Ratings:
    +5,093 / 18 / -0
    Maybe try this system local setting buddy.
    system local.jpg
  3. Tintin Boulsard

    Tintin Boulsard Member

    Joined:
    Dec 23, 2022
    Messages:
    40
    Location:
    France
    Balance:
    152Coins
    Ratings:
    +17 / 0 / -0
    My Motion Simulator:
    3DOF, Arduino
    Hi @yobuddy,
    I'll test this soon, but I don't think this will resolve the problem because I have no problem when I use other codes on the Arduino. I'll keep you updated.

    Thanks anyway,
    Best regards,
    Tintin Boulsard
    • Informative Informative x 1
  4. Tintin Boulsard

    Tintin Boulsard Member

    Joined:
    Dec 23, 2022
    Messages:
    40
    Location:
    France
    Balance:
    152Coins
    Ratings:
    +17 / 0 / -0
    My Motion Simulator:
    3DOF, Arduino
    Hello,
    I changed the thread title to "Resolved" because I don't have issues anymore. I didn't change the "system local setting" but it works perfectly well. The problem was coming from, I think, the Arduino library...

    Best regards,
    Tintin Boulsard
    • Like Like x 2