Newb having trouble with variables

Discussion specific to projects ideas and support.
Post Reply
simonauv
Posts: 13
Joined: 29 Aug 2012, 18:43
Location: Australia

Newb having trouble with variables

Post by simonauv »

Image

This is a program I made to try and show some kids at the school I teach at how to use variables to control and compare functions and I can't work out why it's not working. I wanted it to follow a black line using a colour sensor as a light sensor, which it does. There are three colour sensors mounted along the front; one colour sensor made to work as a light sensor flanked by two colour sensors on either side. The colour sensors on each side are to detect green in the form of navigation cues (4ox40mm green square in the vertices of the black lines) and a large green tile known as the chemical spill in the Robocup tournaments. I'm trying ot get it to detect green on either the left or right side and exucte a motor function when it does. I tried to get each colour sensor to assign a variable to the colour detected (between 1-6 with green being 3) and then ask the true/false question whether the variable was equal to 3 and if it was write a new variable (cs3 and cs4) to be sued at close the loop at the end if cs4=cs3. The robot just makes one turn then stops and I can't work out why as it seems to make sense to me. If anyone has any advice as to how to make this work I'd be very grateful to hear them.

Regards,

Simon
hassenplug
Posts: 346
Joined: 27 Sep 2010, 03:05
Contact:

Re: Newb having trouble with variables

Post by hassenplug »

I can't quite make out all the values in the picture, however the one thing I would suggest is clearing your variable values at the start of the program. It looks like it could get to the end without setting c3 or c4, so the value is not defined. (and it could exit)

Before the loop, write values into c3 & c4

Steve
---> Link to lots of MINDSTORMS stuff under my picture --->
simonauv
Posts: 13
Joined: 29 Aug 2012, 18:43
Location: Australia

Re: Newb having trouble with variables

Post by simonauv »

Sorry about the pic... photobucket resized it down.

Is this (slightly different) better?

http://i33.servimg.com/u/f33/13/33/54/62/rescue10.jpg
h-g-t
Posts: 552
Joined: 07 Jan 2011, 08:59
Location: Albania

Re: Newb having trouble with variables

Post by h-g-t »

If you are using Mindstorms 2.0 then the values will be 'real', ie with a decimal point, whereas 1.0 only used integers.

In that case it is possible that one value may be getting stored as (say) 1.001 and another as 0.999 so that that they will never be equal.
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.
simonauv
Posts: 13
Joined: 29 Aug 2012, 18:43
Location: Australia

Re: Newb having trouble with variables

Post by simonauv »

Thankyou both. This has helped my thinking greatly. Can I use the range data hub on a colour sensor to send range data to a compare block to compare the range obtained from two sensors at the same time?
hassenplug
Posts: 346
Joined: 27 Sep 2010, 03:05
Contact:

Re: Newb having trouble with variables

Post by hassenplug »

simonauv,

I see a few things that could be causing problems. This may be a good time for a quick lesson on wires & sensor blocks...

First, the sensor blocks all have conditions built-in to them. So you can use them to do a lot of your compare functions.

Next, wires can sort of act like variables, holding values until you need them.

Attached is a program that should function the same as yours (except that I don't have the color sensor blocks loaded on this computer, so I used light sensors instead). The True/False returned by the sensor block is transported in the wire, and in the end, compared.

Problem 1:If you're setting both C3 & C4 to zero, after it goes through the loop once, both values are equal, so the loop will exit. When that happens, the motors will run for a bit, then shut off.

Problem 2:With three switch blocks, you're telling the motors to do three different things. I would suggest making a simple truth table to determine what the motors should do in each of the eight possible cases, and possiblly use nested if statements, or a case statement

Sensor 1, Sensor 2, Sensor 3, Motor B, Motor C
Green, Green, xx, stop, stop
Green, Not-Green,xx , Turn Left?
Not Green, Green, xx, Turn Right?
Not Green, Not Green, Light, Turn Left?
Not Green, Not Green, Dark, Turn Right?

Be careful not to turn by driving B back, and C forward. The robot may never drive forward, if you do that. (but, you may want that when the sensors see green)

Steve
Attachments
Test1.rbt
(1.1 MiB) Downloaded 377 times
---> Link to lots of MINDSTORMS stuff under my picture --->
simonauv
Posts: 13
Joined: 29 Aug 2012, 18:43
Location: Australia

Re: Newb having trouble with variables

Post by simonauv »

Thankyou, Steve. Sorry for the tardy reply. The boys were competing in the Robocup Challenge over the weekend and I've been busy helping out with that. After posting the above program I redid it and came up with something that looks similar to your program (see attachment). I was still focused on defining and writing variables at that stage so I thought in the initial switch (line following routine) I would get sensor 1 to detect line/dark and make the adjustment needed and then look for green straight away in case it was then over green after the adjustment and then assign it as either cs1range or cs2range variables. I see in your program it does the same thing and is much simpler without having to reset the variables at the beginning of the loop each time. Thankyou.

I'm not sure what you mean by a case statement and I need to look at truth tables more I think. Looking at your example I can see only 5 different possibilities instead of 8. I'm missing something but am not sure what.

The next thing I'd like to find out about is how to write parallel programs.
Attachments
greensensorvars.rbt
(1.82 MiB) Downloaded 375 times
simonauv
Posts: 13
Joined: 29 Aug 2012, 18:43
Location: Australia

Re: Newb having trouble with variables

Post by simonauv »

I've worked out how to do parallel sequences.
hassenplug
Posts: 346
Joined: 27 Sep 2010, 03:05
Contact:

Re: Newb having trouble with variables

Post by hassenplug »

simonauv wrote:I'm not sure what you mean by a case statement
In a switch, you can pass in true/false OR an Int. If you pass in an Int, that value could be any Int, like 1-8 (or 0-7) and you'll see you can add numbers to the switch corresponding to each number.

simonauv wrote:and I need to look at truth tables more I think. Looking at your example I can see only 5 different possibilities instead of 8. I'm missing something but am not sure what.
My shorthand. Sorry.

In the above truth table, I put XX for "Don't Care". Meaning you don't care what the value is, but it could be either light or dark.
So:
Green, Green, XX, Stop, Stop

is the same as
Green, Green, Light, Stop, Stop
Green, Green, Dark, Stop, Stop

The three sets of XX represent the three missing truth table lines.

Steve
---> Link to lots of MINDSTORMS stuff under my picture --->
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests