2

Author Topic: HEXAPOD builders need Atmega128  (Read 3097 times)

0 Members and 1 Guest are viewing this topic.

Offline benjiTopic starter

  • Supreme Robot
  • *****
  • Posts: 832
  • Helpful? 0
HEXAPOD builders need Atmega128
« on: March 02, 2008, 09:48:11 AM »
hey folks as long as im doing a hexapod i finally got the best processor for it
its the atmega128,,why? cuz this boy can provide you 7 PWM pins
and in a hexapod you can send  6 signals(different pwms) only to your 12 servo,
check with the movement and youll know what i mean

first signal/ legs 1/3/4/6 forward and backward
second signal/ legs 2/5 forward and backward
third signal/ legs 1/3 up down
fourth signal legs 4/6 up down
fifth signal leg 2 up down
sixth signal leg 5 up down

i guess this processor would make programming such hexapods somthin as hard as chewin gum
good ol' BeNNy

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: HEXAPOD builders need Atmega128
« Reply #1 on: November 24, 2008, 01:13:42 PM »
Anthing with 12 i/o pins or more should work for a 12 servo hexapod. pwm isnt strictly necessary, i would even edge away from it for servo control

Offline boo

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
Re: HEXAPOD builders need Atmega128
« Reply #2 on: November 24, 2008, 04:58:18 PM »
Anthing with 12 i/o pins or more should work for a 12 servo hexapod. pwm isnt strictly necessary, i would even edge away from it for servo control

I'm actually having a hard time finding a way to control the 18 servos in my hexapod project
how on earth can you control servos without PWM ports?
I'm sure it can be done but I just can't put my finger on it
any help would be apreciated ^^

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: HEXAPOD builders need Atmega128
« Reply #3 on: November 24, 2008, 07:10:32 PM »
All you need to do is switch an output high for the amount of time corresponding to the position you want(usually between 1-2 ms) and then turn it back off again(for about 10 - 20ms) then back on again etc... It has to stay off or low for a specific amount of time (too early and you get erratic behaviour and too late the servo goes to sleep).

 Thats why pwm isnt recommended, you control the high pulse to set the servo's position (between 1 - 2 ms), but the low pulse becomes exactly the same length of the high pulse. The low pulse should be around 10ms - 20ms.
As you can see even at the 2ms high pulse extreme, the standard pwm square wave would produce also a 2ms low. This is 8ms shorter than what it should be. You are not giving the servo a chance to do its before you send the next high pulse resulting in erratic behaviour and juddery movements.

this is how you would control a servo on port b6 without pwm (psuedi codeish):

#define SERVO_CONTROL   pin_B0

// Move the servo to the centre
// send 6 pulses to make sure it gets there accurately
for (counter2=0;counter2<6;counter2++) {
   output_high(SERVO_CONTROL); // high pulse
   delay_us(1500);         // 1500us = 1.5ms
   output_low(SERVO_CONTROL);  // low pulse
   delay_ms(10);
}

« Last Edit: November 24, 2008, 07:13:13 PM by paulstreats »

Offline boo

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
Re: HEXAPOD builders need Atmega128
« Reply #4 on: November 25, 2008, 05:24:44 AM »
yeh but with 18 servos all moving at once, I can't exactly wait that long before my next pulse that's kind of my problem.
If all of them get 2.0 ms, 2.0x18=36 ms which would go over my 20ms window for sending in my next pulse.

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: HEXAPOD builders need Atmega128
« Reply #5 on: November 25, 2008, 07:13:17 PM »
There are a few ways of doing this, I would look at a recurring interrupt function that sets each servo. Each servo gets its own variable and the interrupt can do the work of deciding wether its time to switch one off or on. It can be complicated, its similar to making a multitasking system but there is easily enough time to do this.

 By using an interrupt, you dont have to wait for 1 servo to complete its high cycle before moving on to the next one.The trouble is that you can lose a small amount of precision while the interrupt checks the servo states and decides the next course of action. Of course this can be optimised with a lot of work.

It goes without saying that a dedicated microcontroller should be used for this many servo's with another microcontroller doing the logic and just sending movement commands to it.

Okay you probably dont need an interrupt, look at the quick and dirty psuedocode below:

Code: [Select]
#define SERVO_CONTROL1   pin_B0
#define SERVO_CONTROL 2  pin_B1
timer0 = 0; (increment us)
int servo1starttime = 0;         //what the timer value is when the last phase was issued
int servo1positiontime = 1500;    //what the timer value is to set the position (1500)us for centered
boolean servo1ishigh = false;      //is the servo in a high state or a low state?
int servo2starttime = 0;
int servo2positiontime = 1500;
boolean servo2ishigh = false;
int allservolowtime = 20000    //as in they should all be low for this amount of time(us)

void main(){
    while(1){
        setupServo's();//havent written this, but assign times to the servo's for how long they need to be high for
        changeStates();
    }
}

