Flywheel code

He guys im just wondering. How do I make a function in robotc that if you push a button the robot will automatically adjust its speed based on how close/far it is to the goal(could you use ultrasonic sensors for this), and when you push it again it shuts off the flywheel. I use robotc and have a x drive if that helps.

David @5839A

ok so this gets very complicated, very fast. I don’t think you can use an ultrasonic because the goal is net, and it will probably just eat the ultrasonic.

you have 2 options as I see it, 1, you use pre-determined spots on the field, so you might use a line follower to line up on a line and shoot at a pre-set power

option 2 - this is the one I am trying to get working, you make a “robot gps” how you do this is you put an encoder on your wheels and use a gyro, the encoder on your wheels tells you how far you have moved, and the gyro gives you an angle, a little trig can tell you how far you moved in X and Y coordinates, and if you know where you started you can track the bot as it moves around the field, there is a a whole thread that goes very in depth on this subject

now if you know where your robot is and you know where the goal is, you can figure how far you are from the goal, then you need to use some physics equations to determine how fast you need to shoot

the equation you are looking for is this

Vo = 1/((( Y- (Sin(LaunchAngle )* (x/Cos(launchAngle)+LA))/-16) * (Cos(LaunchAngle)/X))

LA = launch altitude
Y = height of the goal
X = distance to the goal
Vo = required launch velocity of the ball

finaly you write some code to set your fly wheel shooter to a certain velocity, to do this you need an encoder on your flywheel to measure velocity, note, the velocity of the wheel does not = velocity the ball launches at, but it will get you close, the code you need for setting the fly wheel velocity will look like this

float ShooterVelocity

task getWheelVel(){

float wheelCirc = 5*3.14159265

float distPerTic = wheelCirc/360


int Distance = 0

SensorValue[ShooterEncoder] = 0


Distance = SensorValue[ShooterEncoder}

float inPerSec = Distance*5

ShooterVelocity  = inPerSec/12 ///this is the velocity of the outer edge of the wheel in Feet Per Second



void setShooter(int targerVel){

float power = TargetVel * 4

While(Difference >/= 1){

motor[shooter] = power


float difference = targetVel - ShooterVelocity

float P = .25

 power = power + (Difference * P)

motor[shooter] = power

If(power < 0){

power = 0


if(power > 127){

power = 127




note I have not tested this code, it’s something I just threw together, and I know I did not use proper syntax, if you want to use this code you will need to add semicolons and what not

oops, I just saw that you have an X drive, so the encoder and gyro robot GPS won’t work so well, what you can do, is put two free spinning Omni’s in the middle of your bot, attached to encoders, then use a gyro and you can get the same thing

In order to know how far from the goal you are, you’ll need to know your position on the field, which can be difficult to calculate accurately. I would steer you away from using ultrasonic rangefinders, as other objects in the field such as balls or other robots can get in the way and affect your readings. Also, the walls may be too far away at times to actually get a reading.

Most teams I have heard of who determine their position do so by using a gyroscope and encoders on the wheels to continually update their position. This is hard to get accurate, but it can be done:

Once you know your exact position, you should be able to calculate your distance to the net. Then if you determine the speed you need for that distance, you can set your launcher to run at that speed.

Finally, as far as making the button work as you wanted it to, it sounds like you want a toggle function. Here’s a thread about those:

I didn’t go into detail much about each of these steps, because there’s so much to this. I hope I helped by giving you an overview of what you need to do, though.](

This isn’t necessary, as you can just use encoders with the wheels that are already on the robot, and use the trigonometry to figure out your total change in displacement. For example, if one set of wheels (wheels opposite each other in an x-drive), moves 3 inches, and the other set moves 4 inches, then you know you’ve moved 5 inches total because of the Pythagorean theorem. Then you can figure out the angle using some simple trigonometry, and update your position that way. I hope this makes sense.

Ive done this here
maintaining a consistent position is quite difficult though, you have drift and wheel slip when another robot pushes you or you run into a wall
in order to be accurate enough you need a gyro, and a lot harder to implement: accelerometer and line sensors

ya, some people have messed around with using the accelerometers, but they just have soo much background noise