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 and X-Sim

Discussion in 'SimTools compatible interfaces' started by HammerHead, Oct 30, 2010.

  1. shomi87

    shomi87 New Member

    Joined:
    Nov 7, 2011
    Messages:
    10
    Location:
    Serbia
    Balance:
    57Coins
    Ratings:
    +0 / 0 / -0
    Hi everyone. I am also trying do it with arduino.

    I had a little time and play with PID library. I write code that simulate controlling one motor. I change PID library from Arduino site. I think this is good code. What are you think about it?

    Code:
    #include <PID_v1milos.h>
    
    /********************************************************
     * PID Basic Example
     * Reading analog input 0 to control analog PWM output 3
     ********************************************************/
    
    
    
    //Define Variables we'll be connecting to
    double Setpoint, Input, Output;
    
    int i;
    int Gforce = 0;
    int Gforce1 = 0;
    int Gforce10 = 0;
    int Gforce100 = 0;
    
    PID myPID(&Input, &Output, &Setpoint,2,0.05,0.25, DIRECT);
    
    void setup()
    {
      //initialize the variables we're linked to
      Input =90;
      Setpoint = 90;
    
      //turn the PID on
      myPID.SetMode(AUTOMATIC);
      Serial.begin(9600);
    }
    
    void loop()
    {
      if(Serial.available() > 0)
       {
         char tip;
         tip=Serial.read();
    
        delay(2);
        int Gforce100 = Serial.read()- '0';
        delay(2);
        int Gforce10 = Serial.read()- '0';
        delay(2);
        int Gforce1 = Serial.read()- '0';
        
        if (Gforce1 < 0) { Gforce = Gforce10 + 10*Gforce100; }
        if (Gforce1 < 0 && Gforce10 < 0) { Gforce = Gforce100; }
        if (Gforce100 < 0) { Gforce = 127; }
        if (Gforce1 >=0 && Gforce10 >= 0 && Gforce100 >= 0) { Gforce = 100 * Gforce100 + 10 * Gforce10 + Gforce1; }
         
        if(tip == 'p') //simulate reading potentiometer value with manual inputting potentiometer value in console in format pxxx
        {
            //i=0;
            Serial.println(InputX:  );
            Serial.println(Gforce);
            Input=double(Gforce);
        }
    
        if(tip == 's') //simulate setting new setpint value from PC with manual inputting  value in console in format sxxx
        {
            Setpoint=double(Gforce);
            Serial.println(Setpoint:  );
            Serial.println(Setpoint); 
        }
         
       }
       
       
      //if(i<10)
      myPID.Compute();
      
      while((Input<Setpoint-1 || Input>Setpoint+1) && Output!=0.0)
      {
        Serial.println(Input:  );
        Serial.println(Input);
        
        {
          myPID.Compute();
        
            Serial.println(output: );
            Serial.println(Output);
            
          if(myPID.smer==1) // myPID.smer tell us in what direction motor should turn
            Input+=Output/10; //simulate motor turning and potentiometer new value
            else
             Input-=Output/10;
             
            Serial.println(Input:  );
            Serial.println(Input);
            Serial.println(Direction);
            Serial.println(myPID.smer);
            Serial.println(----------------);
         }
         
      }
      
    }
    
    
    
    when you read potentiometer value you should MAP it from 0 to 180.
    same thing you should do with values that PC sends to Arduino.

    Here is my modded library :D

    How i could send data from arduino via x-sim to game???

    Attached Files:

  2. hraanan

    hraanan New Member

    Joined:
    Aug 10, 2011
    Messages:
    2
    Balance:
    1Coins
    Ratings:
    +0 / 0 / -0
    Hi,
    I have problems with how to define the arduino in x sim. I can't see the com of the arduino in the list!!
    if anyone can help I will be very happy.

    tnx
    hraanan