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

Arduino uno disconnect in game

Discussion in 'DIY Motion Simulator Building Q&A / FAQ' started by gigi, Jun 7, 2019.

  1. gigi

    gigi Active Member

    Joined:
    Nov 16, 2013
    Messages:
    466
    Balance:
    900Coins
    Ratings:
    +139 / 1 / -0
    My Motion Simulator:
    2DOF, JRK, 4DOF
    hallo to everyone.
    I have a problem that i'm not able to solve...Hope to find your support
    My project is this one https://www.xsimulator.net/community/threads/my-third-project-4-dof.13285/page-2#post-178190:
    My problem is that during the game (ac or dirt rally), after a few seconds one or both of the Arduino boards are disconnected. Sometimes the problem happens even in test if I move the cursor quickly
    If I close and restart simtools everything starts again perfectly but after a while the problem reappears.

    I state that I have this configuration:
    - n.4 24V 350W motors managed by 2 sabertooth 2x32 boards and a genuine arduino one
    I use the code that released Wanegain (see at the end of this post) and in which I placed the P at 100. The code is this one :
    - I use potentiometers with 180 ° range but as you can see in the arduino code the range of use of my motors is only 90 ° so I tend to exclude the hypothesis that arduino is blocked because the potentiometers go out of range.
    Last week, thinking about a problem with my usb ports, I formatted my pc and reinstalled my os windows 10 and all programs. I made all the updates
    Besides simtools I use simdashboard and also simhub for my bass shaker system. I also entered the SimHubWPF.exe file and excluded the COM3 and COM4 ports which are those used by simtools
    So apparently everything seems fine. Nevertheless, my problem remains.
    I use 2 psu 24v 1000w (one for each sabertooth and 2 motors). Some weeks ago bought 4 new power supplies that I could install in parallel to see if the problem is solved but I don't think it's a power supply problem the psu are these https://www.ebay.co.uk/itm/TDK-Lambda-240V-AC-24V-DC -1000w-44A-Switch-Mode-Power-Supply-SMPS-SWS1000L-24/401700985973? hash = item5d873e9c75: g: mO0AAOSwGgdbvgS3: rk: 1: mp: 1. In my opinion the problem concerns the arduino boards.
    I have the feeling that in the case of very sudden and rapid movements the card gets stuck or loses the signal. But being a problem that does not concern a single card and having also tried to use other Arduino cards that I had without stocking, I wonder if the problem concerns any setting in the code I uploaded

    I hope you will help me.
    thanks
    gigi

    Here my arduino code:
    /*
    * Original source code from sirnoname : x-sim.de
    changelog:
    1.6
    - changing var for array of (int) values

    1.5
    - add a switch to reset position of motors

    1.4 :
    - you can use a potentiometer to modify the global power of the motion simulator
    - to disable this functionnality you need to set GP_select = 0;

    1.23 :
    - When serial send XE00C, the motion plateform will be placed in original position
    - Use of packet serial communication instead of simplified serial (more stable)


    10 bits (16 bits value / 64)
    57600 bauds 8N1

    Simtools : X1<Axis1a>CX2<Axis2a>CX3<Axis3a>CX4<Axis4a>C

    XE00C - End
    Pin out of arduino for Sabertooth

    Pin 8 - TX data to connect to S1 Sabertooth 1
    Pin 10 - TX data to connect to S1 Sabertooth 2

    Analog Pins

    Pin A0 - input of feedback positioning from motor 1
    Pin A1 - input of feedback positioning from motor 2
    Pin A2 - input of feedback positioning from motor 3
    Pin A3 - input of feedback positioning from motor 4

    Pin A5 - input of potentiometer to define the global power

    Pin 13 - switch to move the motion simulator in standby value. connect to GND

    As well 5v and GND pins tapped in to feed feedback pots too.
    */

    #include <Sabertooth.h>
    #include <SoftwareSerial.h>

    //Some speed test switches for testers ;)
    #define FASTADC 1 //Hack to speed up the arduino analogue read function, comment out with // to disable this hack

    // defines for setting and clearing register bits
    #ifndef cbi
    #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
    #endif
    #ifndef sbi
    #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
    #endif

    #define LOWBYTE(v) ((unsigned char) (v)) //Read
    #define HIGHBYTE(v) ((unsigned char) (((unsigned int) (v)) >> 8))
    #define BYTELOW(v) (*(((unsigned char *) (&v) + 1))) //Write
    #define BYTEHIGH(v) (*((unsigned char *) (&v)))

    int DeadZone = 9; //increase this value to reduce vibrations of motors
    int ReadAnalog = 6;

    int EmergencyPin = 13; // Emergency switch (GND)

    //motors array : Pin, currentAnalog, target, Minpot, MaxPot, pot deadzone, standby value, P, I, D, integrated error, last error, Power
    int motors[4][13] = { {A0, 0, 0, 256, 768, 0, 512, 100, 0, 100, 0, 0, 0}, //motor 1
    {A1, 0, 0, 256, 768, 0, 512, 100, 0, 100, 0, 0, 0}, //motor 2
    {A2, 0, 0, 256, 768, 0, 512, 100, 0, 100, 0, 0, 0}, //motor 3
    {A3, 0, 0, 256, 768, 0, 512, 100, 0, 100, 0, 0, 0}}; //motor 4

    int GlobalPowerPin = A5; //use a potentiometer to define the power of the motion simulator on pin A11 (12)
    int GP_select = 0; //1:use potentiometer 0:always 100%
    double GlobalPower = 1.00; //100%

    int buffer=0;
    int buffercount=-1;
    int commandbuffer[5]={0};

    int disable = 1; //Motor stop flag

    SoftwareSerial SWSerial1(NOT_A_PIN, 8); //Pin 2 utilisé pour communiquer avec la Sabertooth
    SoftwareSerial SWSerial2(NOT_A_PIN, 10); //Pin 3 utilisé pour communiquer avec la Sabertooth
    Sabertooth ST1(128, SWSerial1); //nom de l'objet communication serie avec une pin differente de TX
    Sabertooth ST2(128, SWSerial2); //nom de l'objet communication serie avec une pin differente de TX

    void setup()
    {
    Serial.begin(57600);

    SWSerial1.begin(115200); //boutons 1 et 2 OFF
    SWSerial2.begin(115200); //boutons 1 et 2 OFF

    ST_stop();

    disable=1;

    pinMode(EmergencyPin, INPUT_PULLUP);


    #if FASTADC
    // set analogue prescale to 16
    sbi(ADCSRA,ADPS2) ;
    cbi(ADCSRA,ADPS1) ;
    cbi(ADCSRA,ADPS0) ;
    #endif
    }

    void ST_stop()
    {
    ST1.motor(1, 0);
    ST1.motor(2, 0);
    ST2.motor(1, 0);
    ST2.motor(2, 0);

    ST1.stop();
    ST2.stop();
    }

    void FeedbackPotWorker()
    {
    for(int z=0;z<4;z++)
    {
    motors[z][1]=0; //set to 0 all current analog
    }

    for(int i=0;i<ReadAnalog;i++)
    {
    for(int z=0;z<4;z++)
    {
    motors[z][1] += analogRead(motors[z][0]);
    }
    }

    for(int z=0;z<4;z++)
    {
    motors[z][1] /= ReadAnalog;
    }
    }

    void SerialWorker()
    {
    while(Serial.available())
    {
    if(buffercount==-1)
    {
    buffer = Serial.read();
    if(buffer != 'X')
    {
    buffercount=-1;
    }
    else
    {
    buffercount=0;
    }
    }
    else
    {
    buffer = Serial.read();
    commandbuffer[buffercount]=buffer;
    buffercount++;
    if(buffercount > 3)
    {
    if(commandbuffer[3]=='C')
    {
    ParseCommand();
    }
    buffercount=-1;
    }
    }
    }
    }

    void ParseCommand()
    {
    int diff;
    int i;
    bool commande = false;

    switch (commandbuffer[0])
    {
    case '1':
    i = 0;
    commande = true;
    break;

    case '2':
    i = 1;
    commande = true;
    break;

    case '3':
    i = 2;
    commande = true;
    break;

    case '4':
    i = 3;
    commande = true;
    break;

    case 'E':
    commande = false;
    unsigned long start;
    unsigned long time;
    start=millis();

    for(int z=0;z<4;z++)
    {
    motors[z][2]=motors[z][6]; //reset to standby position
    }

    time = millis();

    while(time < (start + 1500)) //1.5s
    {
    FeedbackPotWorker();
    CalculatePID();
    SetPWM();
    time=millis();
    }

    ST_stop();

    disable=1;
    break;

    default:
    commande = false;
    return;
    break;
    }

    if (commande)
    {
    commande = false;
    motors[2]=(commandbuffer[1]*256)+commandbuffer[2]; //target
    diff = constrain((GlobalPower * (motors[2] - 512)), -511, 511) + 512;
    motors[2]=map(diff,0,1023,motors[3],motors[4]); //ajustement en fonction des min et max
    disable=0;
    }
    }

    int updateMotorPid(int targetPosition, int currentPosition, int numMot)
    {
    float error = (float)targetPosition - (float)currentPosition;
    motors[numMot][10] += error;
    motors[numMot][11] = error;
    float pTerm_motor = ((float)motors[numMot][7] * error) / 100.0;
    float iTerm_motor = ((float)motors[numMot][8] * constrain(motors[numMot][10], -100, 100)) / 100.0;
    float dTerm_motor = ((float)motors[numMot][9] * (error - motors[numMot][11])) / 100.0;

    return constrain((pTerm_motor + iTerm_motor + dTerm_motor)/2, -127, 127);
    }

    void CalculatePID()
    {
    for (int z=0;z<4;z++)
    {
    motors[z][12] = updateMotorPid(motors[z][2],motors[z][1], z);
    }
    }

    void SetPWM()
    {
    for(int z=0;z<4;z++)
    {
    if (abs(motors[z][2] - motors[z][1]) <= (DeadZone + motors[z][5]))
    {
    motors[z][12] = 0;
    }
    }

    ST1.motor(1, motors[0][12]);
    ST1.motor(2, motors[1][12]);
    ST2.motor(1, motors[2][12]);
    ST2.motor(2, motors[3][12]);
    }

    void loop()
    {
    //Program loop
    while (1==1) //Important hack: Use this own real time loop code without arduino framework delays
    {
    FeedbackPotWorker();
    if (GP_select == 1)
    {
    GlobalPower = double(analogRead(GlobalPowerPin)) / 512.0 ;
    }
    else GlobalPower = 1.00;

    if (digitalRead(EmergencyPin) == LOW)
    {
    for (int z=0;z<4;z++)
    {
    motors[z][2] = motors[z][6]; //target = stdby
    }
    }

    else SerialWorker();

    CalculatePID();
    if(disable==0 || digitalRead(EmergencyPin) == LOW)
    {
    SetPWM();
    }
    }
    }
  2. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    20,460
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    144,596Coins
    Ratings:
    +10,741 / 52 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
  3. gigi

    gigi Active Member

    Joined:
    Nov 16, 2013
    Messages:
    466
    Balance:
    900Coins
    Ratings:
    +139 / 1 / -0
    My Motion Simulator:
    2DOF, JRK, 4DOF
    Hi noorbeast, thanks for your reply.
    The problem seems to be a conflict between simhub and simtools. the strange thing is that the problem does not appear as soon as I enter the game but after a few minutes. Just now, for example, an arduino board has crashed after almost 10 minutes of play. With simhub I use both bassshaker and shakelt. I noticed the simtools arduino board crashes and at the same time also my bassshaker and shakelt stops working. It is certainly not a problem of pc usb ports because for each card I use only one pc socket of the pc, i don't use multiple usb sockets.
    Also in SimHubWPF.exe.config I excluded the COM3 and COM4 ports as well as all the ports that don't have anything to do with simhub.
    Very very strange.
    Just close and restart simtools and everything goes back to working perfectly until the crash is suddenly repeated
  4. titomaky

    titomaky Member Gold Contributor

    Joined:
    Aug 4, 2008
    Messages:
    75
    Balance:
    175Coins
    Ratings:
    +23 / 1 / -0
    My Motion Simulator:
    Motion platform
    I have seen your simulator project and noticed that you are using a direct drive steering wheel, so I would advise you to try to isolate from your cockpit to be able to transmit electricity parasites to your arduino modules, as these are very sensitive and possibly This is the cause of your problem. sorry for the translation and I hope this helps your problem
  5. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    20,460
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    144,596Coins
    Ratings:
    +10,741 / 52 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
    That will likely be a port conflict, read through the thread here: https://www.xsimulator.net/community/threads/simtools-and-simhub-dashboard-no-working.11017/
  6. SeatTime

    SeatTime Well-Known Member

    Joined:
    Dec 27, 2013
    Messages:
    2,574
    Occupation:
    Retired
    Location:
    Brisbane Australia
    Balance:
    28,370Coins
    Ratings:
    +2,844 / 38 / -0
    My Motion Simulator:
    AC motor, Motion platform
  7. gigi

    gigi Active Member

    Joined:
    Nov 16, 2013
    Messages:
    466
    Balance:
    900Coins
    Ratings:
    +139 / 1 / -0
    My Motion Simulator:
    2DOF, JRK, 4DOF
  8. gigi

    gigi Active Member

    Joined:
    Nov 16, 2013
    Messages:
    466
    Balance:
    900Coins
    Ratings:
    +139 / 1 / -0
    My Motion Simulator:
    2DOF, JRK, 4DOF
    Thanks Titomaky for your reply and support. what you say should really be my problem, as like I wrote the problem hapen not when i start the game but after some minutes in game, with ac or dirt (the only 2 games I use).
    My osw is fixed on an iron plate but this in turn is fixed on the post which instead is made with aluminum profiles so in theory it should already be isolated. The OSW USB cables are wrapped together with all the other cables. It could be that the proximity between OSW USB cables and other USB cables (including those of the other Arduino running simhub or my steering wheel plate) generates interference that causes my problem. The only certainty is that when a simtools arduino board crashes simultaneously, my bassshaker and my shakelt also crashes
  9. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    20,460
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    144,596Coins
    Ratings:
    +10,741 / 52 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
    Have you excluded the com ports used by other devices in the SimHub settings?
  10. gigi

    gigi Active Member

    Joined:
    Nov 16, 2013
    Messages:
    466
    Balance:
    900Coins
    Ratings:
    +139 / 1 / -0
    My Motion Simulator:
    2DOF, JRK, 4DOF
    Yes sure in in SimHubWPF.exe.config i excluded COM3 and COM4 used by simtools and all other COM ports used by other devices.
    Is there another place in simhub where I need to exclude the COM port byother devices?
    With simhub open if I check I can see only the COM5 connected that is the ports of shakelt motors used with it.
  11. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    20,460
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    144,596Coins
    Ratings:
    +10,741 / 52 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
    That is all you need to exclude.

    Are you using ferrite clips on cables?

    Do you still get crashes when the wheel is off?
  12. gigi

    gigi Active Member

    Joined:
    Nov 16, 2013
    Messages:
    466
    Balance:
    900Coins
    Ratings:
    +139 / 1 / -0
    My Motion Simulator:
    2DOF, JRK, 4DOF
    In fact, everything suggests an interference since the crash occurs not immediately but after a few minutes.
    In truth, I didn't put the ferrite and maybe this could solve the problem. Should I put it on all USB cables? Where in the cable? Near the USB port on the PC or near the cable connection to the device? Any suggestion for ferrite to use?
    I have not tried with the steering wheel off, because if it is off I have no other peripherals to drive the car
    Thanks
  13. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    20,460
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    144,596Coins
    Ratings:
    +10,741 / 52 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
    You can use Live For Speed with a keyboard, so use that to test the wheel being off.

    Ferrite clips are pretty cheap so use them both ends, though the rule of thumb is closest to the suspect source of interference: https://www.ebay.com/sch/i.html?LH_...sacat=&_sadis=&_sop=12&_udhi=&_udlo=&_fosrp=1
  14. gigi

    gigi Active Member

    Joined:
    Nov 16, 2013
    Messages:
    466
    Balance:
    900Coins
    Ratings:
    +139 / 1 / -0
    My Motion Simulator:
    2DOF, JRK, 4DOF
  15. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    20,460
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    144,596Coins
    Ratings:
    +10,741 / 52 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
    Many peripheral power cords have ferrites as standard and they are likely worth adding if that is the suspected cause. But test the wheel being off first.
  16. gigi

    gigi Active Member

    Joined:
    Nov 16, 2013
    Messages:
    466
    Balance:
    900Coins
    Ratings:
    +139 / 1 / -0
    My Motion Simulator:
    2DOF, JRK, 4DOF
    Goodmorning everyone. I update my problem.
    At the moment I have not yet tried to play excluding my osw.
    I tried this:
    - Installed ferrite in all osw cables and in all other usb cables , including those of simtools arduino boards, the buttonplate arduino micro and the bodnar BU0036 which manages my gearbox and handbrake. (the ferrite was placed in the part of the cable that is near the boards). I put the ferrite also in all the power cables (the one that powers osw, the one that powers the pc, the one that powers the two psu cha, which in turn feed my sabertooth cards). Not put the ferrite only in the power cables of my dashboard tablet, in the cable that feeds my vpr and in the cable that feeds the electric motor of my cloth screen for the vpr.
    - Following this guide https://issuu.com/simracingcoach I picked up the ground of my osw's power supply by connecting it to the motor and also to a point of my rig. https://issuu.com/simracingcoach/docs/tutorial_eliminate_emi_issues.
    - I also tried to play without starting simhub and simdashboard.
    Despite these attempts, the problem remains and nothing has changed. For a few minutes everything seems to be working well but then suddenly one or both of the arduino boards lose the signal and their motors stop. Sometimes before that happens my bassshaker also stops working. As I have already written, it is sufficient to close and restart the simtools gameengine and everything returns to work perfectly until suddenly it stops to lock again.
    There are now some things to try, very important.
    1) as Noorbeast suggest I have to try to unplug my osw and try to play using a keyboard or mouse. Only then will I be able to understand if the problem really comes from OSW
    2) For my bassshaker I use a car amplifier 12 powered by a psu 220v input - 12v 50A output. For reasons of space and with the desire to have all my system as compact as possible I have fixed both the amplifier and the psu which feeds it on the external façade of my PC case. It seems a strange solution but positioned vertically they are very easy to access and make everything very compact. I will post a photo as soon as possible. The problem is that both the amplifier and the psu are made of metal and also my pc case on which they are fixed is metal. Both the PC and the psu that powers the amplicator are powered using a grounded cable but I wouldn't want there to be interference here.
    3)I would also like to try to create a plug that connected to my 220v network pick up the ground and take it to all my peripherals regardless of the fact that some of them that are powered at 220v (and not via usb) already have the ground present. Do you think it might be a good idea to connect all my peripherals to the ground of my power line (osw, fanatec pedals, pc, shifter, handbrake)?
    I also wonder if there may be a problem related to sound cards. for the bass shaker I use the sound card output incorporated in my mobo. I connected my amplifier to it. While for audio I use a soundblaster z card to which I have connected a soundbar.

    This problem, which, given the randomness with which it occurs, makes me think only of an interference that is taking away my sleep. It's a real pity because the system ... when it works ..... it works really well, very reactive and immersive. sighsigh
    Last edited: Jun 12, 2019
  17. Milani Fabio

    Milani Fabio Active Member

    Joined:
    Feb 21, 2019
    Messages:
    105
    Location:
    Italia
    Balance:
    714Coins
    Ratings:
    +74 / 2 / -0
    Hi Gigi,
    sorry for your problem, it just looks like a mess.
    In my opinion everything is to understand why the problem occurs after 10 minutes and not immediately.
    What stops you from the arduino, the power supply or the data signal.
    If it is feeding it could be something that after 10 minutes heats up and then goes into protection.
    If the data signal, the data comes from the PC ...

    From the height of my ignorance I cannot help you solve the problem, but only to add some doubts and other possibilities ...

    Soon my sim will be ready too, but I'll wait to stress you, because your already good problems ...
    Hello
  18. gigi

    gigi Active Member

    Joined:
    Nov 16, 2013
    Messages:
    466
    Balance:
    900Coins
    Ratings:
    +139 / 1 / -0
    My Motion Simulator:
    2DOF, JRK, 4DOF
    Hi Fabio, Thanks for your support.
    As I wrote in a previous post on the external wall of my PC case I fixed my car amplifier (renegade rxa550) and a PSU 220V -12v 50A power supply needed to power it. This psu was powered with a 3-wire cable and therefore also with the gnd cable.
    In my attempts I had tried to get into gaming excluding simhub but stupidly I had not tried removing power from the system which then remained on. So last night I disconnected the psu which then powers the amplifier and I entered the game. I don't know if it's a coincidence but I played for about 30 minutes, without bassshaker and with the related system disconnected and everything went perfectly. At the same time I had also added some additional ferrite on the cables of my Osw (now the ferrite is positioned at both ends of the USB cables) as well as in the power cable. Moreover, as I had written, I took the ground of my simcube's power supply, taking it up to the motor and from there to my chassis.
    Everything seems to be working yeahhh !.
    It is probable that it will be enough to bring a cable from the gnd of my psu that feeds the bassshaker amplifier up to the chassis of my pc.
    Ultimately, I think I can say with almost certainty that the problem is an interference (EMI) even though I haven't been able to understand where it came from (whether from the OSW, or from my bassshaker system). Before I can declare the problem solved I want to do further tests. I have also just purchased a pci usb 2.0 card with 4 usb 2.0 ports as suggested by simracing coach and tonight i will try to install it and at the same time connect the whole structure to the ground of my electrical network.
    It is the first time that I have faced a similar problem. I thought it was something that happens to others and never would have happened to me, instead ..... I treasure what happened and from now on I will pay a lot of attention to this kind of problem with careful grounding of each component.
    I will, however, keep you updated on the definitive outcome .... we hope well
    • Like Like x 1