Arduino Forum

Using Arduino => Motors, Mechanics, Power and CNC => Topic started by: swapnil_nimbalkar on Sep 17, 2020, 10:09 am

Title: PC Steering wheel centering
Post by: swapnil_nimbalkar on Sep 17, 2020, 10:09 am
Hello everyone,

I want to make a steering wheel for PC.  I want to make a centering mechanism for it.  Arduino will be used only to center the steering wheel.

Can someone help me with what components to use and code.

Here is what I am thinking of using

1) Disc encoder from old printer or rotary encoder
2) L298 Motor controller
3) Arudino Uno
4) 775 DC Motor

Thank you
Title: Re: PC Steering wheel centering
Post by: Robin2 on Sep 17, 2020, 10:46 am
In what circumstances do you want the Arduino to centre the wheel?

How do you define the centre position?

An L298 is almost certainly completely inadequate for a big 775 motor - what is the stall current for the motor.?

...R
Title: Re: PC Steering wheel centering
Post by: swapnil_nimbalkar on Sep 20, 2020, 06:45 pm
hi,

I am going to use an encoder to center the wheel. 


I have tested the code with LED's.  It works as intended.  Will test it with L298 and motor soon.


Here is the code that I am planning to use.

Swapnil
Title: Re: PC Steering wheel centering
Post by: swapnil_nimbalkar on Sep 20, 2020, 07:01 pm
Code: [Select]
[code]
 /*
 Steering wheel centering mechanism.
 This mechanism is independent of input that is going to the PC.  A mouse is mounted on a round shaft (shaft can rotate but mouse stays at its position), the input from mouse is used to control the steering of truck in the game (ETS2).
 An encoder is attached on one end of the shaft and the steering wheel is on the other end.  A DC motor connected through gears or pully is used to return the shaft to the center.  L298 controls the DC Motor.
 To be noted
 1) encoder has 38clicks for 360 degree rotation
 2) steering is designed to move 900 degrees, how to stop it beyond that
*/
 
 #define inputCLK 4 //input pin CLK from encoder is attached to the digital pin 4 on uno
 #define inputDT 5 //input pin DT from encoder is attached to the digital pin 5 on uno
 
 #define in1 8 //output pin - when this pin is high motor rotates in CW direction
 #define in2 9 //output pin - when this pin is high motor rotates in CCW direction
 #define enA 3 //PWM signal to enable motor rotation

 int counter = 100; //this is center position of the encoder
 int currentStateCLK;
 int previousStateCLK;

 String encdir ="";

 void setup() {
   
   // Set encoder pins as inputs 
   pinMode (inputCLK,INPUT);
   pinMode (inputDT,INPUT);
   
   // Set motor pins as outputs
   pinMode (in1,OUTPUT);
   pinMode (in2,OUTPUT);
   pinMode (enA,OUTPUT);
   
   // Setup Serial Monitor
   Serial.begin (9600);
   
   // Read the initial state of inputCLK
   // Assign to previousStateCLK variable
   previousStateCLK = digitalRead(inputCLK);

 }

 void loop() {
 
  // Read the current state of inputCLK
   currentStateCLK = digitalRead(inputCLK);
   
   // If the previous and the current state of the inputCLK are different then a pulse has occured
   if (currentStateCLK != previousStateCLK){
       
     // If the inputDT state is different than the inputCLK state then
     // the encoder is rotating counterclockwise
     if (digitalRead(inputDT) != currentStateCLK)
     {
       counter --;
       encdir ="CCW";
     }
     else
     {
       // Encoder is rotating clockwise
       counter ++;
       encdir ="CW";

     }
      previousStateCLK = currentStateCLK; //update previousStateCLK with the current state

       
       if (counter > 101) // if steering is moved, encoder should move too, thus we know motor need to start tuning to bring steering back to center
       {
       digitalWrite(in1, HIGH);
       digitalWrite(in2, LOW);
       digitalWrite(enA, HIGH); // move motor although this is pwm pin I am not using PWM right now
       }

       else if (counter < 99)
       {
       digitalWrite(in1, LOW);
       digitalWrite(in2, HIGH);
       digitalWrite(enA, HIGH); // move motor
       }

       else
       {
       digitalWrite(in1, LOW);
       digitalWrite(in2, LOW);
       digitalWrite(enA, LOW); // stop motor
       }

     Serial.print("Direction: ");
     Serial.print(encdir);
     Serial.print(" -- Value: ");
     Serial.println(counter);
   }
 }
   
   
 
[/code]
Title: Re: PC Steering wheel centering
Post by: Robin2 on Sep 20, 2020, 08:09 pm
I am going to use an encoder to center the wheel. 


I have tested the code with LED's.  It works as intended.  Will test it with L298 and motor soon.
I assume that means you don't need any further assistance because you have not answered my two questions.

...R
Title: Re: PC Steering wheel centering
Post by: swapnil_nimbalkar on Oct 03, 2020, 06:15 pm
I assume that means you don't need any further assistance because you have not answered my two questions.

...R
No its not like that.  I think I was not able to put it in proper words to understand.
Title: Re: PC Steering wheel centering
Post by: swapnil_nimbalkar on Oct 03, 2020, 06:25 pm
Here is an update,

I wanted to use a normal encoder (KY-40) for centering.  But it is not precise and sometimes jumps and won't center properly.

So, I changed my mind, I am going to use a PS2 mouse as an encoder, which is very precise.

The mouse will be mounted on steering shaft to record shaft position.  Accordingly Arduino will control DC motor to center the shaft. 

I have tested the PS2 mouse with L298N and 12V DC Motor. It does move motor as intended.

