Offline
Newbie
Karma: 0
Posts: 24
|
 |
« on: October 07, 2012, 06:25:59 pm » |
So yes, I'm a noob. I have a DC motor connected to a L298N driver. The motor is spinning what I call a large plate. In the center of this plate I have a pot. I'm trying to get the plate to rotate in one direction until the pot reads 750, then reverse it's direction until the pot reads 300, then reverse again. Basically turning back & forth or Ping-Pong, but I'm having a hard time getting my head around the code. Can anyone give me some suggestions? I've tried stuff like this, but of course it's not working....: #define POT_PIN A8 #define MAX_LEFT 750 #define MAX_RIGHT 300
//Motor A int dir1PinA = 32; int dir2PinA = 34; int speedPinA = 36; int motor_speed; int dir = 0;
void setup() { pinMode (dir1PinA, OUTPUT); pinMode (dir2PinA, OUTPUT); pinMode (speedPinA, OUTPUT); motor_speed = 250; }
void loop() { int headRot = analogRead(POT_PIN); analogWrite (speedPinA, motor_speed); //Turn motor on
if (headRot < MAX_LEFT && dir == 0) // 750 { flipMotorDirection(1); dir = 1; Serial.println("dir = 1"); } if (headRot > MAX_RIGHT && dir == 1) // 300 { flipMotorDirection(0); dir = 0; Serial.println("dir = 0"); } }
void flipMotorDirection(int dir) { if (1 == dir) { digitalWrite (dir1PinA , LOW); digitalWrite (dir2PinA, HIGH); } else { digitalWrite (dir1PinA, HIGH); digitalWrite (dir2PinA, LOW); } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #1 on: October 07, 2012, 06:36:02 pm » |
but I'm having a hard time getting my head around the code. Can anyone give me some suggestions? The code does something. You want it to do something. What you want it to do is clear. What it actually does is not. We can't really help you until we know what the problem is.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6335
-
|
 |
« Reply #2 on: October 07, 2012, 07:34:28 pm » |
That code looks as if it is trying to power the motor in the target direction until it reaches the target, then reverses the motor direction and waits until the motor slows down, stops, accelerates in the reverse direction and reaches the other limit.
You don't say what it actually does, but this design is a recipe for a massively overshooting system which would be liable to be unstable and just overshoot further and further until it hits a mechanical limit somewhere.
I would have thought that a system with significant inertia as you're describing would benefit from using a PID algorithm to control the motor direction and speed. That way you could easily get it to move to the required position and stop.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #3 on: October 07, 2012, 08:06:18 pm » |
Thanks for the replays. Currently, when it's between 300 & 750 it alternates between 0 & 1 on every loop. dir = 1 dir = 0 dir = 1 dir = 0 dir = 1 dir = 0 dir = 1 dir = 0
e.g. Runs the motor at 1/2 speed. When the pot goes over 750, it runs at full speed, but doesn't reverse direction. When the pot goes under 300, it runs at full speed, and reverses direction. I'm not sure what a PID algorithm is or how to use it....?
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6335
-
|
 |
« Reply #4 on: October 07, 2012, 09:34:57 pm » |
Ignoring everything but the motor, are you able to control it so that it moves in the direction you want at the speed you want?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #5 on: October 07, 2012, 09:52:50 pm » |
Yes, if I run the code below, the motor runs as expected. In one direct for 4 sec. then in the other for 4 sec..... //Motor A int dir1PinA = 2; int dir2PinA = 3; int speedPinA = 5;
unsigned long time; int speed; int dir;
void setup () { pinMode (dir1PinA, OUTPUT); pinMode (dir2PinA, OUTPUT); pinMode (speedPinA, OUTPUT); time = millis (); speed = 200; dir = 1; }
void loop () { analogWrite (speedPinA, speed); // set direction if (1 == dir) { digitalWrite (dir1PinA , LOW); digitalWrite (dir2PinA, HIGH); } else { digitalWrite (dir1PinA, HIGH); digitalWrite (dir2PinA, LOW); } delay(4000); if (1 == dir) {dir = 0;} else {dir = 1;} }
It's just the logic with the pot code that I can't figure out.
|
|
|
|
« Last Edit: October 07, 2012, 09:58:07 pm by BazzaCAD »
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #6 on: October 08, 2012, 01:35:06 am » |
The code does something. You want it to do something.
That's what happens with my code! I want it to do something. It does something. But usually that is "something else". 
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6335
-
|
 |
« Reply #7 on: October 08, 2012, 02:10:25 pm » |
It's just the logic with the pot code that I can't figure out.
Initially I assumed that MAX_LEFT was less than MAX_RIGHT, and with that assumption your code seemed to make sense. But in fact MAX_LEFT is greater than MAX_RIGHT. That means that the comparisons you're applying to your pot values are back to front.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #8 on: October 08, 2012, 02:41:57 pm » |
OK I "think" I got it now. How does this look? Will this have the over shooting issue & hit a mechanical limit & break? #define POT_PIN A8 #define MAX_LEFT 750 #define MAX_RIGHT 300
int dir = 0; // 0 = Left, 1 = Right
void setup() { Serial.begin(9600); }
void loop() { int headRot = analogRead(POT_PIN); // Serial.println(headRot); // read-print pot if (headRot < MAX_LEFT) { if (headRot > MAX_RIGHT) { // Serial.println("Center"); } else { // Serial.println("Max Right"); dir = 0; } } else { // Serial.println("Max Left"); dir = 1; } if (dir == 0) { Serial.println("Turning Left"); } else { Serial.println("Turning Right"); } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #9 on: October 08, 2012, 03:11:53 pm » |
You never actually do anything with dir or motors...
If the potentiometer values represent limits, names like MIN_something and MAX_something make more sense than two MAX_somethings.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #10 on: October 08, 2012, 03:22:36 pm » |
You never actually do anything with dir or motors...
If the potentiometer values represent limits, names like MIN_something and MAX_something make more sense than two MAX_somethings.
Yes I was just testing to make sure I got the logic correct before connecting to the motor. Here it is with the motor code. Thanks for the tip on the MIN/MAX.. #define POT_PIN A8 #define MAX_LEFT 750 #define MIN_RIGHT 300
//Motor A int dir1PinA = 32; int dir2PinA = 34; int speedPinA = 36; int motor_speed = 250; int dir = 0; // 0 = Left, 1 = Right
void setup() { Serial.begin(9600); }
void loop() { int headRot = analogRead(POT_PIN); // Serial.println(headRot); // read-print pot analogWrite (speedPinA, motor_speed); //Turn motor on if (headRot < MAX_LEFT) { if (headRot > MIN_RIGHT) { // Serial.println("Center"); } else { // Serial.println("Min Right"); dir = 0; } } else { // Serial.println("Max Left"); dir = 1; } flipMotorDirection(dir); }
void flipMotorDirection(int dir) { if (0 == dir) { digitalWrite (dir1PinA , LOW); digitalWrite (dir2PinA, HIGH); } else { digitalWrite (dir1PinA, HIGH); digitalWrite (dir2PinA, LOW); } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #11 on: October 08, 2012, 03:28:12 pm » |
analogWrite (speedPinA, motor_speed); //Turn motor on Varying the motor speed as you get closer to the limit would be a good thing. That's what the PID idea is all about. Sneak up on the limits to avoid overshooting. You only need to flip the motor direction if it changes, not on every pass through loop. Your commented out Serial.print() information does not reflect reality.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #12 on: October 08, 2012, 03:35:18 pm » |
Varying the motor speed as you get closer to the limit would be a good thing. That's what the PID idea is all about. Sneak up on the limits to avoid overshooting.
You only need to flip the motor direction if it changes, not on every pass through loop.
Your commented out Serial.print() information does not reflect reality.
Thanks for the info. So I should slow down the motor as I get closer to the limit?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #13 on: October 08, 2012, 03:39:14 pm » |
So I should slow down the motor as I get closer to the limit? Yes. Make the speed depend on the absolute value of the difference between the headRot value and the middle of the range. As the difference gets larger, slow down. As it gets smaller, speed up. The maximum speed, then, would be right in the center of the range.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #14 on: October 08, 2012, 04:05:28 pm » |
Yes. Make the speed depend on the absolute value of the difference between the headRot value and the middle of the range. As the difference gets larger, slow down. As it gets smaller, speed up. The maximum speed, then, would be right in the center of the range.
Could you give me a hint on how to code that?
|
|
|
|
|
Logged
|
|
|
|
|
|