View unanswered posts | View active topics It is currently Thu Jul 31, 2014 12:22 am






Reply to topic  [ 77 posts ]  Go to page 1, 2, 3, 4, 5, 6  Next
Trying to get Sonar Sensor to work on Autonomous 
Author Message
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Trying to get Sonar Sensor to work on Autonomous
I am not sure if i am doing this right. I am trying to get The Sonar sense to work while the Autonomous program is working. What the goal is Let the Program run on autonomous Then when We get near a wall it turns or backs up a lil and stops there.


Code:
#pragma config(Hubs, S1, HTMotor, HTServo, HTMotor, none)
#pragma config(Sensor, S2, sonarSensor, sensorSONAR)
#pragma config(Motor, mtr_S1_C1_1, motorD, tmotorNormal, openLoop, reversed)
#pragma config(Motor, mtr_S1_C1_2, motorE, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C3_1, motorF, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C3_2, motorG, tmotorNormal, openLoop)
#pragma config(Servo, srvo_S1_C2_1, , tServoStandard)
#pragma config(Servo, srvo_S1_C2_6, , tServoContinuousRotation)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Autonomous Mode Code Template
//
// This file contains a template for simplified creation of an autonomous program for an Tetrix robot
// competition.
//
// You need to customize two functions with code unique to your specific robot.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////

#include "JoystickDriver.c" //Include file to "handle" the Bluetooth messages.


/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// initializeRobot
//
// Prior to the start of autonomous mode, you may want to perform some initialization on your robot.
// Things that might be performed during initialization include:
// 1. Move motors and servos to a preset position.
// 2. Some sensor types take a short while to reach stable values during which time it is best that
// robot is not moving. For example, gyro sensor needs a few seconds to obtain the background
// "bias" value.
//
// In many cases, you may not have to add any code to this function and it will remain "empty".
//
/////////////////////////////////////////////////////////////////////////////////////////////////////

void initializeRobot()
{
servo[servo2] = 240; // Place code here to sinitialize servos to starting positions.
// Sensors are automatically configured and setup by ROBOTC. They may need a brief time to stabilize.
servo[servo1] = 200;
return;
}


/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Main Task
//
// The following is the main code for the autonomous robot operation. Customize as appropriate for
// your specific robot.
//
// The types of things you might do during the autonomous phase (for the 2008-9 FTC competition)
// are:
//
// 1. Have the robot follow a line on the game field until it reaches one of the puck storage
// areas.
// 2. Load pucks into the robot from the storage bin.
// 3. Stop the robot and wait for autonomous phase to end.
//
// This simple template does nothing except play a periodic tone every few seconds.
//
// At the end of the autonomous period, the FMS will autonmatically abort (stop) execution of the program.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////

task main()
{
initializeRobot();

waitForStart(); // Wait for the beginning of autonomous phase.

while(true)
{

if(SensorValue(sonarSensor)>15)

{motor[motorD] = 100;
motor[motorE] = 100;
wait1Msec(1000);
}

{motor[motorD] = -60;
motor[motorE] = 60;
wait1Msec(500);
}

{motor[motorD] = 50;
motor[motorE] = 50;
wait1Msec(2000);
}

{motor[motorD] = -80;
motor[motorE] = 80;
wait1Msec(1000);
}

{motor[motorD] = 80;
motor[motorE] = 80;
wait1Msec(5500);
}

{motor[motorD] = 0; //Stop 'rightmotor'.
motor[motorE] = 0; //Stop 'leftmotor'.
}
}
}

If you can help us, Please try :cry: . Thanks :bigthumb: :D


Tue Nov 01, 2011 9:26 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Trying to get Sonar Sensor to work on Autonomous
Since you don't have comment to describe what you are trying to do, I am guessing what you want. First, unless you are trying to do your "routine" over and over again, you should not put that in a forever-while-loop. Secondly, if you are going forward by the sonar sensor, it should be in a while-loop and you should check the reading more often than waiting 1-second in between. Note that except for using the sonar sensor to drive forward, you are basically doing dead reckoning so the autonomous routine won't be consistent from match to match.
Code:
task main()
{
    initializeRobot();

    waitForStart(); // Wait for the beginning of autonomous phase.

    //
    // Move forward until 15 cm from the wall.
    //
    while (SensorValue(sonarSensor) > 15)
    {
        motor[motorD] = 100;
        motor[motorE] = 100;
        EndTimeSlice();
    }

    //
    // Turn for half a second
    //
    motor[motorD] = -60;
    motor[motorE] = 60;
    wait1Msec(500);

    //
    // Go forward for 2 seconds.
    //
    motor[motorD] = 50;
    motor[motorE] = 50;
    wait1Msec(2000);

    //
    // Turn for 1 second.
    //
    motor[motorD] = -80;
    motor[motorE] = 80;
    wait1Msec(1000);

    //
    // Move forward for 5.5 seconds.
    //
    motor[motorD] = 80;
    motor[motorE] = 80;
    wait1Msec(5500);

    //
    // Stop!
    //
    motor[motorD] = 0; //Stop 'rightmotor'.
    motor[motorE] = 0; //Stop 'leftmotor'.
}


Wed Nov 02, 2011 12:43 am
Profile
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Re: Trying to get Sonar Sensor to work on Autonomous
What we are trying to do is have the sonar sensor sense when it gets about 15cm and turn to the left then stop. So we want the orginal autonomous program to run but when it gets near the end of the autonomous program the team wants it to stop and turn to the left. or at least moves in a little more but with out hiting or smashing into the wall.


