# Programming ?

So I was programming my autonomous code and it kept turning the robot. I know that if you program it to do the wheel rotations it wont. My only question is how do you do the rotations code?

`````` 1. What programming system are you using? RobotC? EasyC? PROS? ConVEX?
2. What is your drive base? Tank? Holonomic? Banshee?
3. What is the "rotations code"? Specify what you want the robot to do; include pictures for bonus points
4. How are your motors plugged into the cortex? Which wheel motors are plugged where?``````

RobotC
idk
Move forward/backward
FrontLeft port 2
FrontRight port 3
BackLeft port 8
BackRight port 9

For a safe bet, we’re going to assume tank drive, and rotation code means how many rotations the wheel moves forward or backward. Use Quad encoders on your wheels to measure rotations, They are those red boxes that you can put a shaft through, and they measure rotation in ticks.

Is there special code?

Hello,

Please try to understand that the community needs more information before we can help you. We are by all means dedicated to helping you solve a problem you have, but you’re going to have to give some detail. Using informal text and slang and speaking in short lines are not going to help us interpret your question any better. Please give at least a paragraph explaining your problem, or what you need help with.

There is no “special code”. There are statements and functions. You use these statements and functions in the most dumbed-down way possible to make a robot do what you need it to do.

If you’re using RobotC, try looking up videos on YouTube on how to do the basics, which is what you’re asking for from what I can tell.

About your drive base, do you have one that looks like this?

Like this?

Well… There is many different assortments to knowing for how long you should have your robot turning.

1. Using encoders
2. Using a gyroscope
3. Using time by trial-and-error
• Using Encoders:
1. First, find the circumference of the wheel. Grab the diameter of the wheel and multiply it by 3.14. example: 4" (Diameter of wheel) x 3.14 = 12.56

2. If you are using encoders, first the best way to get the closest-to-exact angle is by measuring the distance from the top-left wheel (Where the wheel touches the ground) to the bottom-right wheel (Where the wheel touches the ground) (This should be the diameter of the path of turning). You multiply that by 3.14(estimated) which will become the circumference. The circumference of that is the length in which would be a full 360 degrees of the robot.
example: 20" (Length from top-left to bottom-right) x 3.14 = 62.8 (Circumference of path the robot travels)

3. What you would do it divide your circumference of the path the robot travels by the robot’s’ wheel circumference. This should give you the amount of rotations to complete a full 360.
example: 62.8 (path circumference) / 12.56 (wheel circumference) = 5 (Rotations for a full 360)

4. Multiply the amount of rotations by 360 (The amount of ticks for a full revolution of an encoder) and that should be the closest-to-exact angle for the robot to make a full rotation.
example: 5 (Rotations for a full 360) * 360 (Ticks for a full rotation for the encoder) = 1800 (Ticks for the robot to create a full 360 degrees)

Coding using an encoder example:

``````
while(SensorValue[Encoder1]<1800){
motor[frontLeft]=127;
motor[backLeft]=127;
motor[frontRight]=-127;
motor[backRight]=-127;
}
motor[frontLeft]=0;
motor[backLeft]=0;
motor[frontRight]=0;
motor[backRight]=0;

``````
• Using gyroscope:
If you want the robot to do a full 360, you only need the robot to be turning with a wait loop for the value to reach 3600 (The gyroscopes’ angle is 10x an angle in degrees).
``````
while(SensorValue[Gyroscope1]<3600){
motor[frontLeft]=127;
motor[backLeft]=127;
motor[frontRight]=-127;
motor[backRight]=-127;
}
motor[frontLeft]=0;
motor[backLeft]=0;
motor[frontRight]=0;
motor[backRight]=0;

``````
• Using Time:
If you are using time, it’s best to just use trial-and-error.
``````
motor[frontLeft]=127;
motor[backLeft]=127;
motor[frontRight]=-127;
motor[backRight]=-127;
wait1msec(3000)//3 seconds
motor[frontLeft]=0;
motor[backLeft]=0;
motor[frontRight]=0;
motor[backRight]=0;

``````

Hopefully that was the answer because that was a TON of work to do

Doesn’t a gyro measure angular velocity, not angular position? (I’ve never used one before, but the VEX website describes the gyro as measuring a “rotation of 1000 degrees per second”)

Well… I have no experience of what the gyroscope actually does to get an angle. I just assume it’s a holy thing that the angels has brought down that’s too sciency for me
But I believe that the gyroscope grabs the acceleration of turning made by the robot, then turns it into the degree of rotation. I may be wrong but that’s what I’m using it for for my robot…

