Code - Barrier detect

Wink setup, hardware, basic programming. User to user support.
User avatar
sil
Posts: 40
Joined: Thu Oct 01, 2015 1:04 am

Code - Barrier detect

Postby sil » Wed Feb 17, 2016 5:25 am

Had a bit of a play with barrier detection on my Wink. Here's my first attempt, not well written and havent got the random number generator picking both numbers instead of just one. Works well enough as it is though, I've been using a flat baking tray and Wink has been happily bouncing around on it for ages. Its a start , try it out, feel free to modify it as you like.

cheers




Code: Select all

/*

Wink Robot
Barrier Detect test 01
Steve Bolton Feb '16

This is a basic sketch that can be used as a starting point
for various functionality of the Wink robot.
*/

#include "WinkHardware.h"
/* set up hardware */
void setup(){
  hardwareBegin();        //initialize Wink's brain to work with his circuitry
  playStartChirp();       //Play startup chirp and blink eyes
  RestartTimer();
  randomSeed(analogRead(B0));  //for the random number generator, but doesnt work well
  delay(2000);         // 2 sec delay before start
}

/* declare variables */
//int leftOn, leftOff, rightOn, rightOff;
//int leftDiff,rightDiff;
//int speedleft, speedright;

long rndm;


void loop(){

eyesGreen(20);
motors(40,40);
rndm = random(0, 1);

digitalWrite(Headlight, HIGH);



 if (analogRead(AmbientSenseCenter) > 25)
 {
  eyesRed(30);
  motors(0,0);
  delay(50);
  eyesOrange(10);
  motors(-50,-50);
  delay(300);

  // rndm needs work, ends up returning same value, this is
  // intended to pick if wink turns left or right before
  // heading off again
  if (rndm = 0){
   motors(-60,60);
  playStartChirp();
  }
  if (rndm = 1){
   motors(60,-60);
  }

 
  delay(300);
  eyesOff();
 }
 // dump rndm to serial to see if its picking different values
 //Serial.print(rndm);       //print sensorValue through serial port
 //Serial.print("\t");              //print a couple blank spaces
 //Serial.println();                //print a blank line feed
 
} //closing curly of the “loop()” function

wclift
Posts: 32
Joined: Sun Feb 07, 2016 6:10 am

Re: Code - Barrier detect

Postby wclift » Wed Feb 17, 2016 8:47 pm

I'm pretty sure why your random selection isn't working. Here's your code:

Code: Select all

  if (rndm = 0){
   motors(-60,60);
  playStartChirp();
  }
  if (rndm = 1){
   motors(60,-60);
  }


I haven't tested the outcome, but I made sure it compiled. But remember that in C, the "=" is an assignment operator, not a comparison operator. Here's what you meant:

Code: Select all

  if (rndm == 0){
   motors(-60,60);
  playStartChirp();
  }
  if (rndm == 1){
   motors(60,-60);
  }


The "==" will compare the two numbers and return true/false like you wanted.

And for that matter, why not use the "else" operator to save execution time/memory:

Code: Select all

  if (rndm == 0){
   motors(-60,60);
  playStartChirp();
  } else {
   motors(60,-60);
  }



Good Luck,
Wayne

User avatar
sil
Posts: 40
Joined: Thu Oct 01, 2015 1:04 am

Re: Code - Barrier detect

Postby sil » Wed Feb 17, 2016 10:13 pm

