View unanswered posts | View active topics It is currently Wed Jun 20, 2018 8:26 pm






Reply to topic  [ 4 posts ] 
Question about code for sensors. 
Author Message
Rookie

Joined: Wed Oct 01, 2014 9:02 am
Posts: 2
Post Question about code for sensors.
The end goal of my project is to be able to get my robot to escape through an 8 inch hole in an otherwise dark box. I have decided to go with 1 sonar sensor (which is facing forwards), and 1 light sensor (which is facing towards the right. I am trying to initially get the robot to go straight until it is 20 cm from the wall and then make a left turn and follow the wall until A. the sensor picks up light and turns right to escape, or B. the robot runs into the next wall resulting in another left turn in hopes of finding the lights. The issue I am having is that I do not know how to get my set of code as a whole to restart once it reaches the en, and if that is even possible. Also, I cant seem to get the light sensor to kick in when i am using an if statement. I am still relatively new to robotc and this is my fourth day using the software so any help would be amazing.

code below:
Code:
#pragma config(Sensor, S1,     Sonar,          sensorSONAR)
#pragma config(Sensor, S2,     Light,          sensorLightInactive)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
   int distance_in_cm=15;
   while(SensorValue[S1] > distance_in_cm)   
{
      motor[motorA]=30;
      motor[motorB]=30;
}      
{
   if(SensorValue[Light] < 10)
   {
   }
   else
   {
      motor[motorA]=-15;
      motor[motorB]=15;
      wait1Msec(2000);
   }
}
   if(SensorValue[S1]< distance_in_cm)
{
      motor[motorA]=0;
      motor[motorB]=0;
      wait1Msec(200);
      motor[motorA]=15;
      motor[motorB]=-15;
      wait1Msec(2000);
}

}


Wed Oct 01, 2014 9:16 am
Profile
Rookie

Joined: Sun Sep 28, 2014 7:18 am
Posts: 5
Post Re: Question about code for sensors.
Hi,
I didn't understand what do you want ;).
Please, describe it in more details, some scheme would be great.

About your code. I'm not a guru myself, but:

1) use function to reuse the code and develop self-explainig code, say, for turns

Code:
void turnLeft1()
{
  motor[motorA]=-15;
  motor[motorB]=15;
  wait1Msec(1000); // put here the time which is proper for your robot
}

void turnLeft2()
{
  motor[motorA] = 0;
  nMotorEncoder[motorB] = 0;
  motor[motorB] = 30;
  while (nMotorEncoder[motorB] < 240) // put here the angle which is proper for your robot
  {
    wait1Msec(1);
    //or do sth else
  }
}

void turnLeft3()
{
  motor[motorA] = 0;
  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorB] = 240; // put here the angle which is proper for your robot
  motor[motorB] = 30;
}


I personally prefer use angle not time.


2) The while loop in your code better to write as
Code:
  motor[motorA]=30;
  motor[motorB]=30;

  while(SensorValue[S1] > distance_in_cm)   
  {
     wait1Msec(1);
  }   


Wed Oct 01, 2014 10:58 am
Profile
Rookie

Joined: Wed Oct 01, 2014 9:02 am
Posts: 2
Post Re: Question about code for sensors.
specifically i need to write a code that is mutitasking. The sonar sensor needs to know when it is too close to another wall so that the robot can turn, and the light sensor needs to know when it passes an opening in the box so that it can turn and exit. I hope that makes more sense. The whole project is taking place under a dark box with one 8 inch opening and the goal is to escape.


Wed Oct 01, 2014 12:58 pm
Profile
Rookie

Joined: Sun Sep 28, 2014 7:18 am
Posts: 5
Post Re: Question about code for sensors.
As far as I understand, your algorithm (in assumption that we move in a box/maze keeping nearest wall on the right) can be this:
1) move forward
2) if a wall is before the robot -> turn left
3) keep definite distance from the wall
4) if the robot "feels" light on the right -> turn rigth and leave the box/maze

You do not need any multitasking. Just write these steps consequently.
Note that for 3) you have to use regulation, for example, the simplest one - proportional (http://en.wikipedia.org/wiki/Control_system#Proportional_control), sth like below.
In fact, for that task you need use PI-regulator or PID-regulator.

Code:
#pragma config(Sensor, S1, Sonar, sensorSONAR)
#pragma config(Sensor, S2, Light, sensorLightInactive)

const int target_dist = 15; // desirable distance from walls
const float coef = 0.1;     // gain coefficient
const int speed = 30;       // speed of normal movement

int div;

void SetMotors(int a, int b)
{
   motor[motorA] = a;
   motor[motorB] = b;
}

void turnLeft(int t)
{
   SetMotors(speed-div*coef,speed+div*coef);
   wait1Msec(t);
   SetMotors(speed,speed);
}

task main()
{
  SetMotors(speed,speed);

  while (true)
   {
     div = SensorValue[S1] - target_dist;
     if (abs(div) < target_dist) // change movement when distance is from 0 to 2xtarget_dist from a wall
       {
         turnLeft(50);           // put the time you like here
      }
     else if (SensorValue[S2] > 75)
     {
        //turnRigth and leave the box;
        return;
     }
     wait1Msec(5);
   }
}


I have not checked this for real robot, it's just an example.
Also, you need orient the sonar forward-and-right.
----------------------------------------------------
Regards, Sergey


Wed Oct 01, 2014 4:25 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 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.