Asked — Edited

Help In Programming

We have built a robot car that contains two continuous servos for movement, a camera that is mounted on two standard servos used as heads, and an ultrasonic sensor. The purpose of our project is to respond to a traffic light. We want the robot to recognize the red color by the camera and stop in a certain distance away from the traffic light using the ultrasonic sensor. And, the robot should start moving again when the red color disappears.

The following is the algorithm we have in mind,

The algorithm of the program starts with moving forward while "tracking" is enabled aiming to find Red. If Red is detected, the Distance between the robot and Red is calculated. This Distance is compared with Y, which is a set value in inches corresponding to a distance away from Red where the robot should stop. If the Distance is not less than or equal to Y, the robot keeps moving until this condition is satisfied. If the Distance is less than or equal to Y, the robot stops. When the color changes (becomes not Red anymore), the program starts again with moving forward.

Attached is a flowchart of the algorithm and a picture of the robot.

We would appreciate it if you could give us a script that helps us to accomplish such a job.

User-inserted image

*Note: that the flowchart doesn't have an " end " because we are going to stop the robot manually.

User-inserted image


ARC Pro

Upgrade to ARC Pro

Don't limit your robot's potential – subscribe to ARC Pro and transform it into a dynamic, intelligent machine.

United Kingdom
#1  

I'll give it a go, but bear in mind I have not yet written any scripts which use the camera so that part may be off.

You will need the camera control added and set up correctly. You will also need the Movement Panel added and set up correctly.

I'll do this step by step so you (and others) can see how we come to the final script.

Start off by issuing a forward command in the script;


Forward()

The enable the camera for colour tracking


Forward()
ControlCommand("Camera", CameraColorTracking, red)

Now you need to set up the IF statement to act on detection of red. This is the only part I am unsure of as I haven't done anything with EZ-Script and the camera. Try it, if it doesn't work then we can revisit this part.

As far as I am aware, when the camera tracking is used, and the camera is tracking an object it will change the variable $CameraIsTracking from 0 to 1. So we can use that to run code if it is tracking.


Forward()
ControlCommand("Camera", CameraColorTracking, red)
IF($CameraIsTracking=1)
EndIF

Then we check the distance. A side note, what is it the distance to? The ping sensor may not "ping" off of the light but a wall far away from the light.

First we add the variable $Y for the pre-determined distance. Then we get the distance $distance using the GetPing command and finally another IF statement to check if $distance is less than or equal to $Y, if it is then a Stop command is issued and the robot stops moving, otherwise it continues on.


$Y = 100
Forward()
ControlCommand("Camera", CameraColorTracking, red)
IF($CameraIsTracking=1)
  $distance = GetPing(D0,D1)
  IF($distance <= $Y)
    Stop()
  ENDIF
ENDIF 

This is assuming the Ping sensor is on D0 and D1 and the minimum distance $Y is 100.

And to loop it all around we add in a :label at the start and a Goto at the end.


:start
$Y = 100
Forward()
ControlCommand("Camera", CameraColorTracking, red)
IF($CameraIsTracking=1)
  $distance = GetPing(D0,D1)
  IF ($distance <= $Y)
    Stop()
  ENDIF 
ENDIF 
Goto(start)

Like I said, I haven't done any scripting for the camera before so have used information taken from other posts to pretty much guess at the code. Let me know if it works or not.

It's a very basic and crude script but I've purposely done it that way so you get the chance to make it better. Hopefully the way I've explained it has helped you understand how it works and will aid you in expanding the script and writing new ones.

Also, anyone else who knows a thing or two about ez-scripts please feel free to jump in (and feel free to give it a score from 1 to 10 especially considering this one was done from memory without ARC in sight!.. it's like a test:))

#2  

Rich, Thank you very much. I truly appreciate it. I will test the code and then let you know how it goes. I just have a quick question, don't we need to do something with the two standard servos that are used as heads in which the camera is attached? I mean, I would like the camera itself to move to "keep an eye" on the traffic light, the red color in particular. I'm afraid that while the camera is moving toward the traffic light and getting close to it, it will lose vision on it, if it was stable and not tracking the red traffic light.

Thanks a lot,

United Kingdom
#3  

Set the servo tracking information in the camera control config and enable servo tracking. The camera will then centre on the red when detected.

PRO
Synthiam
#4  

You may determine the distance by the size of the detected color also. In case you are using the stop light distance to determine when to stop?

Also, the OnTrackingStart function in the Camera Config located under the Scriots tab. You can use that to start the Forward() and check for color, etc.

Multi color coming soon:)

#5  

WOW thats will be very cool DJ multi color

traffic ligght it look great your project

#6  

Rich, Thank you very much. The code works great. We just had to do a little modification with it. As of now, we only tested the IF statement that includes the camera condition.

I have a question regarding the ultrasonic sensor. Above in your code, you specified $Y= 100. And then $Y=GetPing(D0,D1). My question is, what is the unit of $Y that the ping sensor is getting? I mean, is it getting 100 inches or 100 cm, for example?

Thank you,

#7  

DJ Sures, Thank you for your response. I like the approach you proposed. Could you kindly tell me how it is done.

Thank you,

United Kingdom
#8  

It depends on the sensor. I believe that the sensor in the EZ-Kit has a range of 255 inches and therefore 1 = 1inch. You will have to do some testing though.

The GetPing is going to be a value of between 0 and 255 (I think), with 0 being closest and 255 being furthest away. If you can measure how far away "furthest" actually is (the point where it hits 255 on the ping sensor's reading) you can calculate the resolution.

For instance, if a reading of 255 is 255" away then a value of 1 would be equal to an inch. If it's 25 inches away then a value of 1 would be 1/10th of an inch. If it's 7'8" away then it's (7x12)+8/255 or 92/255 or a ping of 1 = 0.3607". It's Max Distance/255 = resolution.

In my example script 100 is just a figure put in, it isn't 100 inches or 100cm (if it is it wasn't intended).

Hopefully that makes sense.

United Kingdom
#9  

Also, to use the light itself for distance you would need to determine the size of it for when you want it to stop.

There are two variables used with the camera tracking, $CameraObjectWidth and $CameraObjectHeight

The easiest way to do this is to add the variable watcher in to ARC and look at the two variables values when it is the right distance to stop, then change the IF statement for the GetPing to check the variables against the pre-determined sizes...

Define the two new variables at the start of the script;


$WidthMax = 20
$HeightMax = 20

The replace the IF statement for the Ping sensor to use the camera variables


If($CameraObjectHeight >= $HeightMax or $CameraObjectWidth >= $WidthMax)
  Stop()
EndIf

And the new code should be something like;


# Set up the maximum values for stopping
$WidthMax = 20
$HeightMax = 20

# Start the main code
:start
Forward()
ControlCommand("Camera", CameraColorTracking, red)
IF($CameraIsTracking=1)
  If($CameraObjectHeight >= $HeightMax or $CameraObjectWidth >= $WidthMax)
    Stop()
  ENDIF 
ENDIF 
Goto(start)

Something like that should do it. Again, this has been done with no prior use of those two variables and without testing. Also note that the WidthMax and HeightMax variables will need to be determined as mentioned at the start.

Have a read (when you have half an hour or so) of the topic I made the other day An Introduction To Scripting. It has some great tips in it on how to start with scripting.