So our competition is Tomorrow and I need an Auton but I have not been able to work on it. How do you code a Sonar Sensor?
Can someone give me a sample code for the SOnar sensor?
Thanks
So our competition is Tomorrow and I need an Auton but I have not been able to work on it. How do you code a Sonar Sensor?
Can someone give me a sample code for the SOnar sensor?
Thanks
Google.
http://help.robotc.net/WebHelpVEX/Content/Resources/topics/VEX_Cortex/ROBOTC/Sensor/sensorValue.htm
while(SensorValue(SonarSensor) < 10)
{
// Robot will move forward at half (63) power
motor[rightMotor] = 63;
motor[leftMotor] = 63;
}
What exactly is your plan for your autonomous function? You might get away with not needing a SONAR, depending on your route.
Are you planning to move forward using encoder counts or time? In addition, judging from the code you posted an hour ago, your best bet is probably just to park. To do that, you need to turn. Are you planning to turn with a gyro, encoders, or something else?
I am planning to put a distance on the SOnar sensor in order for our cap flipper to be able to flip caps. If the Cap is 8 in away from the Sonar sensor then that will activate the Capflipers motor which will flip the cap. I also want to get the Low flags but for now I just want to focus on the Caps because that is our main priority.
DO I put this in the Competition template where it says Autonomous function?
You could always try to just have it move for a certain amount of time - the caps should generally be in basically the same place, so if you just go forward for xx seconds and then flip that might be a simple solution for a quick program. If you haven’t used the sonar sensor before, you might want to wait until after the competition and give yourself more time to learn how to program it and tweak it.
So, playing in VCS, the sensing code is
Sonar.distance(distanceUnits::in)
,
So if you need to drive UNTIL it finds something less than the distance, and then stop, it should be something along the lines of
while(Sonar.distance(distanceUnits::in)>8){
LeftMotor.spin(forward);
RightMotor.spin(forward);
}
LeftMotor.stop();
RightMotor.stop();
So, while the Sonar doesn’t sense something within those 8 inches, it should keep driving forward, until it stops.
If you are a first-time programmer, I would honestly suggest programming in time. It isn’t that difficult to be able to drive to a certain encoder count, but if this is your first time ever coding, I would wait until after the competition to experiment.
Thankyou. Would this be correct?
void autonomous( void ) {
while(Sonar.distance(distanceUnits::in) <8)
{
Arm1.spim(forward);
}
while(Sonar.distance(distanceUnits::in)>8)
{
LeftMotor.spin(forward);
RightMotor.spin(forward);
}
LeftMotor.stop();
RightMotor.stop();