go away spammer

Author Topic: Arduino first robot programming questions  (Read 12182 times)

0 Members and 1 Guest are viewing this topic.

Offline robonoobTopic starter

  • Robot Overlord
  • ****
  • Posts: 149
  • Helpful? 0
Arduino first robot programming questions
« on: March 21, 2008, 12:59:05 AM »
Hey everybody.
so i made this new thread to ask my software questions here.

How can i make the servos turn clockwise for 1 second, then counterclockwise for 1 second and then again clockwise 1 second and so on so on?? and i don't get why this kind of code doesn't work:
Code: [Select]
servo1.write(0);
delay(1000);
servo1.write(180);
delay(1000);
Servo::refresh();
and take into consideration that I'm using arduino servo library(quite a useful thing you know :D)

secondly: how would you recommend me to make the scanner scan? like i thought i would make it scan from 5 different directions: far left, left, front, right, far right. but once again, i cant make a code for this. but still tell me what u think of this?
then i also thought about something like this: scan from front, then if any obstacle on the way, then scan from the sides for a way out.

okey I'd really appreciate if you come out with your ideas that i could use.
of course i can try all of those methods and then tell you which one was the best.

but once again, plz can u find an answer to my first question, cuz then i can programme the scanning too. otherwise this robot is not going to work :D

edit: hey admin(or anybody else) can u give me the sourcecode of the left edge following robot(Stampy)?
cuz i'm going to try this too :P

thank you
« Last Edit: March 21, 2008, 01:36:00 AM by robonoob »

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: Arduino first robot programming questions
« Reply #1 on: March 21, 2008, 07:53:00 AM »
when you give a servo a pulse to center it , it will not center itself right away with only one pulse
you need a certain amount of pulses of centering for the servo to center

like say the pulse to center a servo is 150
so PULSOUT Servo,150 would give teh servo a pulse of 150 once

However you need a lot of those same pulses of 150 to make the servo centered from any point

Its a bit confusing

think about it
a pulse of 150 will move the servo a certain amount towards center , now if you want it to go from the leftmost to center you need more pulses because its far.
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline robonoobTopic starter

  • Robot Overlord
  • ****
  • Posts: 149
  • Helpful? 0
Re: Arduino first robot programming questions
« Reply #2 on: March 21, 2008, 10:17:24 AM »
okey i made somthing like this:
Code: [Select]
  for (int IRpos=0; IRpos <= 20; IRpos++){
      scanner.write(IRpos);
      delay(10);
      Servo::refresh();
}
and it works... now another question: how can i make the servos stop??? using
Code: [Select]
servo.detach() command?
cuz i tryed and it didn't work...and
Code: [Select]
break; doens't work either.
what i want to do, is: the robot moves forward, scanning at the front at the same time, then if an object is afront of the robot, the robot stops(this is what i cant do, the stoping part. how can i stop the robot to do the following?) and scans from the left. then if left is free, it turns to left(oh and how can i do that too? like i want it to turn left for 0,6 seconds and then start the loop all over again) and starts moving again.


plz answer me fast, cuz i cant wait to get the robot done :P

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: Arduino first robot programming questions
« Reply #3 on: March 21, 2008, 10:24:40 AM »
you want the servo to stop in the place that it is in?


all you have to do is set that pin LOW or give it a value of digital 0





plz answer me fast, cuz i cant wait to get the robot done :P

Cant wait to see the bot when its done!

Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline robonoobTopic starter

  • Robot Overlord
  • ****
  • Posts: 149
  • Helpful? 0
Re: Arduino first robot programming questions
« Reply #4 on: March 21, 2008, 11:03:36 AM »
OK i still cant do this turn for 1 second with my modified servos...
i want them to turn for 0,6 second(600 milliseconds if anybody didint understand :D)

my other problem is that when it makes a turn, it starts moving forward right after and making it still bump into stuff. i want it to make the turn, then scan the area again and then movie forward if no obstacle on way.

i post the code here so u could give me some hints on what I'm doing wrong(this is one hell of a code i made, i mean it's quite difficult. i mean for me at least :D)

Code: [Select]
#include <Servo.h>

Servo left;
Servo right;
Servo scanner;

