Society of Robots - Robot Forum

Software => Software => Topic started by: GalacticNerd on June 13, 2010, 08:21:39 AM

Title: Help on figuring out the programming side
Post by: GalacticNerd on June 13, 2010, 08:21:39 AM
Hello, I'm not new to robotics, and not really new here too, but I haven't done much, or been really active.
I've been programming a "micromouse" buggy in FlowCode (by matrix multimedia), got it to fly through mazes using differential drive etc.
It was a very fun experience, but while it is a good way to develop your algoritm-designing skills, you learn virtually nothing on the electronics, and returning to C has also proven to be a bitch.
Now, I'm going to buy a roboduino and modded servos, and attempt to recreate my FlowCode buggy.
For starters, It'll be a normal photovore robot with 2 or 3 Photodiodes.
I've taken a look at the 50$ robot code for the roboduino posted by airman00 (I think), and I understand the structure of the code, but some things are unclear. I was hoping someone could explain those to me.
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);
}


/* Servo Test Code
i=250;
while(i>0)
{
servo_left(40);
i--;
}

i=250;
while(i>0)
{
servo_left(24);
i--;
}
*/

//rprintf("Initialization Complete\r\n");

//output message to serial (use hyperterminal)
//print("Hello, World! Read My Analog: %u\r\n", sensor_0);

delay_cycles(500);//a small delay to prevent crazy oscillations
}
/*********ADD YOUR CODE ABOVE THIS LINE **********/

return 0;
}

For starters, what do the numbers in following lines between the brackets mean?
Code: [Select]
sensor_left=a2dConvert8bit(5);
sensor_right=a2dConvert8bit(4);
servo_left(44);
servo_right(44);
servo_left(25);
servo_right(25);

I was thinking it was a form of speedcontrol, but that doesn't make any sense if you take a closer look.
This code IS a differential drive algoritm, right? Or is it the one with the 3 different angles?
If it is, could someone give me an example of how to get a value of a photodiode, and how to apply a certain speed to a servo ?
I think I can manage from there on.

Also what does the servo test part do exactly, and why is it there? *confused*

Thanks in advance :)
Title: Re: Help on figuring out the programming side
Post by: SmAsH on June 13, 2010, 08:51:46 AM
Okay, my code reading skills suck but here we go!
The numbers next to servo left and right are speed settings in arduino language.
one 44 and one 25 will make the robot go forward or reverse (depending on servo placements) because one servo is flipped.

The servo test program just turns the servo, its just there because its helpful i suppose.
Just spins the servo back and forth.

I don't know about the
"sensor_left=a2dConvert8bit(5);
sensor_right=a2dConvert8bit(4);" numbers though, sorry.
All i know is the function converts the resistance to an analogue value and compares the two sensors to each other.
Title: Re: Help on figuring out the programming side
Post by: GalacticNerd on June 13, 2010, 08:59:36 AM
Thanks SmAsH, clears it up a bit.

Anyway, after looking at some arduino examples I came up with this:
Code: [Select]
#include <Servo.h>

//Haven't checked pin numbers...

Servo Leftservo, Rightservo;  // create servo objects to control a servo
const int leftpin = 0, rightpin=0;  // analog pin used to connect the photodiode
int left, right;    // variable to read the value from the analog pin

void setup();
void updateServo();

int main()
{
setup();
while(1){updateServo;delay(15);}                        //waits for the servo to get there (is this needed with rotation?)
return 0;
}

void setup()
{
  LeftServo.attach(9);  // attaches the servo on pin 9 to the servo object
  RightServo.attach(10);
}

void updateServo()
{
left = analogRead(leftpin);            // reads the value of the photodiode (value between 0 and 1023)
right = analogRead(rightpin);
left = map(left, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
right = map(right, 0, 1023, 0, 179);
Leftservo.write(left);                  // sets the servo position according to the scaled value
Rightservo.write(right);
}


Excuse me if there are coding errors, my C isn't too good.
Would this work ?
Title: Re: Help on figuring out the programming side
Post by: Razor Concepts on June 13, 2010, 11:49:09 AM
You can't have a main() loop, Arduion code isn't true C.

setup() automatically runs once first, then the loop() one runs continuously.
Title: Re: Help on figuring out the programming side
Post by: GalacticNerd on June 13, 2010, 12:40:02 PM
You can't have a main() loop, Arduion code isn't true C.

setup() automatically runs once first, then the loop() one runs continuously.

Oh, I see..
So would this work?;
Code: [Select]
#include <Servo.h>

//Haven't checked pin numbers...

Servo Leftservo, Rightservo;  // create servo objects to control a servo
const int leftpin = 0, rightpin=0;  // analog pin used to connect the photodiode
int left, right;    // variable to read the value from the analog pin

void setup()
{
  LeftServo.attach(9);  // attaches the servo on pin 9 to the servo object
  RightServo.attach(10);
}

void loop()
{
left = analogRead(leftpin);            // reads the value of the photodiode (value between 0 and 1023)
right = analogRead(rightpin);
left = map(left, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
right = map(right, 0, 1023, 0, 179);
Leftservo.write(left);                  // sets the servo position according to the scaled value
Rightservo.write(right);
        delay(15);
}