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

95% finished - now I'm completely stuck. Xsim Comms to my ARD.

Discussion in 'DIY Motion Simulator Building Q&A / FAQ' started by kev b, Jan 23, 2017.

  1. kev b

    kev b Active Member

    Joined:
    May 3, 2016
    Messages:
    162
    Occupation:
    Academic Lecturer - Electrical Engineering
    Location:
    Swindon, UK
    Balance:
    899Coins
    Ratings:
    +129 / 0 / -0
    My Motion Simulator:
    3DOF, Arduino, Motion platform, 6DOF
    I've finished putting together my arduino uno and monster moto board for my 2DOF.
    I used racingmatt's 2DOF arduino code and circuit as shown at the bottom of this post.
    The 2DOF circuit does not yet have the motors installed but I can check motor 1 or 2 operation and direction from the LED's on the monstermoto for now.
    Everything works as it should when I power up. The motor outputs are active until I adjust the pots to centre pos. I can send HEX position codes like R7FL7F etc to the ARD using the serial window within the sketch and it all works perfectly.

    As I understand it, the code sets up a com port (3 in my case) for the ARD at 115200 baud rate. I have my pc com port set the same. So when I type in a hex position data into the serial window in the sketch, the program is doing it job of passing this to the serial read etc and bingo - the motors go there.

    So, now I download and install Xsim V2 and follow the setup instructions (from racingmatt's page and the actual instructions from this forum).
    I also install the joystick and latest elite dangerous plugins, patch them successfully, start up game manager and game engine - no problems there. I set up 2 axis (1 and 2) for sway and surge for dof 1 and 2 respectively. When I run the joystick plugin I can see the pitch roll sway etc sliders move and the corresponding axis outputs (1a 1nd 2a) in Xsim 'output testing - virtual axis'. They all move correctly and as expected. I believe these axis outputs are directly representative of the axis data that should output from Xsim to my ARD ?? (is this a correct assumption).
    Elite also works in the same way - no problem with Xsim reading the position data from the game and relaying it to the virtual axis in Xsim.
    So far everything is looking good for the final connection between my pc and ARD. I'm expecting to be able to use the 'sliders' in Xsim 'axis testing' to actually move the motors on my simulator or use the joystick to do the same and fire up Elite and see the same (again - is this a correct assumption??)

    I go to the 'interface settings' in Xsim and set comms up for serial, Com 3, 115200, 8 data bits, no parity, 1 stop bit, and output bit range set at 8 in Hex. These are the same settings for my pc com port for the ARD.

    As I see it, the final link now is the 'interface output' to link Xsim to the ARD. The format for this is listed as
    R<Axis1>L<Axis2> in the racingmatt code. R and L correspond to the ARD sketch and the Axis numbers match the outputs I will use, and have seen simulated successfully in Xsim.

    I have yet to get Xsim to talk to the ARD correctly.

    On startup of Xsim, my motors both rotate for approx 40 degrees and stop. Then no matter what I do with the pots, I cannot drive the motors. The ARD comm LED is on but this is likely initiated by the ARD sketch rather than Xsim?? (is this a correct assumption).

    I know the sketch code works, I can send position data serially to it via serial window and positions are moved to. Hex is received and converted into positions for my two motors.
    This is the same sketch code routine that would process the serial data from Xsim so I am really struggling to identify what has happened.
    I'm sure the prob is with the comm port or the Xsim comm setup format. Both Xsim and ARD work perfectly when in isolation - but working together, not yet.

    The gutting thing is, I've contacted another forum member who had exactly the same problem last year and he got his going. We are using the same sketch code and circuit layout so it should work for me too.

    Hoping one of you master gurus can cast an eye and let me know what noob mistake I've made (lol - I'm a Lecturer believe it or not). I've included a couple of Xsim screenshots showing my current settings. I also ensure I close arduino GUI down before opening Xsim (so only one can access port 3).

    I'm including the sketch below; and will add some screenshots/vids if requested.
    Many thanks in anticipation, I really hope someone can help as i don't want to chuck to towel in on this one. I am tenacious, but am running out of options to try. Please help.
    Kev B

    ~~~~~~~~~~~~~~
    #define BRAKEVCC 0
    #define RV 2 //beware it's depending on your hardware wiring
    #define FW 1 //beware it's depending on your hardware wiring
    #define STOP 0
    #define BRAKEGND 3

    ////////////////////////////////////////////////////////////////////////////////
    #define pwmMax 255 // 255 or less, if you want to lower the maximum motor's speed

    // defining the range of potentiometer's rotation
    const int potMini=208;
    const int potMaxi=815;

    ////////////////////////////////////////////////////////////////////////////////
    #define motLeft 0
    #define motRight 1
    #define potL A0
    #define potR A1

    ////////////////////////////////////////////////////////////////////////////////
    // DECLARATIONS
    ////////////////////////////////////////////////////////////////////////////////
    /* VNH2SP30 pin definitions*/
    int inApin[2] = {
    7, 4}; // INA: Clockwise input
    int inBpin[2] = {
    8, 9}; // INB: Counter-clockwise input
    int pwmpin[2] = {
    5, 6}; // PWM input
    int cspin[2] = {
    2, 3}; // CS: Current sense ANALOG input
    int enpin[2] = {
    0, 1}; // EN: Status of switches output (Analog pin)
    int statpin = 13; //not explained by Sparkfun
    /* init position value*/
    int DataValueL=512; //middle position 0-1024
    int DataValueR=512; //middle position 0-1024

    ////////////////////////////////////////////////////////////////////////////////
    // INITIALIZATION
    ////////////////////////////////////////////////////////////////////////////////
    void setup()
    {
    // serial initialization
    Serial.begin(115200);

    // initialization of Arduino's pins
    pinMode(statpin, OUTPUT); //not explained by Sparkfun
    digitalWrite(statpin, LOW);

    for (int i=0; i<2; i++)
    {
    pinMode(inApin, OUTPUT);
    pinMode(inBpin, OUTPUT);
    pinMode(pwmpin, OUTPUT);
    }
    // Initialize braked for motor
    for (int i=0; i<2; i++)
    {
    digitalWrite(inApin, LOW);
    digitalWrite(inBpin, LOW);
    }
    }
    ////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////// Main Loop ////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    void loop()
    {
    int sensorL,sensorR;

    readSerialData(); // DataValueR & L contain the last order received (if there is no newer received, the last is kept)
    // the previous order will still be used by the PID regulation MotorMotion Function

    sensorR = analogRead(potR); // range 0-1024
    sensorL = analogRead(potL); // range 0-1024

    motorMotion(motRight,sensorR,DataValueR);
    motorMotion(motLeft,sensorL,DataValueL);
    }
    ////////////////////////////////////////////////////////////////////////////////
    // Procedure: wait for complete trame
    ////////////////////////////////////////////////////////////////////////////////
    void readSerialData()
    {
    byte Data[3]={
    '0','0','0' };
    // keep this function short, because the loop has to be short to keep the control over the motors

    if (Serial.available()>2){
    //parse the buffer : test if the byte is the first of the order "R"
    Data[0]=Serial.read();
    if (Data[0]=='L'){
    Data[1]=Serial.read();
    Data[2]=Serial.read();
    // call the function that converts the hexa in decimal and that maps the range
    DataValueR=NormalizeData(Data);
    }
    if (Data[0]=='R'){
    Data[1]=Serial.read();
    Data[2]=Serial.read();
    // call the function that converts the hexa in decimal and maps the range
    DataValueL=NormalizeData(Data);

    }
    }
    if (Serial.available()>16) Serial.flush();
    }
    ////////////////////////////////////////////////////////
    void motorMotion(int numMot,int actualPos,int targetPos)
    ////////////////////////////////////////////////////////
    {
    int Tol=20; // no order to move will be sent to the motor if the target is close to the actual position
    // this prevents short jittering moves
    //could be a parameter read from a pot on an analogic pin
    // the highest value, the calmest the simulator would be (less moves)

    int gap;
    int pwm;
    int brakingDistance=30;

    // security concern : targetPos has to be within the mechanically authorized range
    targetPos=constrain(targetPos,potMini+brakingDistance,potMaxi-brakingDistance);

    gap=abs(targetPos-actualPos);

    if (gap<= Tol) {
    motorOff(numMot); //too near to move
    }
    else {
    // PID : calculates speed according to distance
    pwm=195;
    if (gap>50) pwm=215;
    if (gap>75) pwm=235;
    if (gap>100) pwm=255;
    pwm=map(pwm, 0, 255, 0, pwmMax); //adjust the value according to pwmMax for mechanical debugging purpose !

    // if motor is outside from the range, send motor back to the limit !
    // go forward (up)
    if ((actualPos<potMini) || (actualPos<targetPos)) motorGo(numMot, FW, pwm);
    // go reverse (down)
    if ((actualPos>potMaxi) || (actualPos>targetPos)) motorGo(numMot, RV, pwm);

    }
    }

    ////////////////////////////////////////////////////////////////////////////////
    void motorOff(int motor){ //Brake Ground : free wheel actually
    ////////////////////////////////////////////////////////////////////////////////
    digitalWrite(inApin[motor], LOW);
    digitalWrite(inBpin[motor], LOW);
    analogWrite(pwmpin[motor], 0);
    }
    ////////////////////////////////////////////////////////////////////////////////
    void motorOffBraked(int motor){ // "brake VCC" : short-circuit inducing electromagnetic brake
    ////////////////////////////////////////////////////////////////////////////////
    digitalWrite(inApin[motor], HIGH);
    digitalWrite(inBpin[motor], HIGH);
    analogWrite(pwmpin[motor], 0);
    }

    ////////////////////////////////////////////////////////////////////////////////
    void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm)
    ////////////////////////////////////////////////////////////////////////////////
    {
    if (motor <= 1)
    {
    if (direct <=4)
    {
    // Set inA[motor]
    if (direct <=1)
    digitalWrite(inApin[motor], HIGH);
    else
    digitalWrite(inApin[motor], LOW);

    // Set inB[motor]
    if ((direct==0)||(direct==2))
    digitalWrite(inBpin[motor], HIGH);
    else
    digitalWrite(inBpin[motor], LOW);

    analogWrite(pwmpin[motor], pwm);

    }
    }
    }

    ////////////////////////////////////////////////////////////////////////////////
    void motorDrive(uint8_t motor, uint8_t direct, uint8_t pwm)
    ////////////////////////////////////////////////////////////////////////////////
    {
    // more readable function than Jim's (for educational purpose)
    // but 50 octets heavier -> unused
    if (motor <= 1 && direct <=4)
    {
    switch (direct) {
    case 0: //electromagnetic brake : brake VCC
    digitalWrite(inApin[motor], HIGH);
    digitalWrite(inBpin[motor], HIGH);
    break;
    case 3: //Brake Ground (free wheel)
    digitalWrite(inApin[motor], LOW);
    digitalWrite(inBpin[motor], LOW);
    break;
    case 1: // forward : beware it's depending on your hardware wiring
    digitalWrite(inApin[motor], HIGH);
    digitalWrite(inBpin[motor], LOW);
    break;
    case 2: // Reverse : beware it's depending on your hardware wiring
    digitalWrite(inApin[motor], LOW);
    digitalWrite(inBpin[motor], HIGH);
    break;
    }
    analogWrite(pwmpin[motor], pwm);
    }
    }
    ////////////////////////////////////////////////////////////////////////////////
    // testPot
    ////////////////////////////////////////////////////////////////////////////////
    void testPot(){

    Serial.print(analogRead(potR));
    Serial.print(";");
    Serial.println(analogRead(potL));
    delay(250);

    }
    ////////////////////////////////////////////////////////////////////////////////
    void testpulse(){
    int pw=120;
    while (true){

    motorGo(motLeft, FW, pw);
    delay(250);
    motorOff(motLeft);
    delay(250);
    motorGo(motLeft, RV, pw);
    delay(250);
    motorOff(motLeft);

    delay(500);

    motorGo(motRight, FW, pw);
    delay(250);
    motorOff(motRight);
    delay(250);
    motorGo(motRight, RV, pw);
    delay(250);
    motorOff(motRight);
    Serial.println("testpulse pwm:80");
    delay(500);

    }
    }
    ////////////////////////////////////////////////////////////////////////////////
    // Function: convert Hex to Dec
    ////////////////////////////////////////////////////////////////////////////////
    int NormalizeData(byte x[3])
    ////////////////////////////////////////////////////////////////////////////////
    {
    int result;

    if ((x[2]==13) || (x[2]=='R') || (x[2]=='L')) //only a LSB and Carrier Return or 'L' or 'R' in case of value below 16 (ie one CHAR and not 2)
    {
    x[2]=x[1]; //move MSB to LSB
    x[1]='0'; //clear MSB
    }
    for (int i=1; i<3; i++)
    {
    if (x>47 && x<58 ){//for xA to xF
    x=x-48;
    }
    if ( x>64 && x<71 ){//for x0 to x9
    x=x-55;
    }
    }
    // map the range from Xsim (0 <-> 255) to the mechanically authorized range (potMini <-> potMaxi)
    result=map((x[1]*16+x[2]),0,255,potMini,potMaxi);
    return result;
    }

    ~~~~~~~~~~~~~~~
    I attach a couple of pix of my Xsim settings in game engine below
    game engine settings 1.jpg game engine settings.jpg
    Last edited: Jan 23, 2017
  2. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    21,293
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    149,427Coins
    Ratings:
    +10,945 / 54 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
    In your Interface settings it should be like this: R<axis1a>L<axis2a> to specify the grouping for the Axis Allocation.

    Yes you should move the sliders in Output Testing.
    • Agree Agree x 1
  3. kev b

    kev b Active Member

    Joined:
    May 3, 2016
    Messages:
    162
    Occupation:
    Academic Lecturer - Electrical Engineering
    Location:
    Swindon, UK
    Balance:
    899Coins
    Ratings:
    +129 / 0 / -0
    My Motion Simulator:
    3DOF, Arduino, Motion platform, 6DOF
    Many thanks for the quick reply noorbeast,
    I'll give it a go first thing in the morning. I see the logic there. I hope its as simple as that.
    Cheers for now - I'll report back if it's fixed (hopes)
  4. kev b

    kev b Active Member

    Joined:
    May 3, 2016
    Messages:
    162
    Occupation:
    Academic Lecturer - Electrical Engineering
    Location:
    Swindon, UK
    Balance:
    899Coins
    Ratings:
    +129 / 0 / -0
    My Motion Simulator:
    3DOF, Arduino, Motion platform, 6DOF
    Ok - I've had a busy day on this problem and have made progress.

    I changed the outputs to R<axis1a>L<axis2a>, but the same problem remains. No output to the ARD.
    I then installed Simtools v2 to a spare laptop. The ARD set itself on com 4. Started up simtools. Ensured same comm settings in sketch, pc comms and game engine comms. But the same problem remains - No output to the ARD
    (its worth noting that the rx led on the ARD behaves as though it is receiving data, and switches on and off when sim engine is turned on or off - this has always been the case and what I rresume to be correct behaviour).

    I then decided to wipe simtools v2 and install v1.3 instead. Wayhay!! - full functions. The sliders in axis testing operates my motors correctly.
    I then installed the joystick plugin for v1.3, patched it, and it ran perfectly. Been playing with it all afternoon and built a mini 2DOF on my bench.

    I was coming to the conclusion that I would happily stick with v1.3 if it's now working. However, I only play elite dangerous so really want to make use of the very excellent plugin made by value1/silentchill.
    I installed and patched the ED plugin for v1.3 and gave it a whirl. No motion out to my ARD.
    I figured this may be because the v1.3 plugin is really for an earlier version of ED (with different data memory locations), so I will try and install the latest plugin for ED into v1.3. Will this work?? using a v1.3 plugin for v2?? If not I may be screwed (or forced to use the v1.3 joystick plugin with ED - not ideal really).

    I do know that when I had v2 installed the latest ED plugin worked flawlessly - giving axis 1a and 2a output (albeit no all the way out to my ARD).

    I can only conclude that between v1.3 and v2 of simtools there has been a change that either requires some code rewrite in my ARD sketch due to a comm protocol change in simtools OR the format of the comms output settings in game engine v2 is incorrect somehow.
    It cant be anything else really can it?. Are there any other comm format variations worth trying instead of R<axis1> etc?

    I'm attaching a couple of v.short utube vids I made of the v1.3 simtools and ARD working correctly and the v2.0 simtools + ED plugin working (but not outputting). Settings screenshots are in the vids. The code sketch is in my opening post and is the same code I used with simtools v1.3 (worked ok) and v2 (didn't work).

    Can I ask then if v1.3 simtools will work with the ED plugin for v2 simtools? and does anyone have any idea what else I can try to get v2 simtools outputting just like v1.3 does?
    Thanks for taking the time to read this, much appreciated.
    Kev

    my bench sim working correctly with v1.3 simtools


    simtools v2 and LATEST elite plugin working in v2 simtools (but not outputting to ARD)
    Last edited: Jan 25, 2017
  5. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    21,293
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    149,427Coins
    Ratings:
    +10,945 / 54 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
    Did you change your Interface Output like I suggested?

    Just double checking, what is the Arduino Com Port listed as in Device Manager?
  6. kev b

    kev b Active Member

    Joined:
    May 3, 2016
    Messages:
    162
    Occupation:
    Academic Lecturer - Electrical Engineering
    Location:
    Swindon, UK
    Balance:
    899Coins
    Ratings:
    +129 / 0 / -0
    My Motion Simulator:
    3DOF, Arduino, Motion platform, 6DOF
    Hi noorbeast.
    Yes, tried the settings you suggested but still movement.
    The ard is listed on com 3.
    I listed all my settings in my post above.
    Thanks for your help.

    Kev
  7. yobuddy

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

    Joined:
    Feb 9, 2007
    Messages:
    5,333
    Occupation:
    Computer Technician
    Location:
    Portland, Oregon - USA
    Balance:
    49,825Coins
    Ratings:
    +5,125 / 18 / -0
    Please try this @kev b ,
    R<Axis1a>L<Axis2a>
    yobuddy
    • Agree Agree x 1
  8. kev b

    kev b Active Member

    Joined:
    May 3, 2016
    Messages:
    162
    Occupation:
    Academic Lecturer - Electrical Engineering
    Location:
    Swindon, UK
    Balance:
    899Coins
    Ratings:
    +129 / 0 / -0
    My Motion Simulator:
    3DOF, Arduino, Motion platform, 6DOF
    Hi buddy.
    I already tried that. It made no difference. All my findings are detailed above.
    There must be something else that's stopping the comms from working.
    Last edited: Jan 25, 2017
  9. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    21,293
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    149,427Coins
    Ratings:
    +10,945 / 54 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
    Have you tried with the upper case A?
  10. kev b

    kev b Active Member

    Joined:
    May 3, 2016
    Messages:
    162
    Occupation:
    Academic Lecturer - Electrical Engineering
    Location:
    Swindon, UK
    Balance:
    899Coins
    Ratings:
    +129 / 0 / -0
    My Motion Simulator:
    3DOF, Arduino, Motion platform, 6DOF
    I will delete v1.3 after work today and reinstall v2. I will certainly try it with capital letters for the axis values although the assignments are lower case in simtools.
    I also updated my .net to the latest version.
    To be honest, I've read 100s of threads now and as far as I can see it should work (as it does in v1.3).
    Am I correct in that simtools does output hex values via serial when these are selected in the engine comms settings, but is there some other format that v2 needs that v1.3 didn't other than the addition of a and b in the axis assignations ?
  11. yobuddy

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

    Joined:
    Feb 9, 2007
    Messages:
    5,333
    Occupation:
    Computer Technician
    Location:
    Portland, Oregon - USA
    Balance:
    49,825Coins
    Ratings:
    +5,125 / 18 / -0
    Nope.
    • Agree Agree x 1
  12. kev b

    kev b Active Member

    Joined:
    May 3, 2016
    Messages:
    162
    Occupation:
    Academic Lecturer - Electrical Engineering
    Location:
    Swindon, UK
    Balance:
    899Coins
    Ratings:
    +129 / 0 / -0
    My Motion Simulator:
    3DOF, Arduino, Motion platform, 6DOF
    I have now fired up my pc since updating my .net to latest version. May be nothing to do with the problem but I'll try anything at this point.
    I uninstalled simtools v1.3 and reinstalled v2.

    Set up the axis outputs as R<Axis1a>L<Axis2a>. Double checked the comms then tried the sliders in game engine. WOoHOo! it's only bloody well working. When it was R<axis1a>L<axis2a> it didnt work!. So capital letters for the 'A' in 'Axis'.
    Thanks for all your help guys.
    I only got the Ard less than 2 weeks ago and I can't believe how much I've learned in that time -comms, hex, coding, debugging and quite a few hours researching on this forum and the web in General. lol.

    I'll be upscaling everything now and doing some tuning. I'll be sure to start a build thread on the forum but leave you with a short vid of it all working with the elite dangerous plugin (the only real reason I want a platform).

    Thanks again noorbeast and yobuddy.
    Kevin
    • Like Like x 2
    • Winner Winner x 1
    Last edited: Jan 26, 2017
  13. yobuddy

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

    Joined:
    Feb 9, 2007
    Messages:
    5,333
    Occupation:
    Computer Technician
    Location:
    Portland, Oregon - USA
    Balance:
    49,825Coins
    Ratings:
    +5,125 / 18 / -0
    @kev b,
    Way to hang in there buddy!
    • Agree Agree x 1