ROBOTC.net forumshttp://robotc.net/forums/ Problem with turning the head of my robot for 90 degreeshttp://robotc.net/forums/viewtopic.php?f=1&t=5626 Page 1 of 1

Author:  endazyar [ Sun Mar 17, 2013 12:15 pm ]
Post subject:  Problem with turning the head of my robot for 90 degrees

Hello everyone, I'm currently building an explorer robot which can move its head(sonarSensor) to the right and left for 90 degrees to check the obstacles. I have implemented 2 functions for that. See them below;

I have a trouble with moving the head to the left and back the position(front). When I call the functions separately they both work well. I mean when i just call checkLeft() function. It turns left and then back to the previous position. Same as for checkRight() function;however, when I call them in order, like checkLeft(); checkRight(); it passes the previous position about 22.5 more degrees.

If you have an idea about what's wrong I'd be happy if you answer me. Also if you have any other ways to have exact turns, pls let me know.

 Code:bool checkLeft(){  nMotorEncoder[motorA] = 0;  // Set the encoder for Motor A to 0  while(nMotorEncoder[motorA] >= -90){  motor[motorA] = -100;  }  motor[motorA] = 0;  sonarValue = SensorValue(sonarSensor);  /********** return right *********/  nMotorEncoder[motorA] = 0;  while(nMotorEncoder[motorA] <= 45){  motor[motorA] = 100;  }  motor[motorA] = 0;  /*********************************/    nMotorEncoder[motorA] = 0;  if( sonarValue > DIST ){    return true;  }  else{    return false;  }}bool checkRight(){  nMotorEncoder[motorA] = 0;  while(nMotorEncoder[motorA] <= 90){  motor[motorA] = 100;  }  motor[motorA] = 0;  sonarValue = SensorValue(sonarSensor);  /*********** return left ***********/  nMotorEncoder[motorA] = 0;  while(nMotorEncoder[motorA] > -45){  motor[motorA] = -100;  }  motor[motorA] = 0;  /**********************************/    nMotorEncoder[motorA] = 0;    if( sonarValue > DIST ){    return true;  }  else{    return false;  }}

 Author: Kevin Selavko [ Sun Mar 17, 2013 11:15 pm ] Post subject: Re: Problem with turning the head of my robot for 90 degrees Why do you have it returning only 45 degrees?

 Author: Kevin Selavko [ Mon Mar 18, 2013 12:32 am ] Post subject: Re: Problem with turning the head of my robot for 90 degrees int sonarValue;int DIST;void resetEncoder();void goToPosition(int pos);bool checkLeft();bool checkRight();task main(){ resetEncoder(); while(true) { checkLeft(); checkRight();}}void resetEncoder() // Put this somewhere at the start, this zero will be your center position{ nMotorEncoder[motorA] = 0; // Set the encoder for Motor A to 0}void goToPosition(int pos){ if (nMotorEncoder[motorA] < pos ) { while(nMotorEncoder[motorA] < pos + 10) { motor[motorA] = 100; } while(nMotorEncoder[motorA] < 0) // Slow down the motor so it does not fly past { motor[motorA] = 25; } } else if (nMotorEncoder[motorA] > pos ) { while(nMotorEncoder[motorA] > pos-10) { motor[motorA] = -100; } while(nMotorEncoder[motorA] > pos) { motor[motorA] = -25; } motor[motorA] = 0; }}bool checkLeft(){ goToPosition(-90); sonarValue = SensorValue(sonarSensor);goToPosition(0); if( sonarValue > DIST ){ return true; } else{ return false; }}bool checkRight(){goToPosition(90); sonarValue = SensorValue(sonarSensor);goToPosition(0); if( sonarValue > DIST ){ return true; } else{ return false; }}

Author:  endazyar [ Tue Mar 19, 2013 11:38 am ]
Post subject:  Re: Problem with turning the head of my robot for 90 degrees

 Kevin Selavko wrote:Why do you have it returning only 45 degrees?

Cause when i make it 90 degrees to return back, then it passes the previous position. When it was 45 it was a little bit ok. And thank you for your replies, I'll check your code now. Grateful.

 Author: endazyar [ Tue Mar 19, 2013 3:39 pm ] Post subject: Re: Problem with turning the head of my robot for 90 degrees When I call the functions just for once, and the code terminates the head doesn't get its previous position. it passes the previous position. Even When I just call checkLeft() it doesn't get the previous position. Couldn't figure out whats wrong with it :S

Author:  Kevin Selavko [ Tue Mar 19, 2013 5:23 pm ]
Post subject:  Re: Problem with turning the head of my robot for 90 degrees

 Code:#pragma config(Sensor, S1,     sonarSensor,    sensorSONAR)//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//int sonarValue;int DIST;void resetEncoder() {   nMotorEncoder[motorA] = 0;}void goTo(int pos){   while(nMotorEncoder[motorA] != pos)   {      motor[motorA]   = (pos - nMotorEncoder[motorA]);   }   motor[motorA]   = 0;}bool checkLeft(){   goTo(-90);   sonarValue = SensorValue(sonarSensor);   goTo(0);   if( sonarValue > DIST )      return true;   else      return false;}bool checkRight(){   goTo(90);   sonarValue = SensorValue(sonarSensor);   goTo(0);   if( sonarValue > DIST )      return true;   else      return false;}task main{   resetEncoder();   while(true)   {      checkRight();      checkLeft();   }}

 Author: endazyar [ Tue Mar 19, 2013 5:35 pm ] Post subject: Re: Problem with turning the head of my robot for 90 degrees Now it works well, thanks a lot!

Author:  endazyar [ Sun Mar 24, 2013 1:38 pm ]
Post subject:  Re: Problem with turning the head of my robot for 90 degrees

I still face the same problem. When I just use that function to turn the head to a side, and then set it to the previous position, it works. However when I use it among some code statements, the precision is not so good, the head starts flying to one side slowly. For example when I use the code below, it starts flying away slowly when motorB and motorC get power to go straight. Do you have any idea what it can be about ? Thanks in advance.

 Code:#pragma config(Sensor, S4,     sonarSensor,         sensorSONAR)bool checkLeft(){   resetEncoder();   goTo(-90);   sonarValue = SensorValue(sonarSensor);   goTo(0);   if( sonarValue > 18 )      return true;   else      return false;}task main(){  checkLeft();  motor[motorB] =100;  motor[motorC] =100;  wait1Msec(3000);  motor[motorB] = 0;  motor[motorC] = 0;}

 Author: endazyar [ Sun Mar 24, 2013 2:21 pm ] Post subject: Re: Problem with turning the head of my robot for 90 degrees I still face the same problem, if anyone could help, I'd appreciate it.

 Page 1 of 1 All times are UTC - 5 hours [ DST ] Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Grouphttp://www.phpbb.com/