Yes and then RobotC or PROS etc. scales and integrates that for you to give angular position

Here’s a link to a site that shows how a Gyro works
https://learn.sparkfun.com/tutorials/gyroscope/how-a-gyro-works

The key to it is this:
When the gyro is rotated, a small resonating mass is shifted as the angular velocity changes.

Look at the tabs of the product page fro some info about the Vex one

I know kind of confusing, but the gyro measures the rotation of the item relative to the earth (well really the rotation that was used in the calibration step) and outputs the degrees it has seen gone by since initialization - meaning the current rotational position in tenths of a degree from where you initialized it.

That is why you have the calibration step of the gyro and it is important not to touch the robot during the calibration. Otherwise its reference spin rate is off and it thinks it is spinning when it is sitting still.

Yes of course. But you can get angular position easily enough

Calculus explanation
Code just integrates velocity to get position. Obviously it isn’t a true integral but actually a riemann sum.

non Calculus explanation
We are able to take the velocity and compute how much was turned in say 5 miliseconds. Then add that to the current angle. If you do that from the moment the cortex was turned on you can get the angular position.

This is my first year doing robotics. I am in 7th grade and honestly don’t understand most of the things you all are doing.

motor[backleft]=(320);
motor[backright]=(320);
motor[frontright]=(320);
motor[frontleft]=(520);

Current code for autonomous
Thanks everybody for helping me

did you mean motor[frontleft]=(320); ?

and i think the maximum motor speed is 127 but idk

also that code will just run the drive train forwards without stopping so if you want to do that for the whole autonomous section of a match then that’s fine but if you want to go forward for a certain amount of seconds then what cataclysm delta II said is correct:

motor[backleft]=(127);
motor[backright]=(127);
motor[frontright]=(127);
motor[frontleft]=(127);
wait1msec(3000); //forward for 3 seconds then stop
motor[backleft]=(0);
motor[backright]=(0);
motor[frontright]=(0);
motor[frontleft]=(0);

These videos are really good i really recommend them.

When I put motorfrontleft: I was trying to get the robot to go straight because it was gradually turning.
I am honestly not sure because I am a noob but i will test this and watch that youtube video when i get home. I will keep you all updated.

A few things to help get you a good baseline

1. Motor values go from -127 to +127 in Robot C. Anything higher is like it is given a value of 127. (or -127 if way lower)

2. The auton routine is going to run while the competition switch is set too auton made. In a match it switches off after 15 seconds. In programming skills it is 1 minute. Your code turns the motors on and never turns them off. So if you get it going straight it will drive into the fence or wall and keep spinning. Do what @sara says and shut them off using the timer. Having motors run while stuck against a wall is not good for them in the long term.

3. To measure the rotations you have to use a sensor. There are two choices the big red ones called quadrature encoders and the integrated motor encoders which you stick on the back of the motor. Look in the robot C example programs to see how each works. This will get you to how many ticks have been seen by the sensor. When you get to a certain value, you want to stop. So instead of the wait on time, you wait on the sensor value being under your desired number.

4. make sure the motors all turn at the same speed. Use the robot C debugger and look at the sensor window. Tip your robot on its side and run your drive motors at full tilt for a few seconds (@sara code) and see what the encoder values look like. Is one consistently slower than the others? If so, check the internal gearing or throw that motor out and replace it. If it is off by a little bit, look at that wheel and see if there is extra friction there preventing the wheel from going the same speed as the others. Take a nice drill bit to the bearing blocks to open up the one it snags on. Make sure the robot build is solid and the shaft is not quite aligned. Then repeat in robot C.

5. Trying to keep it in a straight line and constantly adjusting is much more complicated. Let’s make sure the motors are all good first before we try a gyro or anything like that. To slow down and glide into your target value is called proportional control. You need to slow down based upon how far away you are from the desired position. Then from there you can get fancy with PID. But 7th grade first year is generally not ready for more than that.

Thanks for writing all of this! I do school robotics so there are a limited amount of motors because there are nine teams with an average of 5 people per team. Is there any other thing we could try?

I was just looking at the encoder and decided that we will play around with it! Thanks to everyone who posted

Could someone check over my code to make sure my code including autonomous and driver control sections are working? The part where I have if else if and else statments are not working
CorrectAutonomous - Copy.zip (1.43 KB)