thanks keep forgetting that one. Not used to using "==" in my scripting at work its just "=" . However it doesnt explain what i had been seeing and as i mentioned doesn't hinder the Wink behaviour, it still bounces around avoiding headon collisions. I spent a while just printing to the serial monitor a random() output (not a variable) and trying different randomSeed options, and i couldn't get an output that would give me two values (a 0 and 1 seemed easiest to go for. had better success with randomSeed(A0) but sitting on my table random(0, 1) might give me a zero first then nothing but 1s afterwards. If i picked Wink up I started getting zeros so A0 must be be from a light sensor. Hopefully a decent random generator is part of an upcoming lesson. Doesn't concern me too much right now, just wanted to share the code, which does work fine for me, would be interesting to see if it works fine for others.

I had used "else" too and its a good point to raise for others to help. I didnt do anything in particular to optimise this code, its small and uncomplicated. At one point I had a few "if else" conditions to do different turns to pick a new direction to head off in and whittled it down to two explicit checks with a beep on one so I could let it lose and didnt have to watch each turn to see which it chose.

another thing i hadn't considered at the start, which makes sense, is this method is affected by light levels too. I tried playing with the barrier detection as an extension to some light following stuff i was playing with to combine them together. I still might be able to make progress combining them, my light reactions were dependant on the left/right sensors and not the center, while the barrier detect is opposite.

wclift
Posts: 32
Joined: Sun Feb 07, 2016 6:10 am

Re: Code - Barrier detect

Postby wclift » Wed Feb 17, 2016 11:44 pm

I've never used the arduino random() function before, but a quick google shows this:
random()
Description
The random function generates pseudo-random numbers.
Syntax
random(max)
random(min, max)
Parameters
min - lower bound of the random value, inclusive (optional)
max - upper bound of the random value, exclusive
Returns
a random number between min and max-1 (long)


Notice "max-1", so "random(0,1)" will actually only return 0->0, not 0->1 like you wanted. So, if you wanted a 0 or 1, try "random(0,2)".

Your description of what you saw seems contrary to the above spec, however. You indicated that sometimes you DO get a 1. I couldn't find in the documentation if random() returns an INTEGER or a floatingpoint number, but everything I read hints at it being an integer that is returned.

Regardless what seed you use, the random function should give you a string of pseudo random numbers. without the seed, it will gave you the SAME string of "random" numbers each time. This should be useful in your testing. Print out each random number you read and write down the sequence. If it's all zeros or ones, then the problem is with calling of the function. If it gives you a string of different values, then doing the "seed" operation will only change the sequence and should not show you a realtime difference when you pick it up and affect the light sensor. it only samples the sensor pin once on reset.

So, remove one mystery by using "random(0,2)" and just watch the continuous sequence of values to the print terminal. See if you can get wider ranges by using random(0,5).

Also, you declared the rndm variable as a "long", which might return a float instead of an int and that might make a difference as well, since you are then looking specifically for 0 or 1. If it's returning a float decimal between 0 and 1, then it will never return either.

So,...
1) try "int rndm;"
2) try "random(0,2)"

Those are my guesses without being able to test it right now.

User avatar
sil
Posts: 40
Joined: Thu Oct 01, 2015 1:04 am

Re: Code - Barrier detect

Postby sil » Thu Feb 18, 2016 2:40 am

like i said i spent a lot of time trying things my aim was to get 0 & 1 from random but the code i posted is not about using random and i wrote many things following tuorials to get a random sequence of any sort up to 10 and unless i picked up the wink the sequences all settled into returning a single value. sorry but i did try all you suggested and much more and i've moved onto other things right now.

with or without a seed, everything tested returns a single value.sometimes the first returned value is different, its not like its returning a repeating sequence which you get from a random function, hence the need for a seed. a lot in theory should have worked, what i was seeing from the serial monitor clearly shows different. there doesnt appear to be any seeds used in the plumgeek includes either. if you want to write some code and post a thread for random number generation it might be best for others, it just happened to be a dead part of the code i posted, didnt think it'd cause a stir as its irrelevant to what i posted or how the bot runs with it.

wclift
Posts: 32
Joined: Sun Feb 07, 2016 6:10 am

Re: Code - Barrier detect

Postby wclift » Fri Feb 19, 2016 8:39 am

Sorry Sil. I know you started the thread on Barrier Detection, but you mentioned something not working, and my debugging habits took over. At your suggestion, I just posted a simple test usage of random() and randomSeed() capabilities. See it here:
http://forum.plumgeek.com/viewtopic.php?f=13&t=738
By using random(0,2) I reliable got random streams of 0's and 1's. For a randomSeed() argument, I read pin A2, Wink's center ambient light sensor. The test produces a realiable random stream that is unaffected by Wink physical manipulation. But I'll drop this line of discussion now. I just thought it would be useful and appreciated. Apologies.


Here's my modification of your original code. I changed seed to read from A2. Changed to random(0,2). Changed to use of "==" for rndm decision comparisons. Added serial.print to display rndm and the light sensor value. And most interesting, I needed to pick a different light sense trigger value. Under bright light, my sensor was returning >500 for far-away barrier and <500 for wall. When I turned the incandescent desk lamp off, the values dropped to the 50-100 range. I'll have to think more about barrier sensitivity when I get more time. Of course, the preloaded demo sketch contains a complex use of all three ambient sensors, but I wonder if that is overkill :-).

Thanks Sil for the code. It was fun to play with.

Code: Select all

/*

Wink Robot
Barrier Detect test 01
Steve Bolton Feb '16
 - modified for high light by Wayne Clift

This is a basic sketch that can be used as a starting point
for various functionality of the Wink robot.
*/

#include "WinkHardware.h"
/* set up hardware */
void setup(){
  hardwareBegin();        //initialize Wink's brain to work with his circuitry
  playStartChirp();       //Play startup chirp and blink eyes
  RestartTimer();
  randomSeed(analogRead(A2));  //for the random number generator, but doesnt work well
  delay(2000);         // 2 sec delay before start
}

/* declare variables */
//int leftOn, leftOff, rightOn, rightOff;
//int leftDiff,rightDiff;
//int speedleft, speedright;

long rndm, lightSense;


void loop(){

eyesGreen(20);
motors(40,40);
rndm = random(0, 2);

digitalWrite(Headlight, HIGH);


 lightSense = analogRead(AmbientSenseCenter);
 if ( lightSense <500 )
 {
  eyesRed(30);
  motors(0,0);
  delay(50);
  eyesOrange(10);
  motors(-50,-50);
  delay(300);

  // rndm needs work, ends up returning same value, this is
  // intended to pick if wink turns left or right before
  // heading off again
  if (rndm == 0){
   motors(-60,60);
  playStartChirp();
  }
  if (rndm == 1){
   motors(60,-60);
  }

 
  delay(300);
  eyesOff();
 }
 // dump rndm to serial to see if its picking different values
 Serial.print(rndm);       //print sensorValue through serial port
 Serial.print("\t");              //print a couple blank spaces
 Serial.print(lightSense);       //print sensorValue through serial port
 Serial.print("\t");              //print a couple blank spaces
 Serial.println();                //print a blank line feed
 
} //closing curly of the “loop()” function

User avatar
sil
Posts: 40
Joined: Thu Oct 01, 2015 1:04 am

Re: Code - Barrier detect

Postby sil » Tue Feb 23, 2016 2:12 am

wclift
Thanks for the input, I don't think I put enough effort into making my code more presentable, and I can appreciate pouncing on an obvious mistake and glad you did. Also glad you moved the random subject to a dedicated thread, randomness is a good way to get some character and behaviour into something, in robotics it can mean the difference between a robot that can roam until its out of power or one that quickly finds a corner to get trapped in.

Sorry i havent fired up my Wink for a while to test your code yet and fix up that part of the code, been sick and also picked up a 3D printer from Aldi which has held my interest, I'm hoping to design something for Ringo and Wink.

User avatar
sil
Posts: 40
Joined: Thu Oct 01, 2015 1:04 am

Re: Code - Barrier detect

Postby sil » Tue Mar 29, 2016 8:28 am

Finally got around to cleaning up the code a little, no real improvements from original post. Randomly picks a turn direction, only uses one sensor but works well for a very simple example. Extract the attached zip archive into your Arduino installation, upload to your Wink robot (not Ringo), and it starts running when you turn it on. On a tabletop you can put your hand in front for it to detect, I used a flat baking tray with a ridge all around and it just "bounces" around until the battery runs out.

Feel free to explore and play with my code and find fun ways to add features!
Attachments
sil-wink-barrier_detect_01a.zip
Wink - barrier detect 01a code
(8.03 KiB) Downloaded 42 times


Who is online

Users browsing this forum: No registered users and 1 guest