Author Topic: Question on code for photovore  (Read 2747 times)

0 Members and 1 Guest are viewing this topic.

Offline krichTopic starter

  • Robot Overlord
  • ****
  • Posts: 165
  • Helpful? 0
Question on code for photovore
« on: October 31, 2007, 04:38:29 PM »
I getting a good grasp of the code provided for the Photovore, but there's minor one part I don't exactly get.

Code: [Select]
while(1) {
//store sensor data
sensor_left=a2dConvert8bit(5);
sensor_right=a2dConvert8bit(4);

//detects more light on left side of robot
if(sensor_left > sensor_right && (sensor_left - sensor_right) > threshold) {
//go left
servo_left(44);
servo_right(44);
}
//detects more light on right side of robot
else if(sensor_right > sensor_left && (sensor_right - sensor_left) > threshold)  {
//go right
servo_left(25);
servo_right(25);
}
//light is about equal on both sides
else {
//go straight
servo_left(25);
servo_right(44);
}
delay_cycles(500);//a small delay to prevent crazy oscillations
}

I don't understand the values sent to the servo_left and servo_right subroutines.  From the code,

Turn Left:  L=44, R=44
Turn Right:  L=25, R=25
Straight: L=25, R=44

Wouldn't you want it to be like this?

Turn Left:  L=25, R=44
Turn Right:  L=44, L=25
Straight:  L=44, R=44

I'm assuming that the numbers are simple values telling the port and pin controlling that servo how long to be in the ON state.  Smaller number, less rotation of the servo.  Or does it equate to the speed of the servo?  I remember reading something about that in the servo mod tutorial.  Either way, it seems a bit odd to me.  I have the sense that I'm missing some key information that will make it all make sense.

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: Question on code for photovore
« Reply #1 on: November 01, 2007, 01:26:24 PM »
Yea its easy to misunderstand . . . the reason is because your servos are physically reversed on your robot. The servo on the left side of your robot is pointing the opposite way as the servo on the right side of your robot.

Swap the code your way and you will see what happens :P

Offline krichTopic starter

  • Robot Overlord
  • ****
  • Posts: 165
  • Helpful? 0
Re: Question on code for photovore
« Reply #2 on: November 01, 2007, 04:51:45 PM »
Oh man.  You're right!! (of course)  I totally missed that, rather obvious, observation.

I'd swap it around on my own robot, but I'm still working on the soldering with my daughter and haven't completely built the chassis yet.  I could probably whip it all out in an hour or two, but I'm letting her do a lot of it.  I work on understanding the other parts in the evenings when she's asleep to make sure things go smoothly once we do get to that point.

By the way, speaking of building chassis's.  The HS-311 don't come with the same mounts as they used to.  Folks will have to come up with a different way to mount the servos.  The blocks that you screw the servo and chassis to are replaced by some little rubber grommets with metal sleeves.  No way to side mount them with the hardware provided.  Real easy to come up with something similar, though.

Offline paulobrasko

  • Beginner
  • *
  • Posts: 6
  • Helpful? 1
Re: Question on code for photovore
« Reply #3 on: December 30, 2007, 12:47:19 AM »
Even though Krich say that "rather obvious, observation", unfortunately that the ADMIN explanation is not that obvious for me. I believe on what you said about changing for the other proposed numbers and see that it will not work, but I do not understand why.

I got more confused after thinking more and more about the answer that the ADMIN gave.
I am a really beginner so bare with me on the following questions.
a) looking at the following code
void servo_left(signed long int speed)
{
   PORT_ON(PORTD, 0);
   delay_cycles(speed);
   PORT_OFF(PORTD, 0);//keep off
   delay_cycles(200);
}

void servo_right(signed long int speed)
{
   PORT_ON(PORTD, 1);
   delay_cycles(speed);      
   PORT_OFF(PORTD, 1);//keep off
   delay_cycles(200);
}

it seems that the motors always go forward, i.e. it does not matter the value of the input argument SPEED. The speed input just make the motor ON for some milliseconds, just that. If the motors are in reverse order, then from item (a) this robot should only spin around and go nowhere...

So, the question is: what is wrong in my code analysis?

b) I have never played with servos, so, the second question that I have is: can I change the velocity of this motor? Can I reverse it?  Can I change the PIC output value so that the motor can go faster? (if possible, how?).

More questions will come for sure, but these are my first two...

Thanks for your attention.

Paulo Brasko




Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: Question on code for photovore
« Reply #4 on: December 30, 2007, 09:18:22 AM »
Paulobrasko, it will become obvious when you program your robot.

When you see this code:
servo_right(number)

just replace 'number' with a number to represent time delay. A servo goes one direction if the delay is less than ~1.5ms, it doesn't move at about ~1.5ms, and it rotates the other direction at a time delay greater than ~1.5ms.

 


Get Your Ad Here