View unanswered posts | View active topics It is currently Tue Sep 23, 2014 10:27 pm






Reply to topic  [ 21 posts ]  Go to page 1, 2  Next
how detect one thing....then another 
Author Message
Rookie

Joined: Fri Oct 15, 2010 2:20 pm
Posts: 17
Location: Rochdale, UK
Post how detect one thing....then another
Hey guys,

This is my first ever post here, so unsure whether im in the right place or not :?:

I am writing a piece of code, for the distance sensor on the NXT, to sense one object, move on, and then sense another object. The whole project is to make a car that can parallel park itself, but this is the first thing I want it to do, sense the space it will be parking in.

Any sort of help would be great. I have only started the project so not really got any code yet.

Many thanks!


Fri Oct 15, 2010 2:31 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: how detect one thing....then another
You could see if you can find the code for this: http://www.youtube.com/watch?v=ebfxYAUBw-0

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Oct 15, 2010 5:59 pm
Profile WWW
Rookie

Joined: Fri Oct 15, 2010 2:20 pm
Posts: 17
Location: Rochdale, UK
Post Re: how detect one thing....then another
Hey, thanks for the reply.

I already have the code for this one, but it is in the NXT software, which doesnt really explain anything! :?


Sat Oct 16, 2010 5:04 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: how detect one thing....then another
You need to look into making what is known as a Finite State Machine (FSM). There is some high level stuff here: [LINK].

In your case you would have a couple of states:
Attachment:
Self Parking Car State Engine.png
Self Parking Car State Engine.png [ 36.69 KiB | Viewed 6907 times ]


You can only move from one state to another if a specific condition has been met, otherwise you loop inside that state until you *can* move to the next state.

Does that make things a bit clearer?

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Oct 16, 2010 5:49 am
Profile WWW
Rookie

Joined: Fri Oct 15, 2010 2:20 pm
Posts: 17
Location: Rochdale, UK
Post Re: how detect one thing....then another
That is really helpful, will get stuck in!

Many thanks.


Sat Oct 16, 2010 6:04 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: how detect one thing....then another
No worries! FSMs are the backbone of most robots. It really helps doing this kind of thing on paper before you start programming.
  • What do you want your robot to do? (this question is more important than you think)
  • Try to chop up the problem into smaller chunks (states), not too small or you'll get stuck on the details.
  • When can you consider a state "finished" so you can move to the next state?
  • When you have this high-level state machine picture, you can start thinking about the internals of the individual states.
This is what programming and information analysis is all about; don't try to analyse the whole thing at once. Any problem (and not just a programming one) is more manageable when broken up :)

Regards,
Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Oct 16, 2010 6:18 am
Profile WWW
Rookie

Joined: Fri Oct 15, 2010 2:20 pm
Posts: 17
Location: Rochdale, UK
Post Re: how detect one thing....then another
Fantastic advise. You have been more than helpful :) I definitely know where to come if I need anymore help! :D

Mohsin.


Sat Oct 16, 2010 8:00 am
Profile
Rookie

Joined: Fri Oct 15, 2010 2:20 pm
Posts: 17
Location: Rochdale, UK
Post Re: how detect one thing....then another
Right, the FSM has been really helpful. I have done the "park car" state. I am struggling to do the rest. I have written some pseudo code to try and make it clearer to me, which is helpful, but still, I am having difficulty in how to structure the actual program to get the car to actually check the start of the parking spot, check end of parking spot and see if the spot is big enough. This is the sort of thing I have written:

Start off with US sensor next to first imaginary car.

Wait for US sensor to detect distance further than 10cm (free space in between cars).

If US sensor detects distance further than 10cm, reset rotation sensor on motor to start counting free space.

If rotation sensor has counted at least 960 degrees (suitable distance for parking) and then US sensor has detected distance nearer than 10cm, get car ready for parking, else loop again.

If US sensor detects distance nearer than 10cm before rotation sensor has counted at least 960 degrees, start loop again.


Any help to help me sort out some structure for this program will be fantastic!

Thanks.


Mon Nov 01, 2010 3:31 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: how detect one thing....then another
Could you attach the code that you have so I can take a look at it?

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Nov 02, 2010 2:41 am
Profile WWW
Rookie

Joined: Fri Oct 15, 2010 2:20 pm
Posts: 17
Location: Rochdale, UK
Post Re: how detect one thing....then another
Of course, the code I have written so far like I said is just simply for the parallel parking manoeuvre, so relatively simple program.

Code:
#pragma config(Sensor, S2,     back,                sensorSONAR)
#pragma config(Sensor, S3,     front,               sensorSONAR)
#pragma config(Sensor, S4,     side,                sensorSONAR)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=75;                     //turn wheels right
  wait1Msec(1000);

  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorB]=540;
  motor(motorB)=35;
  wait1Msec(1000);                     //move car back

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=-75;                    //turn wheels left
  wait1Msec(1000);

  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorB]=270;
  motor(motorB)=35;                     //move car back
  wait1Msec(1000);

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=-75;                    //turn wheels left
  wait1Msec(1000);

  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorB]=270;
  motor(motorB)=35;                     //move car back
  wait1Msec(1000);

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=75;                     //turn wheels right
  wait1Msec(1000);

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=75;                     //turn wheels right
  wait1Msec(1000);

  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorB]=120;
  motor(motorB)=-35;                    //move car forward
  wait1Msec(1000);

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=-75;                    //turn wheels left
  wait1Msec(1000);

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=-75;                    //turn wheels left
  wait1Msec(1000);

  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorB]=120;
  motor(motorB)=35;                    //move car back
  wait1Msec(1000);

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=75;                     //turn wheels right
  wait1Msec(1000);

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=75;                     //turn wheels right
  wait1Msec(1000);

  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorB]=120;
  motor(motorB)=-35;                    //move car forward
  wait1Msec(1000);

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=-75;                    //turn wheels left
  wait1Msec(1000);

}


Also, The above code, I would like to change it so that instead of moving the car a set distance back and forth, I would like the car to move into the space using the sensors.
Thanks for the help :)


Tue Nov 02, 2010 4:48 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: how detect one thing....then another
Can you mark in your code where you think you would need to look where you are with the sensors?

Just edit the original post and add "check here" or something.

Sorry for not giving you the answer, but you will really learn a lot more this way. I'm happy to give you hints and talk you through this :)

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Nov 02, 2010 5:49 am
Profile WWW
Rookie

Joined: Fri Oct 15, 2010 2:20 pm
Posts: 17
Location: Rochdale, UK
Post Re: how detect one thing....then another
Hey that's completely fine, this is the way I will learn :)

But that's the thing, I havnt actually written anything to do with the sensors yet. The above code is only for when it needs to carry out the parallel park from the end point it needs to be at. Its just gettin it to that end point! I'm having difficulty even starting writing that part of the code. Sorry I know I sound like a total amateur :$ lol.


Tue Nov 02, 2010 10:36 am
Profile
Rookie

Joined: Fri Oct 15, 2010 2:20 pm
Posts: 17
Location: Rochdale, UK
Post Re: how detect one thing....then another
Oh I know that it will need to be before the whole of the code, after the task main() bit. I have some sort of idea, that it should first detect when there is some free space, meaning the start of the parking spot. From here I think it should count the distance till the next time it senses something that is not free space-meaning the next car. And if this distance is suitable..carry out the above code to park??


Tue Nov 02, 2010 10:41 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: how detect one thing....then another
No, what I meant was you add some comments like "// Read distance to car behind " to your code. No need for actual code just yet :)

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Nov 02, 2010 12:34 pm
Profile WWW
Rookie

Joined: Fri Oct 15, 2010 2:20 pm
Posts: 17
Location: Rochdale, UK
Post Re: how detect one thing....then another
aaahh im sorry, misunderstood. ok:

Code:
#pragma config(Sensor, S2,     back,                sensorSONAR)
#pragma config(Sensor, S3,     front,               sensorSONAR)
#pragma config(Sensor, S4,     side,                sensorSONAR)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{

//drive alongside first car.

//read distance from beginning of free space.

//wait for sensor to detect second car and see if space is big enough for car park manoeuvre.

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=75;                     //turn wheels right
  wait1Msec(1000);

  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorB]=540;
  motor(motorB)=35;
  wait1Msec(1000);                     //move car back

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=-75;                    //turn wheels left
  wait1Msec(1000);

  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorB]=270;
  motor(motorB)=35;                     //move car back
  wait1Msec(1000);

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=-75;                    //turn wheels left
  wait1Msec(1000);

  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorB]=270;
  motor(motorB)=35;                     //move car back
  wait1Msec(1000);

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=75;                     //turn wheels right
  wait1Msec(1000);

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=75;                     //turn wheels right
  wait1Msec(1000);

  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorB]=120;
  motor(motorB)=-35;                    //move car forward
  wait1Msec(1000);

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=-75;                    //turn wheels left
  wait1Msec(1000);

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=-75;                    //turn wheels left
  wait1Msec(1000);

  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorB]=120;
  motor(motorB)=35;                    //move car back
  wait1Msec(1000);

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=75;                     //turn wheels right
  wait1Msec(1000);

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=75;                     //turn wheels right
  wait1Msec(1000);

  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorB]=120;
  motor(motorB)=-35;                    //move car forward
  wait1Msec(1000);

  nMotorEncoder[motorC] = 0;
  nMotorEncoderTarget[motorC]=180;
  motor(motorC)=-75;                    //turn wheels left
  wait1Msec(1000);

}


is this what you mean by adding comments??


Tue Nov 02, 2010 4:00 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 21 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users and 1 guest


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.