I would like to add PID control to this system. Can someone help me with the PID loop?

Here's the code

Code: [Select]
#include "PS2Mouse.h"

#define in1 8 //output pin - when this pin is high motor rotates in CW direction
#define in2 9 //output pin - when this pin is high motor rotates in CCW direction
#define enA 10 //PWM signal to enable motor rotation

int counter = 5000;
int motorSpeed = 0;
int counter1 = 0;

PS2Mouse mouse(6,5);

void setup()
  {
  Serial.begin(9600);
// Set motor pins as outputs
   pinMode (in1,OUTPUT);
   pinMode (in2,OUTPUT);
   pinMode (enA,OUTPUT);
 
  }

void loop(){
  uint8_t stat;
  int x,y;
  mouse.getPosition(stat,x,y);

  if (x>0)
  {
    counter = counter + x;
  }
  else if (x<0)
  {
    counter = counter + x;
  }
 
  Serial.println(counter);

  if (counter > 5050) // if steering is moved, mouse should detect the movement, thus we know motor needs to start tuning to bring steering back to center
       {
       digitalWrite(in1, HIGH);  // move motor in CW direction
       digitalWrite(in2, LOW);
       motorSpeed = map(counter, 5050, 8000, 0, 255);
        }

       else if (counter < 4950)
       {
       digitalWrite(in1, LOW);
       digitalWrite(in2, HIGH); // move motor in CCW direction
       counter1 = counter1 - 10000 ;
       counter1 = counter1 * -1;
       motorSpeed = map(counter1, 5050, 8000, 0, 255);
       }

       else
       {
       motorSpeed = 0; // stop motor
       }

       if (motorSpeed > 255) motorSpeed = 255;

       analogWrite (enA, motorSpeed);
 
}


Title: Re: PC Steering wheel centering
Post by: Robin2 on Oct 03, 2020, 06:27 pm
No its not like that.  I think I was not able to put it in proper words to understand.
It's not a good idea to allow a long time between your Replies because we forget all about your problem. I suggest you check the Forum about twice per day to see if someone has responded in case you need to provide more information.


In Reply #2 you said "Here is the code that I am planning to use." That was nearly 2 weeks ago so I presume you have tested the program in the meantime, but you have no told us what happened when you tried it.


And you have still not answered my questions from Reply #1

...R
Title: Re: PC Steering wheel centering
Post by: swapnil_nimbalkar on Oct 03, 2020, 09:00 pm
In what circumstances do you want the Arduino to centre the wheel?

How do you define the center position?

An L298 is almost certainly completely inadequate for a big 775 motor - what is the stall current for the motor.?

...R
1) Center position is recorded when Arduino boots up (counter = 5000), now if I move my steering wheel this value will change depending upon which way I am going.
2) As you had pointed out that L298 wont suffice 775, so I am going to use some other geared motor which wont pull much current.  Since I dont wont force feedback other motor should fine too.

Stall current for 775 motor is about 14A.


It's not a good idea to allow a long time between your Replies because we forget all about your problem. I suggest you check the Forum about twice per day to see if someone has responded in case you need to provide more information.


In Reply #2 you said "Here is the code that I am planning to use." That was nearly 2 weeks ago so I presume you have tested the program in the meantime, but you have no told us what happened when you tried it.


And you have still not answered my questions from Reply #1

...R
I really wanted to try and make my own code which took time.  Ofcourse I learned a lot during this process, that why I replied late.

In Reply #2 you said "Here is the code that I am planning to use." That was nearly 2 weeks ago so I presume you have tested the program in the meantime, but you have no told us what happened when you tried it.


And you have still not answered my questions from Reply #1

...R
Yes I did test the program on work bench.  When I move mouse "counter" value changes and accordingly motor direction and PWM output changes too.  

Now I want to add PID to this setup, can you help me with that?
Title: Re: PC Steering wheel centering
Post by: Robin2 on Oct 04, 2020, 10:15 am
2) As you had pointed out that L298 wont suffice 775, so I am going to use some other geared motor which wont pull much current.  Since I dont wont force feedback other motor should fine too.
I'm still not clear about when you want the self-centering to happen (my first question). I'm beginning to think you want to create a system that creates a small force opposing the driver so that if he takes his hands off the wheel it will automatically return to the centre.

If that is what you want you may find that a small geared motor is not suitable because when the person turns the wheel s/he will be forcing the gears to turn the motor and they are not generally designed for that. A more powerful motor with a direct (or nearly direct) drive would be more suitable.



Quote
Now I want to add PID to this setup, can you help me with that?
I'm not going to offer helo with that. I have done it for myself but it is not something I feel comfortable teaching. There is a nice simple PID function in this link (https://forum.arduino.cc/index.php?topic=633678.msg4290897#msg4290897)

...R
Title: Re: PC Steering wheel centering
Post by: swapnil_nimbalkar on Oct 04, 2020, 05:24 pm
As I said earlier, I am not be able to put whatever is in my mind into proper words for you to understand.

Thank you for the link.  I know about PID, but dont know how to implement it in programming language.

Your help is much appreciated.

Regards
Swapnil
Title: Re: PC Steering wheel centering
Post by: Robin2 on Oct 04, 2020, 06:12 pm
As I said earlier, I am not be able to put whatever is in my mind into proper words for you to understand.
That's why I wrote (in Reply #9) my description of what I think is in your mind - did I get it right, or am I wrong?

...R
Title: Re: PC Steering wheel centering
Post by: swapnil_nimbalkar on Oct 06, 2020, 08:28 pm
That's why I wrote (in Reply #9) my description of what I think is in your mind - did I get it right, or am I wrong?

...R
You are absolutely correct.