View unanswered posts | View active topics It is currently Fri Aug 29, 2014 10:59 pm

 Page 1 of 1 [ 4 posts ]
 Print view Previous topic | Next topic
How to make motors coast instead of brake
Author Message
Rookie

Joined: Thu Dec 29, 2011 8:34 pm
Posts: 14
How to make motors coast instead of brake
With a tip from this thread, I was able to get the motors to stop dynamic braking at speed=0 and instead coast. The motivation was that the hard braking was damaging the gearboxes in our motors.

The main idea is "don't use speed = 0." Setting the speed to 1 avoids the braking mode. But this only works if the current speed was > 0 (forward). If the current speed was < 0 (backward), setting speed to +1 caused braking to happen. In this case using -1 avoided braking mode. In other words, the value to use for "coast" depends on the current direction of the motor.

Therefore this requires access to the current speed setting of the motor. I created a "speed-setting" function for each motor that remembers the speed it was set to:
 Code:   setLeftSpeed( speed );
These functions are used in place of
 Code:motor[left] = speed;

The function also contains the logic to set the speed to +1 or -1 if a speed of zero is requested, thus causing the motor to coast instead of brake:
 Code:short leftSpeed  = 0; // globalbool coastMode = true;voidsetLeftMotor( short speed ){  // the motor controller will brake (short) the motor  // if the speed value is zero.  But setting the motor to 1  // only makes it coast if the previous motor value was > 0.  // So we set it to -1 if the previous speed was < 0.  if ( speed == 0 && coastMode )  {    if ( leftSpeed < 0 )      speed = -1;    else      speed = +1;  }  leftSpeed  = speed; // remember it  motor[left] = speed;}
Below is an example of a simple "tophat drive" method that uses coasting:
 Code:// globals://// short rightSpeed = 0;short leftSpeed  = 0;void setLeftMotor( short speed );void setRightMotor( short speed );bool coastMode = true;task main(){  short speed = 75;  // (could be changed using buttons or joystick.)  initializeRobot();  waitForStart();   // wait for start of tele-op phase.  REQUIRED BY FIRST!  // let the driver press and hold RT to change from COAST to BRAKE mode.  if ( (joystick.joy1_Buttons & BTN_RT) )    coastMode =  false;  // brake  else    coastMode =  true;   // coast  // simple tophat drive: forward, backward, spin left, spin right:  switch( joystick.joy1_TopHat )  {  case 0:  // forward    setLeftMotor ( speed );    setRightMotor( speed );    break;  case 4:  // backward    setLeftMotor ( -speed );    setRightMotor( -speed );    break;  case 6:  // spin left    setLeftMotor ( -speed );    setRightMotor( +speed );    break;  case 2:  // spin right    setLeftMotor ( +speed );    setRightMotor( -speed );    break;  default:  // stop    {      setLeftMotor ( 0 );      setRightMotor( 0 );    }  }  // switch( joystick.joy1_TopHat )}voidsetLeftMotor( short speed ){  // the motor controller will brake (short) the motor  // if the speed value is zero.  But setting the motor to 1  // only makes it coast if the previous motor value was > 0.  // So we set it to -1 if the previous speed was < 0.  if ( speed == 0 && coastMode )  {    if ( leftSpeed < 0 )      speed = -1;    else      speed = +1;  }  leftSpeed  = speed; // remember it  motor[left] = speed;}voidsetRightMotor( short speed ){  // (see note about coasting in setLeftMotor()).  if ( speed == 0 && coastMode )  {    if ( rightSpeed < 0 )      speed = -1;    else      speed = +1;  }    rightSpeed = speed; // remember it.  motor[right] = speed;}

Wed Feb 08, 2012 9:27 am
Moderator

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Re: How to make motors coast instead of brake
Nice job on figuring this out. There are some easier ways to do these things though.
If you want your motors to coast, you can always use
 Code:bFloatDuringInactiveMotorPWM = true;

There is also no need to store the current value of the motor in your program. Just reading motor[motorName] will give you the speed that it's been set at.
If you still want to do it by yourself, making all these functions for every motor is a hassle. Use the tMotor variable instead:
 Code:void setMotorSpeed(tMotor myMotor, int speed){  if(speed == 0 && coast){    if(motor[myMotor] > 0){      motor[myMotor] = 1;    else if(motor[myMotor] < 0){      motor[myMotor] = -1;    }  }  else{    motor[myMotor] = speed;  }}

A more concise way to make the same functino would be:
 Code:void setMotorSpeed(tMotor myMotor, int speed){  if(speed == 0 && coast){    motor[myMotor] = sgn(motor[myMotor]);  }else{    motor[myMotor] = speed;  }}

_________________
sudo rm -rf /

Wed Feb 08, 2012 10:21 am
Rookie

Joined: Thu Dec 29, 2011 8:34 pm
Posts: 14
Re: How to make motors coast instead of brake
Thanks!! I imagined there was an easier way, but I haven't discovered extensive documentation.

I read post here that indicated that that bool flag was only meant for NXT motors, "not FTC". I just noticed that that post dates to 2008, so I guess it changed. (See the last post here: viewtopic.php?f=52&t=868&p=4728&hilit=motors+coast+nxt+motors+coast+brake&sid=ffebb1854d9c2c6faf8b2b03f80f62c4#p4728 )

Back in December I tried to read the motor value as you indicated. motor[left] looks like a pointer dereference, but the values I got back did not make any sense. So I figured it was some kind of special RobotC syntax. It's not? I wonder what I did wrong?

-norm

Wed Feb 08, 2012 5:22 pm
Moderator

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Re: How to make motors coast instead of brake
I did not know that you were referring to TETRIX motors in this post. Well, I guess that you should continue using the +-1 method. As for reading the motor value by calling motor[motorname], I know that this works on the VEX Cortex. I've not tested it with NXT/Tetrix yet, but I assume that it should work.

_________________
sudo rm -rf /

Wed Feb 08, 2012 6:16 pm
Display posts from previous:  Sort by
 Page 1 of 1 [ 4 posts ]

#### Who is online

Users browsing this forum: cookthebook and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    2014 Robotics Academy Summer of Learning    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions    Robotics Merit Badge Virtual NXT Robotics Discussions    General Discussions    Project Discussions International Forums    Spanish Forums       ROBOTC for MINDSTORMS       ROBOTC for VEX    French Forums       ROBOTC pour Mindstorms       ROBOTC pour IFI VEX    Japanese Forums （日本語のフォーラム）    German Forums Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge