2DOF Arduino Uno - MotorShield - 2 motor

Running simulator build projects.
Please use the image gallery for your pictures, a short tutorial can be found here.
The first image in the first post will be shown in the project gallery.

2DOF Arduino Uno - MotorShield - 2 motor

Postby flotis » Mon 22. Jun 2020, 18:02

Hi. I am building a 2 DOF simulator. Why do potentiometers have more travel to one side than to the other side? I have the potentiometers in the center, so the motors are stopped up to here everything correct. From the center if I turn the potentiometer counterclockwise it goes towards 0Kom, the motor turns but it stops very fast because I have very little travel on the potentiometer but if from the center of the potentiometer I turn it in favor of the needles The clock goes towards 10kom, the motor turns and faster and faster.
I show a video. As seen in the video, to the left the potentiometer has very little travel and stops very quickly and to the right it has more travel and increases speed. Does anyone know the reason?

[ Play Quicktime file ] Vídeo.MOV [ 3.51 MiB | Viewed 36362 times ]



Electric connections:
Arduino Uno and MotorShield
......GND <-> GND pin
........5V <-> 5V
.....pin 4 <-> pin 4
.....pin 5 <-> pin 5
.....pin 6 <-> pin 6
.....pin 7 <-> pin 7
.....pin 8 <-> pin 8
.....pin 9 <-> pin 9
Arduino and Motomonster stacked.JPG
Arduino and Motomonster stacked.JPG (12.7 KiB) Viewed 36362 times


cabling


Potenciometros


Can someone help me please
flotis
 
Posts: 11
Joined: Sun 5. Apr 2020, 18:13
Has thanked: 1 time
Been thanked: 0 time

Re: 2DOF Arduino Uno - MotorShield - 2 motor

Postby DangerouS » Thu 2. Jul 2020, 22:59

What code have you used to program the arduino?
Sirnoname 2013 or 2014?
DangerouS
 
Posts: 4
Joined: Fri 12. Dec 2014, 20:51
Has thanked: 0 time
Been thanked: 0 time

Re: 2DOF Arduino Uno - MotorShield - 2 motor

Postby flotis » Fri 3. Jul 2020, 15:23

I copied it from the following link: https://www.x-sim.de/forum/viewtopic.php?f=36&t=1617
The code used is the following:
Code: Select all
/*   
 Arduino code for dynamic playseat 2DOF
 Created 24 May 2011 by Jim Lindblom   SparkFun Electronics https://www.sparkfun.com/products/10182 "Example Code"
 Created 24 Apr 2012 by Jean David SEDRUE Version betatest26 - 24042012 http://www.gamoover.net/Forums/index.php?topic=25907
 Updated 20 May 2013 by RacingMat in english http://www.x-sim.de/forum/posting.php?mode=edit&f=37&t=943&p=8481  in french : http://www.gamoover.net/Forums/index.php?topic=27617
 Updated 30 April 2014 by RacingMat (bug for value below 16 corrected)
 */

#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 // 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[i], OUTPUT);
    pinMode(inBpin[i], OUTPUT);
    pinMode(pwmpin[i], OUTPUT);
  }
  // Initialize braked for motor
  for (int i=0; i<2; i++)
  {
    digitalWrite(inApin[i], LOW);
    digitalWrite(inBpin[i], 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(potL));
  Serial.print(";");
  Serial.println(analogRead(potR));
  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[i]>47 && x[i]<58 ){//for x0 to x9
      x[i]=x[i]-48;
    }                       
      if (x[i]>64 && x[i]<71 ){//for xA to xF
      x[i]=(x[i]-65)+10;       
    }
  }
  // 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;
}
flotis
 
Posts: 11
Joined: Sun 5. Apr 2020, 18:13
Has thanked: 1 time
Been thanked: 0 time

Re: 2DOF Arduino Uno - MotorShield - 2 motor

Postby pablobass » Fri 17. Jul 2020, 03:05

Hi,
Could it be anything to do with the potentiometers you are using ( logarithmic or Linear)?
pablobass
 
Posts: 7
Joined: Thu 9. Jul 2020, 04:08
Has thanked: 0 time
Been thanked: 0 time

Re: 2DOF Arduino Uno - MotorShield - 2 motor

Postby flotis » Fri 17. Jul 2020, 14:09

Hi, The Potentiometers are linear 10k. any other idea?. thank you.
flotis
 
Posts: 11
Joined: Sun 5. Apr 2020, 18:13
Has thanked: 1 time
Been thanked: 0 time

Re: 2DOF Arduino Uno - MotorShield - 2 motor

Postby pablobass » Sat 18. Jul 2020, 03:44

Hi,
I'm not sure if this is the issue but this is how I read this part of the code.. abs calculates the absolute value and gap is the current position of the motor as defined by the potentiometer. The code below calculates if the gap tolerance is close enough to stop the motor moving. if not it moves the motor to bring the gap within this tolerance. I am unsure why the code specifies the speed of the output should be different depending on how far the motor is from the target position. As for the potentiometer appearing unbalanced (Small movement one way making the motor turn slowly but a larger turn and a faster response) it does seem to be displaying the characteristics of a logarithmic potentiometer. If you have a test meter it might be worth checking in case they are faulty.

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 !

Hopefully this has been of some help
Paul.
pablobass
 
Posts: 7
Joined: Thu 9. Jul 2020, 04:08
Has thanked: 0 time
Been thanked: 0 time

Re: 2DOF Arduino Uno - MotorShield - 2 motor

Postby flotis » Tue 21. Jul 2020, 00:23

Hello, thank you very much for your answers. I have checked the potentiometers and I have tried other potentiometers and the same is still happening. I just don't understand what the problem is. I don't know what direction to take to solve the problem.
flotis
 
Posts: 11
Joined: Sun 5. Apr 2020, 18:13
Has thanked: 1 time
Been thanked: 0 time

Re: 2DOF Arduino Uno - MotorShield - 2 motor

Postby pablobass » Wed 22. Jul 2020, 14:22

Have you tried taking the potentiometers out of the circuit and supplying the switching 0v (gnd) and 5v manually to the Arduino anologue pins to see if this is still happening? (of course a direct Gnd or 5v will have the effect of telling the motors to run a full speed so caution is advised here). Have the motors got a circuit board attached or are they directly wired (if they have a circuit board this may be causing the issue). You could of course try a different sketch and see if the problem still exists...
pablobass
 
Posts: 7
Joined: Thu 9. Jul 2020, 04:08
Has thanked: 0 time
Been thanked: 0 time

Re: 2DOF Arduino Uno - MotorShield - 2 motor

Postby andres » Fri 24. Jul 2020, 05:02

Podrian orientarme que firmware estan usando? yo tengo arduino uno + motormonster shield. Muchas gracias
andres
 
Posts: 5
Joined: Wed 22. May 2019, 19:29
Has thanked: 0 time
Been thanked: 0 time

Re: 2DOF Arduino Uno - MotorShield - 2 motor

Postby flotis » Fri 24. Jul 2020, 09:17

Hola Andres. yo he seguido el siguiente enlace y descargado el codigo y todo desde este enlace.
https://www.x-sim.de/forum/viewtopic.php?f=36&t=1617.
Un saludo.
flotis
 
Posts: 11
Joined: Sun 5. Apr 2020, 18:13
Has thanked: 1 time
Been thanked: 0 time


Return to Motion simulator Projects in progress

Who is online

Users browsing this forum: No registered users and 1 guest