void changeStates(){
    if (servo1ishigh == false && (timer0-servo1starttime) >= allservolowtime){
        servo1ishigh =false;
        servo1starttime = timer0;
    }

    if (servo1ishigh == true && (timer0-servo1starttime) >= servo1positiontime){
        servo1ishigh =true;
        servo1starttime = timer0;
    }

    if (servo2ishigh == false && (timer0-servo2starttime) >= allservolowtime){
        servo2ishigh =false;
        servo2starttime = timer0;
    }

    if (servo2ishigh == true && (timer0-servo2starttime) >= servo2positiontime){
        servo2ishigh =true;
        servo2starttime = timer0;
    }




}


Okay so it only shows 2 servo's and misses a lot such as timer rollover, how to actually control timers etc, but it does give an example of how to control 2 or even more servos without having to wait for one of them to complete its high cycle. As usual there is more than one way to do this, the above is just an example of one of them.





Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
Re: HEXAPOD builders need Atmega128
« Reply #6 on: November 25, 2008, 08:50:59 PM »
I have seen some code that works like this:

start the main loop
  sort the pulses for all servos so they are in ascending order and store them in an array
  turn all of them ON
  start a timer
  start a For loop
     check the timer until it equals the value of the first array element (servo pulse)
     turn OFF that servo
  next array element (exit the loop when all servos are OFF)
  check the timer until it equals 20ms
go back to main loop

During the last check for timer to get to 20ms, you can do other things, like check sensors, get serial commands and so on. You will have about 18ms untill you need to go back to the main loop, so if you need to read a Sharp IR sensor, set a counter and read the sensor every other run through the loop. This ensures the sensor has enough time to do another measurement. Some sensors work faster than others, for instance, the Ping sensor you can read every run through the loop and it will eat just a few ms for reading it.

« Last Edit: November 25, 2008, 08:52:24 PM by Ro-Bot-X »
Check out the uBotino robot controller!

Offline boo

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
Re: HEXAPOD builders need Atmega128
« Reply #7 on: November 26, 2008, 05:15:28 PM »
I think I got it
hope I manage to code it right

thx for the help guys
u've been great ^^

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 108
Re: HEXAPOD builders need Atmega128
« Reply #8 on: November 26, 2008, 10:05:24 PM »
Thats why pwm isnt recommended, you control the high pulse to set the servo's position (between 1 - 2 ms), but the low pulse becomes exactly the same length of the high pulse. The low pulse should be around 10ms - 20ms.
As you can see even at the 2ms high pulse extreme, the standard pwm square wave would produce also a 2ms low. This is 8ms shorter than what it should be. You are not giving the servo a chance to do its before you send the next high pulse resulting in erratic behaviour and juddery movements.

@paulstreats - This is true for a 50% duty cycle - but the whole point of PWM is that you can change the duty cycle - ie the total of 'off + on' stays the same but you can change how long the output is 'on' from 0% to 100% of the total time. So for a servo the total 'off+on' time can be say 20ms and you can adjust the 'on' time from anywhere between 0ms and 20ms (although obviously 1ms to 2ms is the useful range). So if you hardware has 6 PWM channels then you can control 6 servos purely by changing the value in the 6 PWM OCR registers - everything else is done by the hardware.

Think you may benefit from looking at PWM in more detail  ;)
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,653
  • Helpful? 169
    • Society of Robots
Re: HEXAPOD builders need Atmega128
« Reply #9 on: December 02, 2008, 02:45:22 AM »
I haven't done it myself, but in theory this is how you do it:

1) Store the desired pulse time for each servo into an array.
2) Sort the array from lowest time to highest time.
3) reset timer to 0
4) command all servo pins as high
5) set a timer interrupt to trigger at the first/next value in the array
6) upon receiving an interrupt, command that servo pin as low
7) repeat steps 5 and 6 until all servo pins go low

In theory the entire process will take about 2ms . . . the problem however is what happens when servos share the same time . . .

If you do the delay method on 18 servos . . . and you put 6 on PWM . . . that leaves 12 servos running an average of 1.5ms each, or 18ms. Even if it takes 50ms you probably won't notice the difference . . .

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
Re: HEXAPOD builders need Atmega128
« Reply #10 on: December 02, 2008, 08:40:15 PM »
I haven't done it myself, but in theory this is how you do it:

1) Store the desired pulse time for each servo into an array.
2) Sort the array from lowest time to highest time.
3) reset timer to 0
4) command all servo pins as high
5) set a timer interrupt to trigger at the first/next value in the array
6) upon receiving an interrupt, command that servo pin as low
7) repeat steps 5 and 6 until all servo pins go low

In theory the entire process will take about 2ms . . . the problem however is what happens when servos share the same time . . .

If you do the delay method on 18 servos . . . and you put 6 on PWM . . . that leaves 12 servos running an average of 1.5ms each, or 18ms. Even if it takes 50ms you probably won't notice the difference . . .

Instead of repeating steps 5 and 6, can you set the timer interrupt from the interrupt itself for the next steps? And also check the next value in the array and if it's equal, set that servo Off too, until the value is greater? This way all the servos with the same pulse duration will be turned off almost instantly. And the last element in the array can set the timer interrupt until the 20 ms cycle passes, then when that interrupt occurs, a flag is raised so the cycle is restarted...
Check out the uBotino robot controller!

 


Get Your Ad Here

data_list