int object = 0;
int val = 0;
int IR = 5;
int IRpos = 0;
int scan = 0;
int scanlist = 0;
int summa = 0;
int tehe1 = 0;
int tehe2 = 0;
int summa2 = 0;
int movemode = 0;
int object2 = 0;
int summa3 = 0;
int tehe3 = 0;


void setup() {
  right.attach(6);
  right.setMinimumPulse(500);
  right.setMaximumPulse(2500);
  right.write(90);
  left.attach(7);
  left.setMinimumPulse(500);
  left.setMaximumPulse(2500);
  left.write(90);
  scanner.attach(5);
  scanner.setMinimumPulse(500);
  scanner.setMaximumPulse(2500);
  scanner.write(90);
  Serial.begin(9600);
  Serial.println("valmis töötama");
  delay(1000);
}

void loop() {
  scan = 1;
  if(summa < 525) {
    right.write(180);
    left.write(0);
    Servo::refresh();
  }
  if(summa > 525){
    for (int IRpos=102; IRpos >= 0; IRpos--){
      scanner.write(IRpos);
      delay(10);
      Servo::refresh();
      val = analogRead(5);
    }
    for (int IRpos=0; IRpos <= 20; IRpos++){
      scanner.write(IRpos);
      delay(10);
      Servo::refresh();
      val = analogRead(5);
      tehe2 = tehe2 + val;
    }
    summa2 = tehe2 / 21;
    tehe2 = 0;
    left.write(90);
    right.write(90);

    if (summa2 > 525) {
      for (int IRpos=20; IRpos <= 180; IRpos++){
        scanner.write(IRpos);
        delay(10);
        Servo::refresh();
        val = analogRead(5);
      }
      for (int IRpos=180; IRpos >= 160; IRpos--){
        scanner.write(IRpos);
        delay(10);
        Servo::refresh();
        val = analogRead(5);
        tehe3 = tehe3 + val;
      }
      summa3 = tehe3 / 21;
      tehe3 = 0;
      left.write(90);
      right.write(90);
    }
    if(summa3 < 525) {
      right.write(180);
      left.write(180);
      Servo::refresh();
      }
      if(summa3 > 525) {
      left.write(0);
      right.write(0);
        }
    delay(1000);
  }

  if(scan = 1){
    for (int IRpos=82; IRpos <= 102; IRpos++){
      scanner.write(IRpos);
      delay(10);
      Servo::refresh();
      val = analogRead(5);
      tehe1 = tehe1 + val;
    }
    summa = tehe1 / 21;
    tehe1 = 0;
    left.write(90);
    right.write(90);
  }





}

i bet u don't get wtf have i done, but it does do the things i want it to do, except the things i mentioned above
« Last Edit: March 21, 2008, 11:06:25 AM by robonoob »

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: Arduino first robot programming questions
« Reply #5 on: March 21, 2008, 11:15:14 AM »
one second

remind me of your  robot's  setup again

Also, make sure your program is not timing based but rather cycles based ( this will give less headaches)
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline robonoobTopic starter

  • Robot Overlord
  • ****
  • Posts: 149
  • Helpful? 0
Re: Arduino first robot programming questions
« Reply #6 on: March 21, 2008, 11:29:21 AM »
didnt understand a bit what u said :D what cycles? what setup? u mean this picture?
and how can i make it cycles based?

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: Arduino first robot programming questions
« Reply #7 on: March 21, 2008, 03:28:18 PM »
experiment to see how many times you have to send a pulse for the desired behavior to occur

timing based is if you do it go forward for 2 seconds , cycles is do it 50 times

forget i mentioned it , its not really applied here ......

i have to go now , so I dont have a chance to look at your code

umm what you want in your program is a main routine with small subroutines in it

like this

start:
scan
drive as needed
go back to start
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline robonoobTopic starter

  • Robot Overlord
  • ****
  • Posts: 149
  • Helpful? 0
Re: Arduino first robot programming questions
« Reply #8 on: March 22, 2008, 05:40:58 AM »
i made anew code..does the same basically but it now sees chair legs too not only walls :D

Code: [Select]
#include <Servo.h>

Servo left;
Servo right;
Servo scanner;

int object = 0;
int val = 0;
int IR = 5;
int IRpos = 0;
int scan = 0;
int scanlist = 0;
int summa = 0;
int tehe1 = 0;
int tehe2 = 0;
int summa2 = 0;
int movemode = 0;
int object2 = 0;
int summa3 = 0;
int tehe3 = 0;


void setup() {
  right.attach(6);
  right.setMinimumPulse(500);
  right.setMaximumPulse(2500);
  right.write(89);
  left.attach(7);
  left.setMinimumPulse(500);
  left.setMaximumPulse(2500);
  left.write(89);
  scanner.attach(5);
  scanner.setMinimumPulse(500);
  scanner.setMaximumPulse(2500);
  scanner.write(90);
  Serial.begin(9600);
  Serial.println("valmis töötama");
  delay(1000);
}
void loop() {
  summa = 0;
  summa2 = 0;
  summa3 = 0;
  for(int IRpos=72; IRpos <= 112; IRpos++){
    scanner.write(IRpos);
    Servo::refresh();
    val = analogRead(5);
    delay(10);
    if(val > summa){
      summa = val;
    }
  }
  if(summa < 600){
    right.write(180);
    left.write(0);
  }
  if(summa >= 600){
    right.write(89);
    left.write(89);
    for(int IRpos=112; IRpos >= 0; IRpos--){
      scanner.write(IRpos);
      Servo::refresh();
      delay(10);
    }
    for(int IRpos=0; IRpos <= 30; IRpos++){
      scanner.write(IRpos);
      Servo::refresh();
      val = analogRead(5);
      delay(10);
      if(val > summa2){
        summa2 = val;
      }
    }

    if(summa2 < 600){
      right.write(180);
      left.write(180);
      Servo::refresh();
    }
    if(summa2 >= 600) {
      right.write(89);
      left.write(89);
      Servo::refresh();
for(int IRpos=30; IRpos <= 180; IRpos++){
    scanner.write(IRpos);
    Servo::refresh();
    delay(10);
    }
for(int IRpos=180; IRpos >= 150; IRpos--){
    scanner.write(IRpos);
    Servo::refresh();
    val = analogRead(5);
    delay(10);
    if(val > summa3){
      summa3 = val;
      }
      }
      Serial.println(summa3);
     if(summa3 < 690){
right.write(0);
left.write(0);
 Servo::refresh();
        }
        if(summa3 >= 690){
          right.write(0);
          left.write(180);
          }
     
  }
}
}

but i didn't get this timing thing still... what i want to do is i want the robot to go backwards for 1 second and turn right and left 0,6 seconds when needed.

with cycles you mean what? is one cycle like one time the robot does the whole code from start to end, and then starts it all over again?
plz look at my code and you'll see that it scans from the left(if an object is in front) and if there is no object then i turns left... but i cant controll the time it turns left. it just turns left for a little amount of time. but i want to manipulate with this time manually(i mean type it into the code).
i don't think i can do this with cycles... or can i? if i can then how... a sample code would be a useful thing

thank you

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: Arduino first robot programming questions
« Reply #9 on: March 22, 2008, 06:33:55 PM »
OK here i go trying to explain doing it by timing

Say you wanted a servo to move forward for 1 second using time based

Many beginners do this :

Code: [Select]
Do this routine 50 times
Servo GoForward
pause 20 mS

When you put in a 20mS pause afterwards you made a pause in there so that you establish that it takes 20mS to do the action.  So then to do it for 1000ms ( 1 second) you divide 1000/20. This works but is not exact and extra pausing for no reason.

By cycles I meant that you would see how much and how long the servo turns with each pulse and then divide the distance you want the servo to go by the amount it goes per pulse.
Then if you want it to go say 1 foot , you would see how much the servo turns with each pulse and merely divide it.
Then the answer you got would be the amount of servo pulses or cycles you need.

If you don't get what I am saying then don't worry about it ! Its not necessary since you are doing cycles based, the right way!

 If you want it to go forward for 5 seconds then you would see how much consecutive pulses the servo needs to go forward for 1 second and then multiply that by 5.
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline robonoobTopic starter

  • Robot Overlord
  • ****
  • Posts: 149
  • Helpful? 0
Re: Arduino first robot programming questions
« Reply #10 on: March 23, 2008, 06:53:57 AM »
why isn't anybody helping me here... i want to know how you ppl would make it scan..

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: Arduino first robot programming questions
« Reply #11 on: March 23, 2008, 07:07:15 AM »
why isn't anybody helping me here... i want to know how you ppl would make it scan..

I dont get the question

To make it scan you would just send the servo pulses from 100 mS adding 10 or something like that until 200mS and then subtract.
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline robonoobTopic starter

  • Robot Overlord
  • ****
  • Posts: 149
  • Helpful? 0
Re: Arduino first robot programming questions
« Reply #12 on: March 23, 2008, 08:04:13 AM »
i mean how would you like it ti scan? like mine scans only from robots front and then if an object comes then it scans from the sides... but some ppl like to scan from left to right and then move.

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: Arduino first robot programming questions
« Reply #13 on: March 23, 2008, 08:36:43 AM »
i mean how would you like it ti scan? like mine scans only from robots front and then if an object comes then it scans from the sides... but some ppl like to scan from left to right and then move.

post a video please , I don't get what you are saying
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline robonoobTopic starter

  • Robot Overlord
  • ****
  • Posts: 149
  • Helpful? 0
Re: Arduino first robot programming questions
« Reply #14 on: March 25, 2008, 11:25:11 AM »
never mind... i got this to work... but i now have another strange problem... i made this scanning code :
Code: [Select]
for(int IRpos=0; IRpos <= 20; IRpos++) {
    scanner.write(IRpos);
    Servo::refresh();
    val = analogRead(5);
    delay(5);
    if(val > summa) {
      summa = val;
    }
  }
  for(int IRpos=20; IRpos <= 70; IRpos++) {
    scanner.write(IRpos);
    Servo::refresh();
    delay(del);
  }
  for(int IRpos=70; IRpos <= 110; IRpos++) {
    scanner.write(IRpos);
    Servo::refresh();
    val = analogRead(5);
    delay(del);
    if(val > summa2) {
      summa2 = val;
    }
  }
  for(int IRpos=110; IRpos <= 160; IRpos++) {
    scanner.write(IRpos);
    Servo::refresh();
    delay(del);
  }
  for(int IRpos=160; IRpos <= 180; IRpos++) {
    scanner.write(IRpos);
    Servo::refresh();
    val = analogRead(5);
    delay(del);
    if(val > summa3) {
      summa3 = val;
    }
  }

can u see those summa, summa2 and summa3 ??
the IR is on the servo to scan and it scans left, then front and then right. but here is the problem:
when it scans
1) left, 30 cm gives reading 590
2) front, 30 gives reading 480
3) right, 30 cm gives reading 435

this 30 cm is measured from the IR not from the side of the robot or any other place...

i don't get the thing why does it give so different readings... and it's not just one reading for instance it allways gives the same reading when it scans left(approximately the same)...

i really hope it is something with the code...
mby it helps if i upload the whole code? anyways here it is:
Code: [Select]
#include <Servo.h>

Servo left;
Servo right;
Servo scanner;

int object = 0;
int val = 0;
int IR = 5;
int IRpos = 0;
int scan = 0;
int scanlist = 0;
int summa = 0;
int tehe1 = 0;
int tehe2 = 0;
int summa2 = 0;
int movemode = 0;
int object2 = 0;
int summa3 = 0;
int tehe3 = 0;
int var = 0;
int arv = 0;
int del = 4;
int summa4 = 0;
int summa5 = 0;
int summa6 = 0;

void setup() {
  right.attach(6);
  right.setMinimumPulse(500);
  right.setMaximumPulse(2500);
  right.write(89);
  left.attach(7);
  left.setMinimumPulse(500);
  left.setMaximumPulse(2500);
  left.write(90);
  scanner.attach(5);
  scanner.setMinimumPulse(500);
  scanner.setMaximumPulse(2500);
  while(arv < 5000){
    scanner.write(0);
    Servo::refresh();
    arv++;
  }
  Serial.begin(9600);
  Serial.println("valmis töötama");
  delay(1000);
}
void loop() {
  summa = 0;
  summa2 = 0;
  summa3 = 0;
  summa4 = 0;
  summa5 = 0;
  summa6 = 0;
  for(int IRpos=0; IRpos <= 20; IRpos++) {
    scanner.write(IRpos);
    Servo::refresh();
    val = analogRead(5);
    delay(5);
    if(val > summa) {
      summa = val;
    }
  }
  for(int IRpos=20; IRpos <= 70; IRpos++) {
    scanner.write(IRpos);
    Servo::refresh();
    delay(del);
  }
  for(int IRpos=70; IRpos <= 110; IRpos++) {
    scanner.write(IRpos);
    Servo::refresh();
    val = analogRead(5);
    delay(del);
    if(val > summa2) {
      summa2 = val;
    }
  }
  for(int IRpos=110; IRpos <= 160; IRpos++) {
    scanner.write(IRpos);
    Servo::refresh();
    delay(del);
  }
  for(int IRpos=160; IRpos <= 180; IRpos++) {
    scanner.write(IRpos);
    Servo::refresh();
    val = analogRead(5);
    delay(del);
    if(val > summa3) {
      summa3 = val;
    }
  }
  if(summa2 > 480){
    if(summa > 590){
      if(summa3 > 435){
 Serial.println("pööran ümber");
      }
      else {
Serial.println("Pööran paremale");
      }
    }
    else {
Serial.println("Pööran vasakule");
    }
  }
  else {
Serial.println("Sõidan otse");
  }
 
 
  for(int IRpos=180; IRpos >= 160; IRpos--) {
    scanner.write(IRpos);
    Servo::refresh();
    val = analogRead(5);
    delay(del);
    if(val > summa4) {
      summa4 = val;
    }
  }
  Serial.print("summa4   ");
  Serial.println(summa4);
  for(int IRpos=160; IRpos >= 110; IRpos--) {
    scanner.write(IRpos);
    Servo::refresh();
    delay(del);
  }
  for(int IRpos=110; IRpos >= 70; IRpos--) {
    scanner.write(IRpos);
    Servo::refresh();
    val = analogRead(5);
    delay(del);
    if(val > summa5) {
      summa5 = val;
    }
  }
  Serial.print("summa5   ");
  Serial.println(summa5);
  for(int IRpos=70; IRpos >= 20; IRpos--) {
    scanner.write(IRpos);
    Servo::refresh();
    delay(del);
  }
  for(int IRpos=20; IRpos >= 0; IRpos--) {
    scanner.write(IRpos);
    Servo::refresh();
    val = analogRead(5);
    delay(del);
    if(val > summa6) {
      summa6 = val;
    }
  }
  Serial.print("summa6   ");
  Serial.println(summa6);
    if(summa5 > 535){
    if(summa4 > 500){
      if(summa6 > 290){
 Serial.println("pööran ümber");
      }
      else {
Serial.println("Pööran paremale");
      }
    }
    else {
Serial.println("Pööran vasakule");
    }
  }
  else {
Serial.println("Sõidan otse");
  }
}


notice, that i haven't made the robot move yet, i just put the text where the moving commands should come, just to make it easier to detects any mistaces...

anyway i really hope you can help me with this.


edit: this isn't the code i showed you before... i managed to finish this last one...it wasn't good enough for me so i decided to make another one :P i want it to be faster..
« Last Edit: March 25, 2008, 11:28:12 AM by robonoob »

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: Arduino first robot programming questions
« Reply #15 on: March 30, 2008, 10:40:05 AM »
Hi robonoob

Any luck with this? I highly recommend putting comments in your code - its hard to figure out what your code is doing otherwise . . .

Also, I didn't quite understand your last question.

Are you saying that despite distance being the same, the Sharp IR reads a different value? I noticed the output values are above 255, which is the max for an 8 bit ADC. Are you using 10 bit?

Offline robonoobTopic starter

  • Robot Overlord
  • ****
  • Posts: 149
  • Helpful? 0
Re: Arduino first robot programming questions
« Reply #16 on: April 02, 2008, 12:19:53 PM »
Code: [Select]
Are you saying that despite distance being the same, the Sharp IR reads a different value?yes thats exactly what I'm telling... i don't understand the problem...

yea i think i should put some comments :D but it takes to many time lol :D

Code: [Select]
I noticed the output values are above 255, which is the max for an 8 bit ADC. Are you using 10 bit?umm i don't get what are u talking about lol? 10bit what? and where is that 255?

PS: oh btw don't pay attention on these foreign language words(summa for instance)... i just typed them in my mother tongue for better understanding :D

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: Arduino first robot programming questions
« Reply #17 on: April 06, 2008, 11:29:50 AM »
The typical ADC, 8 bit and stored as an int, can only do values from 0->255.

Also, the Sharp IR sensor has a lot of electrical noise in it. Print out a lot of values and see what the readings look like.

 


Get Your Ad Here

data_list