1. 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!
  2. 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
  3. 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 School project (Hopeful)

Discussion in 'Ready, set, go - Start your engines' started by Michael fogarty, Sep 19, 2018.

  1. Michael fogarty

    Michael fogarty New Member

    Joined:
    Sep 17, 2018
    Messages:
    4
    Balance:
    37Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    Arduino
    Hi guys and girls, im fairly new to this so you will have to be patient with me im afraid.

    I am basically building a small rig with hobby servos (just to start off with) and am hoping to do it as a project in my school to encourage students into an engineering career.

    I am using the code at the bottom of this thread.

    I have the servos connected to pin 4 and 3. The arduino and servos have been tested and work.

    When running the game the virtual axis all move fine.

    however the arduino does not seam to be getting any signal. if i put a serial.println in anywhere in the code it does not print to the serial monitor, unless i place it in the void loop. this makes me think that the data is not being sent to the arduio and it is just constantly looping in the loop.

    i have tried every combination of Interface-output, bit rates and axis assignments that i could find on the forums but noting works. I have also tried other codes i have seen people share but that does not work for me ether.



    upload_2018-9-19_8-18-59.png

    upload_2018-9-19_8-17-54.png


    Attached Files:

  2. Ads Master

    Ads Master

    Balance:
    Coins
    Ratings:
    +0 / 0 / -0
  3. Michael fogarty

    Michael fogarty New Member

    Joined:
    Sep 17, 2018
    Messages:
    4
    Balance:
    37Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    Arduino
    sorry forgot to put the code in


    #include <Servo.h>
    const int kServoCount = 2; // how many servos we are handling
    const int kRIGHT = 0; // constants for our servos
    const int kLEFT = 1; //
    const int kPins[kServoCount] = {4, 5}; // pins to which the servos are attached
    const int kValueCharCount = 3; // how many characters we must read in for a new servo position value
    const int kServoScale[kServoCount][2] = { { 0, 179 } , // Right servo scaling
    { 179, 0 }}; // Left side servo scaling

    Servo servoSet[kServoCount]; // our array of servos
    int servoPosition[kServoCount] = {90, 90}; // current servo positions, initialised to 90
    int currentServo; // keep track of the current servo being read in from serial port
    int valueCharCount = 0; // how many value characters have we read in

    // set up some states for our state machine
    // psReadServo = next character from serial port tells us the servo
    // psReadValue = next 3 characters from serial port tells us the value
    enum TPortState { psReadServo, psReadValue };
    TPortState currentState = psReadServo;

    void setup()
    {
    // attach the servos to the pins
    servoSet[kRIGHT].attach(kPins[kRIGHT]);
    servoSet[kLEFT].attach(kPins[kLEFT]);

    // initialise their position
    updateServo(kRIGHT);
    updateServo(kLEFT);

    Serial.begin(9600); // opens serial port at a baud rate of 9600
    }

    void loop()
    {

    }

    // this code only runs when we have serial data available. ie (Serial.available() > 0).
    void serialEvent() {
    char tmpChar;
    int tmpValue;



    while (Serial.available()) {
    // if we're waiting for a servo name, grab it here
    if (currentState == psReadServo) {
    tmpChar = Serial.read();
    switch (tmpChar) {
    case 'R' : currentServo = kRIGHT;
    Serial.println("reading right servo value");
    break;
    case 'L' : currentServo = kLEFT;
    Serial.println("reading left servo value");
    break;
    }
    currentState = psReadValue; // start looking for the servo position
    servoPosition[currentServo] = 0; // initialise the new position
    valueCharCount = 0; // intitialise our position characters read in from serial port counter
    }

    // if we're ready to read in the current servo's position data
    if (currentState == psReadValue) {
    while ((valueCharCount < kValueCharCount) && Serial.available()) {
    tmpValue = Serial.read() - 48;
    if (tmpValue < 0) tmpValue = 0;
    servoPosition[currentServo] = servoPosition[currentServo] * 10 + tmpValue;
    valueCharCount++;
    }

    // if we've read the number of characters required for servo position, update the servo and start looking for the next servo name
    if (valueCharCount == kValueCharCount) {
    Serial.print("read in" );
    Serial.println(servoPosition[currentServo]);
    // scale the new position so the value is between 0 and 179
    servoPosition[currentServo] = map(servoPosition[currentServo], 0, 255, kServoScale[currentServo][0], kServoScale[currentServo][1]);
    Serial.print("scaled to" );
    Serial.println(servoPosition[currentServo]);
    updateServo(currentServo);
    currentState = psReadServo;
    }
    }
    }

    }


    // write the current servo position to the passed in servo
    void updateServo(int thisServo) {
    servoSet[thisServo].write(servoPosition[thisServo]);

    }
  4. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    17,067
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    123,923Coins
    Ratings:
    +9,714 / 47 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
  5. SeatTime

    SeatTime Well-Known Member

    Joined:
    Dec 27, 2013
    Messages:
    2,716
    Occupation:
    Retired
    Location:
    Newcastle Australia
    Balance:
    28,687Coins
    Ratings:
    +3,003 / 37 / -0
    My Motion Simulator:
    AC motor, Arduino, Motion platform, 6DOF
    Earobbie's code is 'basically' what he posted, down to the comments that were left in ;). I would recommend he go back to the original - nothing wrong with that, if you just want to build a sim.
    Last edited: Sep 19, 2018
  6. Michael fogarty

    Michael fogarty New Member

    Joined:
    Sep 17, 2018
    Messages:
    4
    Balance:
    37Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    Arduino
    Thanks guys, its starting to make much more seance to me now :) One more question however, what should i set in my axis assignments for this set up? i have a left and right servo.

    R<Axis1>~L<Axis2>~

    Attached Files:

  7. SeatTime

    SeatTime Well-Known Member

    Joined:
    Dec 27, 2013
    Messages:
    2,716
    Occupation:
    Retired
    Location:
    Newcastle Australia
    Balance:
    28,687Coins
    Ratings:
    +3,003 / 37 / -0
    My Motion Simulator:
    AC motor, Arduino, Motion platform, 6DOF
    I recommend you refer to @noorbeast 's comment :).
    • Agree Agree x 1
  8. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    17,067
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    123,923Coins
    Ratings:
    +9,714 / 47 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
    It should be:

    R<Axis1a>~L<Axis2a>~

    As per my earlier comment.
  9. Michael fogarty

    Michael fogarty New Member

    Joined:
    Sep 17, 2018
    Messages:
    4
    Balance:
    37Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    Arduino
    Thank you, all working :) just needs some fine tuning.
    Could I use NEMA 17 stepper motors and lead screws as liner actuators for the full sized simulation?

  10. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    17,067
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    123,923Coins
    Ratings:
    +9,714 / 47 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK