View unanswered posts | View active topics It is currently Sat Sep 20, 2014 8:47 am






Reply to topic  [ 6 posts ] 
Trouble With The Sonar Sensor 
Author Message
Rookie

Joined: Wed Jun 25, 2014 9:44 am
Posts: 5
Post Trouble With The Sonar Sensor
I am currently trying to create a program for an autonomous robot that uses the sonar sensor to navigate. The robot's motors and sonar sensor had been working well when the cortex was plugged into the computer through the usb. Then, after compiling and downloading my program, I powercycled the robot and put it on the ground with nothing in front of it and turned it on. It did nothing but sometimes turn in circles. I changed around the code to see if changing things inside would help and this is currently what I have (It doesn't work):

#pragma config(Sensor, dgtl3, sonar, sensorSONAR_cm)
#pragma config(Motor, port2, rightMotor, tmotorVex393, openLoop)
#pragma config(Motor, port7, leftMotor, tmotorVex393, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//



task main()
{
while(true)
{
wait(.5);
if(SensorValue(bumper)==1)
{
motor(rightMotor)=-65;
motor(leftMotor)=0;
}
wait(.5);


if(SensorValue(limit)==1)
{
motor(rightMotor)=-65;
motor(leftMotor)=0;
}
wait(.5);


while(SensorValue(sonar) > 20) SensorValue(sonar) = -1;
{
motor(rightMotor)=-65;
motor(leftMotor)=0;
}


{
motor[rightMotor] = 127;
motor[leftMotor] =127;
}
}
}
Any help would be greatly appreciated because I am getting very frustrated! Again, I want this robot to be able to be turned on, go forward until the sonar picks something up in front of it, then turn and avoid the object and keep going until another object gets picked up by the sonar. Thanks!


Wed Jun 25, 2014 9:54 am
Profile
Expert
User avatar

Joined: Sat Aug 31, 2013 9:15 am
Posts: 104
Post Re: Trouble With The Sonar Sensor
I am puzzled by the line:
Code:
while(SensorValue(sonar) > 20) SensorValue(sonar) = -1;


What exactly is that supposed to do?

_________________
Burning Lights Programming
FTC Team 6100 Chariots of Fire - Programmer (2012-2013)
FTC Team 7468 Blue Chariots of Fire - Programmer (2013-2014)
Check out our team website at http://cof7468.weebly.com/.


Wed Jun 25, 2014 10:15 am
Profile
Rookie

Joined: Wed Jun 25, 2014 9:44 am
Posts: 5
Post Re: Trouble With The Sonar Sensor
This is the new code I've come up with in the last 20 minutes. It works on one of the cortexes I have and that is it. I just tried compiling and downloading it on a different cortex and that robot spun in circles slowly. Here's the new code:

#pragma config(Sensor, dgtl1, bumper, sensorTouch)
#pragma config(Sensor, dgtl2, limit, sensorTouch)
#pragma config(Sensor, dgtl3, sonar, sensorSONAR_cm)
#pragma config(Motor, port2, rightMotor, tmotorVex269, openLoop)
#pragma config(Motor, port7, leftMotor, tmotorVex269, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//



task main()
{
while(true)
{
wait(.5);
if(SensorValue(bumper)==1)
{
motor(rightMotor)=-65;
motor(leftMotor)=0;
}
wait(.5);


if(SensorValue(limit)==1)
{
motor(rightMotor)=-65;
motor(leftMotor)=0;
}
wait(.5);


if(SensorValue(sonar) <5)
{
motor(rightMotor)=-65;
motor(leftMotor)=0;
}


else
{
motor[rightMotor] =127;
motor[leftMotor] =127;
}
}
}


Wed Jun 25, 2014 10:34 am
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 578
Post Re: Trouble With The Sonar Sensor
There are a couple of things that may be causing these issues in your program:

1) You have your code inside of an infinite while loop, which will make it repeat forever, but remember that any code inside of that loop will be repeated every time you run that loop through. There are several 'wait(.5);' commands that will make the wait for half of a second every time the loop iterates. Remember, too, that the wait commands only tell the code to 'pause' at that point for the specified amount of time; they don't shut the motors off, so whatever the robot was doing before it reached the loop, it will continue to do for another half of a second.

Code:
   
while(true)
 {
   wait(.5);
      
   if(SensorValue(bumper)==1)
     {
        motor(rightMotor)=-65;
        motor(leftMotor)=0;
     }

   wait(.5);


2) Your second while loop looks like it is formatted incorrectly; instead of checking to see if the sonar sensor value is greater than 20 or equal to -1 (no object in range), it only check for to see if the object is greater than 20 cm away. Take a look through the 'Sensing -> Limiting the Arm ->Limiting the Arm Parts 1 & 2' videos in the ROBOTC for VEX Cortex Video Trainer using ROBOTC for more information on using boolean operators OR and AND: http://www.education.rec.ri.cmu.edu/pro ... index.html

Code:
while(SensorValue(sonar) > 20) SensorValue(sonar) = -1;


3) Remember, the robot will do only and exactly what you tell it to do. When you want it to make a movement, you have to tell it no only what to do, but how long you want it to keep doing it. Otherwise, the robot's behavior will be erratic and unpredictable. I suggest focusing on the 'Move forward until near' code first, make sure that is working correctly, then add in the turn commands and any other behaviors that you want the robot to do. Take a look through the 'Sensing -> Forward Until Near' and the 'Fundamentals -> Introduction to Programming -> Planning and Behaviors' sections of the Cortex Video Trainer for more information on these concepts.

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Wed Jun 25, 2014 10:43 am
Profile
Rookie

Joined: Wed Jun 25, 2014 9:44 am
Posts: 5
Post Re: Trouble With The Sonar Sensor
Okay thank you for the help! I will re-format my code and see if that helps.


Wed Jun 25, 2014 10:49 am
Profile
Rookie

Joined: Wed Jun 25, 2014 9:44 am
Posts: 5
Post Re: Trouble With The Sonar Sensor
John,

Could you give me an example of code that should work for me? Like what should I change the infinite while loop to? And what should I change the other loops to so that it works? Thank-you. I'm currently running a camp and I just want this to work for the kids and their robots.


Wed Jun 25, 2014 11:07 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


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

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.