Wed Nov 02, 2011 9:39 am
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Trying to get Sonar Sensor to work on Autonomous
dtrain34555 wrote:
at least moves in a little more but with out hiting or smashing into the wall.

You can certainly do that with the sonar sensor. So instead of running the motor for a set amount of time, you should run the motor as long as it hasn't gone too close to the wall. So basically, use the same while (sonar reading > x) loop.


Wed Nov 02, 2011 12:10 pm
Profile
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Re: Trying to get Sonar Sensor to work on Autonomous
Thanks :D i'll try it :D


Wed Nov 02, 2011 3:03 pm
Profile WWW
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Re: Trying to get Sonar Sensor to work on Autonomous
From Head Programmer:
We would also like to have the sonar sonsor set to avoid obstacles when they get too close, which would be very beneficial since we would not constantly be running over crates, raquetballs, and other robots. I am the head programmer, and it's kind of sad that I cannot figure that out. :breakcomputer: Then again, my brain is fried beyond belief. Go figure. Any help that you could give us would be greatly appreciated. If we run over something and it gets jammed in the tracks, they could break. "Dtrain" is sitting right next to me, and he would like to add that we need to utilize the light sensor to find when the floor goal on the field. It would see when the white line passes and help to stop the robot if the sonar sensor fails. Again, any help will be appreciated here.

Thank you

Maximilian
:poke: (And Damien)


Wed Nov 02, 2011 3:47 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Trying to get Sonar Sensor to work on Autonomous
Obstacle avoidance is very tricky. Ideally, I would like to use the "wall following" algorithm where if it detects obstacles, it will follow the obstacle's perimeter and "walk around it". However, in order to do this you need 3 sonar sensors, one to the front, one to the left and one to the right and the algorithm could be very complex. I know this because I am in the middle of adding this capability to our library. You could do this with one sonar sensor by either turning the robot left and right to "look" in different directions. Or you can put the sonar sensor on a rotating servo so it acts as a "radar".
Having said that, you can probably do something simple instead. For example, if you see an obstacle in front, you turn left and drive 1 foot and then turn right again assuming the obstacle is no wider than 1 foot. If you are trying to do this in autonomous, I assume your final goal is to go to the parking zone which has an IR beacon. So after you "clear" the obstacle, you can use the IR seeker to find your way back to the parking zone.


Wed Nov 02, 2011 4:39 pm
Profile
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Re: Trying to get Sonar Sensor to work on Autonomous
We also tried the Program and it is not decting the wall


Wed Nov 02, 2011 5:18 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Trying to get Sonar Sensor to work on Autonomous
dtrain34555 wrote:
We also tried the Program and it is not decting the wall

What program did you try? You mean the one I suggested? I assume you mean you keep running into the wall and not stopping?


Wed Nov 02, 2011 5:37 pm
Profile
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Re: Trying to get Sonar Sensor to work on Autonomous
yes it kept going forward into the wall and never backed up or turned. and we check the senors and where the senors and plunged in. we kno the senor was working b ut we are not sure if the senor is getting any feed back.


Wed Nov 02, 2011 5:44 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Trying to get Sonar Sensor to work on Autonomous
Add a line to display the sonar sensor reading like this:
Code:
task main()
{
    initializeRobot();

    waitForStart(); // Wait for the beginning of autonomous phase.

    //
    // Move forward until 15 cm from the wall.
    //
    while (SensorValue(sonarSensor) > 15)
    {
        nxtDisplayTextLine(3, "sonar=%d", SensorValue(sonarSensor));
        motor[motorD] = 100;
        motor[motorE] = 100;
        EndTimeSlice();
    }

This will tell you if the sensor is getting any valid values.


Wed Nov 02, 2011 5:49 pm
Profile
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Re: Trying to get Sonar Sensor to work on Autonomous
Thank you. We will try that. I would like to know if the sonar sensor should be mounted near the top of the robot... I don't think so, but apparently the builder does. He forgets that I has three years of FLL before FTC... 8) And I outrank him in senority. :roll:

Maximilain

P.S. I love smileys:)


Sat Nov 05, 2011 1:18 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Trying to get Sonar Sensor to work on Autonomous
Again, depends on what you want to detect using the sonar sensor. If you want to detect the side walls of the FTC field, then the sonar sensor should not be mounted higher than the height of the wall (which is quite low). We mounted it on our robot base frame (above the wheel). And it should be relatively forward so that no other parts of the robot will interfere with the ultrasonic sound wave.


Sat Nov 05, 2011 1:44 pm
Profile
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Re: Trying to get Sonar Sensor to work on Autonomous
We just want the robot to avoid running into things like the wall. Damien also said that it would be great if it could make the robot stop at the end of autonomous(in the goal). This will be our first time with an operable program, and we're nervous that it won't work... :programmer:

Maximilain

P.S. What is your team name/number? If you are going to the Iowa regional, it would be cool to talk to you face-to-face.


Sat Nov 05, 2011 2:14 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Trying to get Sonar Sensor to work on Autonomous
I am with Team 3543, Titan Robotics. No, we are in Bellevue, Washington. So I don't think we will go that far to attend the Iowa regional :) But with luck (and more hard work), we are hoping to go to St Louis.


Sat Nov 05, 2011 5:04 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 77 posts ]  Go to page 1, 2, 3, 4, 5, 6  Next

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.