Author Topic: photovore programming issues - newbie needs help -  (Read 2636 times)

0 Members and 1 Guest are viewing this topic.

Offline raptorwesTopic starter

  • Jr. Member
  • **
  • Posts: 43
  • Helpful? 0
photovore programming issues - newbie needs help -
« on: March 26, 2011, 02:06:09 PM »
I am building a photovore robot and it seems to me that i am missing something in my programming to get the robot to follow light. I posted this question in the vids forum with a short clip of my robot going forward and haven't gotten any responses so i figured i would post my question here. Could someone tell me what i am missing in my code to get my robot follow light. I used project designer and the cut and paste from other projects method to get the code to where it is at now. my next step will be to add a sonar or IR rangefinder to the front as well, but i want to get this piece working acceptably before i move on. Any help is much appreciated  

this is the code i have uploaded :




#include "hardware.h"

// Initialise the hardware
void appInitHardware(void) {
   initHardware();


}
// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
   return 0;
      
}
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {

      int threshold=8;//the larger this number, the more likely your robot will drive straight
  
   // -------- Start Switch/Button-------
   // Switch/Button - see switch.h
  
   // To test if it is pressed then
   if(SWITCH_pressed(&button)){
      // pressed
   }
  
   // To test if it is released then
   if(SWITCH_released(&button)){
      // released
   }
   // -------- End   Switch/Button-------

   // -------- Start Marquee-------
   // Marquee - see 'segled.h'
   // Before using the Marquee you need to redirect rprintf to write to it
   // This can be done using
   Writer old = rprintfInit(marqueeGetWriter(&marquee));
  
   // All rprintf output will then be sent to the marquee but will not
   // display until an end-of-line eg "\n" has been sent. Example:-
   // rprintf("Hello World\n");
  
   // If the endDelay is non-zero then the marquee will scroll
   // forever or until you call: marqueeStop(&marquee);
  
   // If the endDelay is zero then the marquee will stop once
   // the entire line has been shown ('one-shot' mode)
  
   // In 'one-shot' mode then you may want to make sure that
   // a previous line has finished before you display a second line.
   // This can be done as follows:-
   marqueeSetEndDelay(&marquee,0); // Make sure we are in one-shot mode
   if(marqueeIsActive(&marquee)==FALSE){
        if(loopCount==1){
           rprintf("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
        }else{
         rprintf("Loop=%u\n",(unsigned)loopCount); // Put the loop count
        }
   }
  
   // Restore rprintf back to its previous location
   rprintfInit(old);
   // -------- End   Marquee-------

   // -------- Start Analogue Input-------
   // Read the Analogue Input and store results
   uint16_t photoresistor_rightval = a2dConvert10bit(photoresistor_right);
   // Dump out the value
   rprintf("photoresistor_right: %d\n",photoresistor_rightval);
   // -------- End   Analogue Input-------

   // -------- Start Analogue Input-------
   // Read the Analogue Input and store results
   uint16_t Photoresistor_leftval = a2dConvert10bit(Photoresistor_left);
   // Dump out the value
   rprintf("Photoresistor_left: %d\n",Photoresistor_leftval);
   // -------- End   Analogue Input-------

   // -------- Start Actuators -------
   // To control your.motors/servos then see actuators.h in the manual   // To retrieve the required speed of servoleft use:
   // DRIVE_SPEED speed=act_getSpeed(servoleft);
   // To set the required speed of servoleft use:
   // act_setSpeed(servoleft,speed);
   // This example will move the motors back and forth using the loopStart time:
   if(Photoresistor_left > photoresistor_right && (Photoresistor_left - photoresistor_right) > threshold)
         {
         // go left
         act_setSpeed(&servoleft,DRIVE_SPEED_MIN);
         act_setSpeed(&servoright,DRIVE_SPEED_MAX);

      
         }
  
      else if(photoresistor_right > Photoresistor_left && (photoresistor_right - Photoresistor_left) > threshold)
         {
         // go right
         act_setSpeed(&servoleft,DRIVE_SPEED_MAX);
         act_setSpeed(&servoright,DRIVE_SPEED_MIN);

      
         }
  
      else
         {
         // Go forwards
         act_setSpeed(&servoleft,DRIVE_SPEED_MAX);
         act_setSpeed(&servoright,DRIVE_SPEED_MAX);

      
         }
  
      //print out detected difference
      rprintf("Sensor Difference: %d\n", Photoresistor_left - photoresistor_right);
delay_ms(30);//slow control loop down to prevent crazy robot oscillation
      
   return 20000; // wait for 20ms to stop crazy oscillations (long story)
      }

   // -------- End   Actuators -------

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: photovore programming issues - newbie needs help -
« Reply #1 on: March 26, 2011, 07:30:16 PM »
First is measure the voltage output of your photo-resistor circuits. Do they have the range you expect over the variation of light intensity encountered?

Offline raptorwesTopic starter

  • Jr. Member
  • **
  • Posts: 43
  • Helpful? 0
Re: photovore programming issues - newbie needs help -
« Reply #2 on: March 28, 2011, 01:21:48 PM »
I am getting 2.6 - 4.5 volts ( power wire to signal wire) depending on the light conditions. I am not entirety sure if it is what i should expect. i used the excel sheet and based my resistor size on that- actually it said use a 5kohm resistor and i substituted that with a 4.7 k ohm resistor be cause that is what i had- but i believe that should only effect the range of light
 

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: photovore programming issues - newbie needs help -
« Reply #3 on: March 28, 2011, 02:17:07 PM »
Quote
I am getting 2.6 - 4.5 volts ( power wire to signal wire)
Is this from the processor ground to the processor's ADC input? I'll assume so for now.

I think the processor's ADC is 10 bit and referenced to Vdd or 5.0V.
So the ADC values would be:
1023 * 2.6/5 = 532
to
1023 * 4.5/5 = 920

Are these the range of value you get from these lines:
   rprintf("photoresistor_right: %d\n",photoresistor_rightval);
   rprintf("Photoresistor_left: %d\n",Photoresistor_leftval);


Offline raptorwesTopic starter

  • Jr. Member
  • **
  • Posts: 43
  • Helpful? 0
Re: photovore programming issues - newbie needs help -
« Reply #4 on: March 28, 2011, 08:22:04 PM »
i was measuring from power (center pin)to signal ( inside pin)or ADC as it should be called when i posted the first set of voltages.  when i measure from ground to ADC i get .272 v and 2.5 v
using the formula you have in your response that should give me

1023*.272/5= 55.65
1023*2.5/5= 511.5

you asked if that was the value i received from these lines:
 rprintf("photoresistor_right: %d\n",photoresistor_rightval);
 rprintf("Photoresistor_left: %d\n",Photoresistor_leftval);


this looks like a print function but i don't know where it outputs the information.  If it is supposed to be output on the marquee the only thing i see there is: loop - followed by some seemingly random numbers




Offline raptorwesTopic starter

  • Jr. Member
  • **
  • Posts: 43
  • Helpful? 0
Re: photovore programming issues - newbie needs help -
« Reply #5 on: March 29, 2011, 04:25:28 PM »
wel i figured out why i couldn't see an output from rprintf and i added some code to to send the output to uart1 (usb) but now i get a lot of ààààààààààààààààààààààààààààà not exactly what i was looking for  ::)

this is my updated code.

#include "hardware.h"

// Initialise the hardware
void appInitHardware(void) {
   initHardware();

// Set UART1 to 19200 baud
    uartInit(UART1, 19200);
    // Tell rprintf to output to UART1
    rprintfInit(&uart1SendByte);
}
// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
   return 0;
      
}
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {

      int threshold=8;//the larger this number, the more likely your robot will drive straight
   
   // -------- Start Switch/Button-------
   // Switch/Button - see switch.h
   
   // To test if it is pressed then
   if(SWITCH_pressed(&button)){
      // pressed
   }
   
   // To test if it is released then
   if(SWITCH_released(&button)){
      // released
   }
   // -------- End   Switch/Button-------

   // -------- Start Marquee-------
   // Marquee - see 'segled.h'
   // Before using the Marquee you need to redirect rprintf to write to it
   // This can be done using
   Writer old = rprintfInit(marqueeGetWriter(&marquee));
   
   // All rprintf output will then be sent to the marquee but will not
   // display until an end-of-line eg "\n" has been sent. Example:-
   // rprintf("Hello World\n");
   
   // If the endDelay is non-zero then the marquee will scroll
   // forever or until you call: marqueeStop(&marquee);
   
   // If the endDelay is zero then the marquee will stop once
   // the entire line has been shown ('one-shot' mode)
   
   // In 'one-shot' mode then you may want to make sure that
   // a previous line has finished before you display a second line.
   // This can be done as follows:-
   marqueeSetEndDelay(&marquee,0); // Make sure we are in one-shot mode
   if(marqueeIsActive(&marquee)==FALSE){
        if(loopCount==1){
           rprintf("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
       }else{
         rprintf("Loop=%u\n",(unsigned)loopCount); // Put the loop count
        }
   }
   
   // Restore rprintf back to its previous location
   rprintfInit(old);
   // -------- End   Marquee-------

   // -------- Start Analogue Input-------
   // Read the Analogue Input and store results
   uint16_t photoresistor_rightval = a2dConvert10bit(photoresistor_right);
   // Dump out the value
   rprintf("photoresistor_right: %d\n",photoresistor_rightval);
   // -------- End   Analogue Input-------

   // -------- Start Analogue Input-------
   // Read the Analogue Input and store results
   uint16_t Photoresistor_leftval = a2dConvert10bit(Photoresistor_left);
   // Dump out the value
   rprintf("Photoresistor_left: %d\n",Photoresistor_leftval);
   // -------- End   Analogue Input-------

   // -------- Start Actuators -------
   // To control your.motors/servos then see actuators.h in the manual   // To retrieve the required speed of servoleft use:
   // DRIVE_SPEED speed=act_getSpeed(servoleft);
   // To set the required speed of servoleft use:
   // act_setSpeed(servoleft,speed);
   // This example will move the motors back and forth using the loopStart time:
   if(Photoresistor_left > photoresistor_right && (Photoresistor_left - photoresistor_right) > threshold)
         {
         // go left
         act_setSpeed(&servoleft,DRIVE_SPEED_MIN);
         act_setSpeed(&servoright,DRIVE_SPEED_MAX);

      
         }
   
      else if(photoresistor_right > Photoresistor_left && (photoresistor_right - Photoresistor_left) > threshold)
         {
         // go right
         act_setSpeed(&servoleft,DRIVE_SPEED_MAX);
         act_setSpeed(&servoright,DRIVE_SPEED_MIN);

      
         }
   
      else
         {
         // Go forwards
         act_setSpeed(&servoleft,DRIVE_SPEED_MAX);
         act_setSpeed(&servoright,DRIVE_SPEED_MAX);

      
         }
   
      //print out detected difference
      rprintf("Sensor Difference: %d\n", Photoresistor_left - photoresistor_right);
delay_ms(30);//slow control loop down to prevent crazy robot oscillation
      
   return 20000; // wait for 20ms to stop crazy oscillations (long story)
      }

   // -------- End   Actuators -------




Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: photovore programming issues - newbie needs help -
« Reply #6 on: March 29, 2011, 05:13:03 PM »
Quote
now i get a lot of ààààààààààààààààààààààààààààà not exactly what i was looking for
The common cause of this is a wrong Baud rate.
I see that you set it to 19.2k in the code. Is your terminal program also set to 19.2k. Try other Baud rates.
What is the processor's clock and is that clock properly defined?

By the way, what processor are you using? I'm guessing it is an Atmega of some variety.

Offline raptorwesTopic starter

  • Jr. Member
  • **
  • Posts: 43
  • Helpful? 0
Re: photovore programming issues - newbie needs help -
« Reply #7 on: March 29, 2011, 06:07:53 PM »
Good eye waltr, it was a baud rate issue. I am using an axon2.  Putty was set to 115200 and as you pointed out the code was set to 19.2k. I updated the code to reflect 115200 and i get a reading  from my sensors now
 the readings go from about 40 to about 500  ( if i black out the sensor it will go as low as 10, and under a lamp 600 or more )
the right and left sensors also show different readings

However it still only wants to drive straight  :(  .  i cant possibly take over the world with a robot that only goes straight. lol

this is what i see in putty -- ( i was moving the sensors to vary the readings )
photoresistor_right: 296
Photoresistor_left: 450
Sensor Difference: 1
photoresistor_right: 301
Photoresistor_left: 444
Sensor Difference: 1
photoresistor_right: 313
Photoresistor_left: 425
Sensor Difference: 1
photoresistor_right: 325
Photoresistor_left: 418
Sensor Difference: 1
photoresistor_right: 328
Photoresistor_left: 397
Sensor Difference: 1
photoresistor_right: 343
Photoresistor_left: 381
Sensor Difference: 1
photoresistor_right: 338
Photoresistor_left: 367
Sensor Difference: 1

Offline rbtying

  • Supreme Robot
  • *****
  • Posts: 452
  • Helpful? 31
Re: photovore programming issues - newbie needs help -
« Reply #8 on: March 29, 2011, 07:16:38 PM »
I think the fact that your 'Sensor Difference' is always 1 would be a clue to your problem - try placing parentheses around the calculation for the difference. 

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: photovore programming issues - newbie needs help -
« Reply #9 on: March 29, 2011, 07:26:28 PM »
Alright, now you are getting some where. Having values output to a terminal is a handy means of seeing what the code is doing.
rbtying pointed out the probable cause of your bot not turning.

Offline raptorwesTopic starter

  • Jr. Member
  • **
  • Posts: 43
  • Helpful? 0
Re: photovore programming issues - newbie needs help -
« Reply #10 on: March 29, 2011, 08:41:45 PM »
I thought the "sensor difference 1" looked ominous, but i am not sure what line of text to put parentheses around. Programming is certainly not my strong suite as i am on week 2 (or so) of  trying to learn this stuff. It appears to me that the lines of text dealing with the sensors already have parentheses around them. I have reviewed a couple different photovore files and i don't see what i am missing. 

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: photovore programming issues - newbie needs help -
« Reply #11 on: March 30, 2011, 01:02:04 AM »
Quote
...i am not sure what line of text to put parentheses around...
It is the specific calculation and not the line that likely requires parenthesis, like this:
Code: [Select]
rprintf("Sensor Difference: %d\n", (Photoresistor_left - photoresistor_right));
Also try removing the line:
Code: [Select]
delay_ms(30);//slow control loop down to prevent crazy robot oscillationThere is already a 20ms delay provided in the return value on the next line down.

But these are not the real issue. The problem is that you read the sensor values into variables called Photoresistor_leftval and photoresistor_rightval and report them, which is working fine.
You then then do your motor speed and sensor difference calculations with variables called Photoresistor_left and photoresistor_right, which are actually the values of the ADC channels used by your sensors (and probably do differ by 1).

A way to avoid this type of error is to give variables names which describe them more accurately eg adc_Photoresistor_left for an adc channel. This makes them easier to spot.

 


Offline raptorwesTopic starter

  • Jr. Member
  • **
  • Posts: 43
  • Helpful? 0
Re: photovore programming issues - newbie needs help -
« Reply #12 on: March 30, 2011, 08:05:32 AM »
I had a few minutes before work to day to try what you suggested hopslink. I removed the line of code referring to the 30ms delay
 and i changed the code to reflect photoresistor_leftval and photoresistor_rightval ( instead of photoresistor_left / photoresistor_right)

and ( drum roll please)  it follows light.I mean really, it actually worked, then.... the battery went dead ...lol
I want to thank you guys for your patients and helpfulness. I was kind of lost trying to figure this out by my self and your help made it a great learning experience. That being said, i am sure i will have more questions as i upgrade sensors and make changes, but i feel like i am pointing in the right direction and that I have something i can work with. I hope to post a short video of a working product this evening when i get home from work
   

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: photovore programming issues - newbie needs help -
« Reply #13 on: March 30, 2011, 09:27:59 AM »
Great.
Now onward.

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: photovore programming issues - newbie needs help -
« Reply #14 on: March 30, 2011, 11:53:04 AM »
Good stuff! Looking forward to that video.

 


Get Your Ad Here

data_list