Go to Post Instead of arguing about what should have been, learn from the breaks, and celebrate the gracious professionalism being displayed by the other 99% of teams. Word. - PVCMike [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 01-12-2004, 19:36
stephenthe1 stephenthe1 is offline
Registered User
#1008
 
Join Date: Dec 2004
Location: Ohio
Posts: 113
stephenthe1 is on a distinguished road
Exclamation pwm ouput limiting

hello,
ok, so say I have a robot controlled by two joysticks. I want to make it so I can simply press a button on either of the joysticks to increase the pwm value by 4 every time the button is pressed. (the robot has two motors for each side of the tank-style robot). this seems like a good idea, but it means I have to limit the maximum value for the y-axis on each controller to something like 138. this way I can add 4 to its power each time the button on the appropriate joy stick is pressed, without going over 254. is there a simple way to limit the joy sticks top output value to 138?

ps-I am aware that shaft encoders are the new fad, and are far more accurate, but this would simply be a way to help me learn the code better and it is a temporary fix to the problem untill I have some good time to understand how to program the shaft encoders.

thanks for your patience,
Stephen
  #2   Spotlight this post!  
Unread 01-12-2004, 19:42
Joshua May's Avatar
Joshua May Joshua May is offline
Go Bears!
FRC #1110 (Binary Bulldogs)
Team Role: College Student
 
Join Date: Nov 2003
Rookie Year: 2003
Location: Berkeley, CA
Posts: 1,306
Joshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond reputeJoshua May has a reputation beyond repute
Send a message via AIM to Joshua May
Re: pwm ouput limiting

If I understand you correctly, then this should work. I can't remember the exact two-joystick code, but this concept should be valid.

Code:
if (p1_sw_top)
{
  pwm01 = p1_y + 4;
  pwm02 = p2_y + 4;
}
if (pwm01 > 138)
  pwm01 = 138;
if (pwm02 > 138)
  pwm02 = 138;
__________________
The FIRST Wiki - openFIRST - Ultimate Robot Challenge - URC Wiki
I currently have 50 GMail invites, PM or email me for one.
UC Berkeley Class of 2009

2005 Las Vegas Regional Autodesk Visualization Award
2005 Las Vegas Regional #8 Seeded Alliance with 988 and 1505
2006 Southern California Regional #15 seed

Last edited by Joshua May : 01-12-2004 at 19:45.
  #3   Spotlight this post!  
Unread 01-12-2004, 20:15
stephenthe1 stephenthe1 is offline
Registered User
#1008
 
Join Date: Dec 2004
Location: Ohio
Posts: 113
stephenthe1 is on a distinguished road
Re: pwm ouput limiting

Quote:
Originally Posted by Joshua May
If I understand you correctly, then this should work. I can't remember the exact two-joystick code, but this concept should be valid.

Code:
if (p1_sw_top)
{
  pwm01 = p1_y + 4;
  pwm02 = p2_y + 4;
}
if (pwm01 > 138)
  pwm01 = 138;
if (pwm02 > 138)
  pwm02 = 138;
thanks for the help. by the way, I meant 238, not 138. (just stating that cause I feel dumb ) no matter, thanks for the help
  #4   Spotlight this post!  
Unread 01-12-2004, 20:17
stephenthe1 stephenthe1 is offline
Registered User
#1008
 
Join Date: Dec 2004
Location: Ohio
Posts: 113
stephenthe1 is on a distinguished road
Re: pwm ouput limiting

however, that won't work. because every time the code is read again, the new speed (238 + 4) will be reset right back to 238 again. considering the code is read every like twent-something milliseconds, the robot would never increase its speed.
  #5   Spotlight this post!  
Unread 01-12-2004, 20:22
Rickertsen2 Rickertsen2 is offline
Umm Errr...
None #1139 (Chamblee Gear Grinders)
Team Role: Alumni
 
Join Date: Dec 2002
Rookie Year: 2002
Location: ATL
Posts: 1,421
Rickertsen2 has a brilliant futureRickertsen2 has a brilliant futureRickertsen2 has a brilliant futureRickertsen2 has a brilliant futureRickertsen2 has a brilliant futureRickertsen2 has a brilliant futureRickertsen2 has a brilliant futureRickertsen2 has a brilliant futureRickertsen2 has a brilliant futureRickertsen2 has a brilliant futureRickertsen2 has a brilliant future
Send a message via AIM to Rickertsen2 Send a message via Yahoo to Rickertsen2
Re: pwm ouput limiting

Quote:
Originally Posted by Joshua May
If I understand you correctly, then this should work. I can't remember the exact two-joystick code, but this concept should be valid.

Code:
if (p1_sw_top)
{
  pwm01 = p1_y + 4;
  pwm02 = p2_y + 4;
}
if (pwm01 > 138)
  pwm01 = 138;
if (pwm02 > 138)
  pwm02 = 138;
be careful. This won't work exactly as expected. The code above will increment the PWM value EVERY PROGRAM LOOP THAT THE BUTTON IS PRESSED FOR. While you may not press the button for very long, ALOT of program loops occur in this time and your pwm value will go haywire. To prevent this, you need to only increment once when the button is first pressed. An example of how to check if a button was jsut pressed goes somethign like the following:
Code:
int previous_Button_State = 0;

//check to see if the button was just pressed.
if (p1_sw_top & !previous_Button_State)
{
  //do stuff here
}
previous_Button_State = p1_sw_top;

Quote:
thanks for the help. by the way, I meant 238, not 138. (just stating that cause I feel dumb ) no matter, thanks for the help
I was a bit confused as to why you are limiting it that low.
__________________
1139 Alumni

Last edited by Rickertsen2 : 01-12-2004 at 20:27.
  #6   Spotlight this post!  
Unread 02-12-2004, 08:39
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,801
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: pwm ouput limiting

Here’s a variation.
If you mean to increase the top joystick speed (originally 238) by 4 every time the button is pushed then you’ll need to preserve the last known value. Expanding on the Joshua and James examples (but only looking at one of the joysticks to simplify):
Code:
static unsigned char rightTopSpeed=238;
static char previous_top_Button_State = 0, previous_trig_Button_State = 0; //FALSE
 
if (p1_sw_top & !previous_top_Button_State)
{
	if (rightTopSpeed < 251) // Limit how high it can go
	{
		rightTopSpeed+= 4;
	}
	previous_top_Button_State = p1_sw_top;
}
 
// You might want to be able to decrement as well
else if (p1_sw_trig & !previous_trig_Button_State) 
{
	if (rightTopSpeed > 238) // Limit how low it can go
	{
		rightTopSpeed-= 4;
	}
	previous_trig_Button_State = p1_sw_trig;
}
 
if (pwm01 > rightTopSpeed)
	pwm01 = rightTopSpeed;
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle

Last edited by Mark McLeod : 02-12-2004 at 13:23. Reason: Changed check from 254 to 251 --Thanks to Greg
  #7   Spotlight this post!  
Unread 02-12-2004, 12:40
Greg Ross's Avatar
Greg Ross Greg Ross is offline
Grammar Curmudgeon
AKA: gwross
FRC #0330 (Beach 'Bots)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Hermosa Beach, CA
Posts: 2,245
Greg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond repute
Send a message via AIM to Greg Ross Send a message via Yahoo to Greg Ross
Re: pwm ouput limiting

Quote:
Originally Posted by Mark McLeod
Code:
	if (rightTopSpeed < 254) // Limit how high it can go
That should probably be
Code:
if (rightTopSpeed < 250) // Limit how high it can go
otherwise your top speed could go as high as 257 -- which, i suspect, would cause unexpected results.
__________________
Greg Ross (The Grammar Curmudgeon formerly known as gwross)
S/W Engineer, Team 330, the Beach 'Bots
<--The Grammar Curmudgeon loves this cartoon.
“Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" Hunter S. Thompson
"Playing a practical joke means doing something mean and calling it funny." Me
  #8   Spotlight this post!  
Unread 02-12-2004, 13:20
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,801
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: pwm ouput limiting

Quote:
Originally Posted by gwross
That should probably be
Code:
if (rightTopSpeed < 250) // Limit how high it can go
otherwise your top speed could go as high as 257 -- which, i suspect, would cause unexpected results.
You know, I actually thought about that too, but forgot to carry through on the thought.

Thanks Greg
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle
  #9   Spotlight this post!  
Unread 03-12-2004, 22:07
stephenthe1 stephenthe1 is offline
Registered User
#1008
 
Join Date: Dec 2004
Location: Ohio
Posts: 113
stephenthe1 is on a distinguished road
Lightbulb Re: pwm ouput limiting

would this code work to help keep the robot from swerving?(uses tank style control with pwm_01 and pwm_02 on one side and pwm_03 and pwm_04 on the other side of the robot) It should. also let me know if there are any errors in it.

Code:
	signed char distance = p1_y - p2_y;
	if (distance < 0)
	distance = -(distance);     //the absolute value of distance
	else if (distance <= 14)
	{
	p1_y = p2_y = (p1_y + p2_y)/2;
	}
	pwm01 = pwm02 = p1_y;
	pwm03 = pwm04 = p2_y;
the only possible problem I can see is how it converts (but doesn't really convert) a signed char to an unsigned char (the pwm values). is this a problem?

Last edited by stephenthe1 : 03-12-2004 at 22:10.
  #10   Spotlight this post!  
Unread 04-12-2004, 19:40
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: pwm ouput limiting

Quote:
Originally Posted by stephenthe1
Code:
	signed char distance = p1_y - p2_y;
	if (distance < 0)
	distance = -(distance);     //the absolute value of distance
	else if (distance <= 14)
	{
	p1_y = p2_y = (p1_y + p2_y)/2;
	}
	pwm01 = pwm02 = p1_y;
	pwm03 = pwm04 = p2_y;
the only possible problem I can see is how it converts (but doesn't really convert) a signed char to an unsigned char (the pwm values). is this a problem?
No, p1_* is unsigned char. The problem may be, though, if the sum of p1_y and p2_y is greater than 255. This would cause wrap around. (eg, 128+128 equals 0).

this would work, though
Code:
p1_y = p2_y = ((long)p1_y + p2_y)/2;
  #11   Spotlight this post!  
Unread 05-12-2004, 16:57
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,801
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: pwm ouput limiting

Jamie's example gets you closer to what you want to experiment with.
I have another quick note on your code:
Code:
signed char distance = p1_y - p2_y;

Will result in a compile error because the declaration of a variable (“signed char distance”) is fixed at compile time, and the value assigned can only be a constant. Since in this case p1_y & p2_y are variables and can be different each time the routine is called, to get past the compiler this would need to be expressed as:
Code:
signed char distance;
distance = p1_y - p2_y;

The first statement is evaluated at compile time (on your PC) and the second will be evaluated at run time (when your code is running in the controller).
------------------
Your method of correcting will expose another problem with driving straight.
The right/left drive trains are rarely 100% balanced. Manufacturing differences in the motors, team fabrication differences in the friction between the gears/sprockets, chains, slight binding and misalignment, the left/right balance of the robot, and so on. This usually means that to drive straight the motors on one side might get a pwm of say 200, while the motors on the other side need to get 180.

Now a driver corrects for this automatically through a little practice with the robot by pushing the throttle on one side a little more than the other until the robot is seen to be driving straight. If you implement your proposed scheme the code will override this minor correction on the part of the driver and it might actually be harder to drive straight.

(I have an ulterior motive here to introduce you to a primitive form of proportional correction, the first step into PID.)

An easy method is to slow the more powerful side down to match the less powerful side as a percentage of full power, e.g., if p1_y is positive…

stronger side power = p1_y - (p1_y/127 * constant)
weaker side power = p2_y

The simplest way to select constant is:

- Run the robot at full power and note which side lags.
- Pick a value for constant above, put it into the code and run the robot again.
- Repeat until you are driving straight.

These corrective values will be different for driving forwards and backwards, so add an if statement for that.
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle

Last edited by Mark McLeod : 05-12-2004 at 17:00.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using an Operator Interface with the 2004 EDU RC wirelessly Dave Flowerday Robotics Education and Curriculum 34 19-04-2004 19:06
Fading LEDs using a PWM signal from the Edubot DanL Programming 22 30-03-2004 00:07
pwm 13-15 wayne 05 Programming 2 04-10-2003 12:08
PWM and burning out motors patrickrd Technical Discussion 7 19-06-2003 15:30
Limiting PWM in pbasic archiver 2001 4 24-06-2002 00:38


All times are GMT -5. The time now is 02:51.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi