EasyC Vex Programming help in RC Mode

My son and I are quite new to Easy C, so your help is greatly appreciated.

We are now programming our vex bot for the FIRST Quad Quandary competition. Our basic setup works fine, but one of our functions works poorly if the motor goes too fast. We don’t have room to gear it down any more than it is, so we’re wondering if there is a way to program a speed limit into RC mode. As it is, it’s a little too easy to overdo it with the joystick.

Also, is there a way to have the vex controller initiate a preprogramed sequence similar to autonomous mode?

thanks,

Nathan’s Dad

This thread addresses making a motor respond more slowly:

You can use the buttons on the back of the transmitter to signal your code to start a sequence of commands.

Dad,

Did you check out the worm gear in the advanced gear kit? it provides a 24:1 reduction in a very small package.

If you decide that you must slow down the motor in software, keep in mind that a DC motor reduces speed linearly with voltage but the power is reduced by the square of the voltage. In other words, you will also be reducing torque…

Depending on your application, the mechanical option may be best.

Mike

If your mechanism does not need a lot of power (see Mike’s post), then you can limit the speed in software. First you would map the Rx Input to a variable. Since the values coming back from the transmitter range from 0 to 255, with 127 neutral, we subtract 127 so our values are now centered around 0. (Don’t declare your variable as an unsigned char, use int here!)

The next step is to multiply our new value times a coefficient between 0 and 1. But since the controller only likes to play well with integers, if we want the motor to only go 60% of max speed, we can multiply ourVariable = ourVariable * 6 / 10; with replacing ourVariable with whatever you declared your variable as.

Then we add 127 back into this, and set this value to the motors. By adjusting the coefficient, you can narrow down or widen how much available speed you want to send to a motor.

First of all, instead of just setting 60 in the code, wouldn’t it be easier to #define it at the start of the code? That way if you want to change it, you can just change it there and don’t have to fish for it in the rest of the code.


#define Percent_Speed 60 //defines the percent of max speed you want to go

The #define sets the value of Percent_Speed (the percentage of max speed you want). Since in this case it is set to 60, Percent_Speed is equal to 60 throughout the code.

I’m assuming you’re saying this to prevent overflow when you multiply the value by 6, and also so that you can go into negative numbers when you subtract 127

Instead of declaring an int and wasting memory, why wouldn’t you just declare the variable as an unsigned short and typecast the variable during the operation?

unsigned short ourVariable;

declare the variable as an unsigned short
Assign it to your input(not shown here).


ourVariable = (((int)ourVariable - 127) * Percent_Speed / 10) + 127; //calculations
motor = ourVariable; //output ourVariable to motor.

If you change motor to whatever the motor you want to output to, the code will take ourVariable, which you have mapped to your input earlier in the code and typecast it into an integer for that operation only (see below). It will then multiply ourVariable by the percentage of your speed and divide by 10 to reduce the power to the percent you set in #define Percent_Speed, add your 127 back to get the value within the accepted range again, and set it to your motor. And yes, I realize I have a pair of unneeded parenthesis in the line, but I like grouping things that way.

Inserting (int) in front of ourVariable makes the program treat ourVariable as an integer for that operation (to give it room to perform the operation), but once the operation is done, ourVariable will return to a nice little unsigned char that doesn’t hog memory.

How sensitive is it to a joystick?

If you are working where you only want it to react to the joystick to a point then what you could do is the following …

If GetRxInput (1,1) > Max Forward (you define)
SetMotor (1,Max Forward) - If joystick is pushed farther than desired position do not let motor go any faster

If GetRxInput (1,1) < Max Backwards (you define)
SetMotor (1,Max Backwards) - Same as above only backwards

Else
MotorRcControl (0,1,1,0) - Respond to joystick value to value (180 on Joystick= 180 to motor)

If you are Not using most of the joysticks movement before it gets out of hand then you could do the following …

If GetRxInput (1,1) > 135 -Allow play in joystick
SetMotor (1,speed desired forward) - you define

If GetRxInput (1,1) < 120 -Allow play in joystick
SetMotor (1,speed desired backwards) - you define

Else
SetMotor (1,127)

This gives the motor only two speeds that it is allowed to run.

Hope that helps. If you have any questions about this feel free to PM me.

-Sam

That code is much easier to manage as well. The code for that would be:


#define Motor_Max (put your maximum value here)
#define Motor_Min (put your minimum value here)

#define statements go at the top of your code


if (GetRxInput(1,1) > Motor_Max)
SetMotor(1, Motor_Max); //Don't go any faster than your max

else if (GetRxInput(1,1) < Motor_Min)
SetMotor(1, Motor_Min); //Likewise, don't let the motor go slower than the min, regardless of what the joystick says.

else
SetMotor(1, GetRxInput(1,1)); //If the joystick is not above the max or below the minimum, assign it directly to the input.

Feel free to PM me as well.

Exactly

-Sam