go_away

Author Topic: Sequencing Servos and Autonomous Servo Control w/ Sharp IR  (Read 7998 times)

0 Members and 1 Guest are viewing this topic.

Offline CanabotsTopic starter

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 245
  • Helpful? 6
  • It's not a bug, it's a feature!
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #30 on: December 13, 2008, 04:49:19 PM »
Hello!

Sorry for the delay! I've been busy lately.

Sorry, I know I'm not the greatest at explaining things. Here's a pseudocode of what I'm interested in:



Center servo

Loop Forever

       for 500 milliseconds
       servo1 turn right
       servo2 turn left

       pause for 250 milliseconds
       
       FOR 250 milliseconds
       servo2 go right
       servo1 center
 End


To cosminprund: Thank you! I'll try the code whe I can!!!


Canabots


My robotics, electronics, software, or other stuff blog:
www.saltech.wordpress.com

Offline CanabotsTopic starter

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 245
  • Helpful? 6
  • It's not a bug, it's a feature!
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #31 on: December 15, 2008, 06:39:46 PM »
Hmmmmm...

After some thought, I suppose preprogrammed movements for later could also work well for my application. I'm building a small robot arm andwant it to do some basic sequenced movements in a repeated pattern, if that helps (probably should have mentioned it earlier, sorry :)). Later, I'd implement it for autonomous function (btw, this is a VERY basic arm, so I'm not into anything too complex :P).

Anyhoo, thanks!

Canabots

« Last Edit: December 15, 2008, 06:41:00 PM by Canabots »
My robotics, electronics, software, or other stuff blog:
www.saltech.wordpress.com

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #32 on: December 16, 2008, 05:41:32 AM »
Without some kind of external input (analog or digital / photoresistor or basic switch) you can't have anything but preprogrammed movements. Getting the preprogrammed movements to work seems to be a prerequisite to making the movements work in relation some sort of external input.

Offline CanabotsTopic starter

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 245
  • Helpful? 6
  • It's not a bug, it's a feature!
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #33 on: December 16, 2008, 07:18:59 PM »
Without some kind of external input (analog or digital / photoresistor or basic switch) you can't have anything but preprogrammed movements. Getting the preprogrammed movements to work seems to be a prerequisite to making the movements work in relation some sort of external input.

Come to think of it, you're right...Thanks :)

I suppose now it's just a matter of writting the program. Would it involve moreheader files? Actually, how do you write header files? Are they much different from writing a C program (whenever I get the option to save as a .c file, .h file is included with the option). I suppose this is general knowledge I'm going to have to learn if I'm going to do more advanced robots later on :P

Thank you!

Canabots
« Last Edit: December 16, 2008, 07:20:17 PM by Canabots »
My robotics, electronics, software, or other stuff blog:
www.saltech.wordpress.com

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #34 on: December 17, 2008, 03:31:05 AM »
You allready have an sample program that does "preprogrammed movements". Did you try it? It's just a variation of the original program (the one that centers the servo). You should try it and then come back with questions based on that.

As for header files: technically they are the same thing as "c" files - the "h" is just a convention. Practically you may think of them as something you need to "#include" so you can access functions from a library. In your sample program you had to include the "delay.h" header so you can use the DelayMs() function and the DelayUs() function. You'll only need to write header files of your own when you'll write bigger programs. The "sequenced movements" program is tiny by any measure.

Offline CanabotsTopic starter

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 245
  • Helpful? 6
  • It's not a bug, it's a feature!
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #35 on: December 17, 2008, 07:03:58 AM »
By preprogrammed movements, do you mean the DelayMS and the DelayUs? If so how would I implement those into timed sequences? I suppose this is where I'm getting confused.

Thank you for all your troubles! ;D
My robotics, electronics, software, or other stuff blog:
www.saltech.wordpress.com

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #36 on: December 17, 2008, 07:22:33 AM »
Here's the a quote from the post where I give you the sample program code:

Quote
What the code does. Imagine the two windshield wipers on a car. Imagine you've got two servos, one for each windshield wiper. The first sequence will make the two wipers move from left to right and back, at the same time. It will take 10 seconds to move from left to right and another 10 seconds to move from right to left. When that's done the two servos will start moving in the opposite directions (one "wiper" moves from left to right, the other "wiper" moves from right to left). This will happen 4 times faster then the first example!

Here's a quote from your request (your request was a reply to the post where I'm giving you code):

Quote
Center servo

Loop Forever

       for 500 milliseconds
       servo1 turn right
       servo2 turn left

       pause for 250 milliseconds
       
       FOR 250 milliseconds
       servo2 go right
       servo1 center
 End

Put the code I gave into your MCU and see what it does. Try to modify it so it does what you want. If you encounter difficulties with anything ask again.

Offline CanabotsTopic starter

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 245
  • Helpful? 6
  • It's not a bug, it's a feature!
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #37 on: January 02, 2009, 01:25:05 PM »
Sorry for taking so long.

I tried compiling the code, but in the build window, this Error pops up (I put stars next to it)):

Code: [Select]
Build C:\My_Robots\ARM 13\Servo Center\Servo_Seq\Servo_Seq for device 16F684
Using driver C:\Program Files\HI-TECH Software\PICC\LITE\9.60\bin\picl.exe

delay.obj is up-to-date
Servo_Seq vSoR.obj is up-to-date
Executing: "C:\Program Files\HI-TECH Software\PICC\LITE\9.60\bin\picl.exe" -oServo_Seq.cof -mServo_Seq.map delay.obj "Servo_Seq vSoR.obj" --chip=16F684 -P -q -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"


*****Error   [499] ; . undefined symbol:
_Delay (Servo_Seq vSoR.obj) *******


********** Build failed! **************

Do I need to declare this as a variable or and unsigned char of some sort (appologies, programming is one of my weaknesses)  :P

Thank you! :)
« Last Edit: January 02, 2009, 01:26:58 PM by Canabots »
My robotics, electronics, software, or other stuff blog:
www.saltech.wordpress.com

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #38 on: January 02, 2009, 01:56:34 PM »
I made an error when I wrote that code. The function name I wanted was "DelayMs", not "Delay". Sorry about that. Change the name of the function or use this reviewed code, this time I got around to test it MPLAB, it compiles just fine. I no longer have the circuit built so I'll just assume it also works fine :)

Code: [Select]
#include <pic.h>
#include "delay.h"

__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT
 & BORDIS & IESODIS & FCMDIS);

// This routine takes two parameters, from 0 to 250, that will represent 250 possible positions
// for every one of the two servos.
void servopulse(unsigned char PulseRA2, unsigned char PulseRA1) {
  // First pulse RA2
  RA2=1;
  DelayMs(1);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  RA2=0;
  // Now pulse RA1
  RA1=1;
  DelayMs(1);
  DelayMs(PulseRA1);
  DelayMs(PulseRA1);
  DelayMs(PulseRA1);
  DelayMs(PulseRA1);
  RA1=0;
  // Wait for a while; The delay is shorter then the
  // original delay to account both 2ms pulses
  DelayMs(16); 
}

// This is one possible first sequence for the servos. Both servos will move at the same time
// to the same positions. I want the full swing from left to right to take about 10 seconds; Since I'll
// have 250 steps for each servo, the servo will need to hold each of those 250 steps for
// (20 * 1000) / 250 = 40 ms. Since the "servopulse" routine takes about 20 ms I'll call that routine
// twice for each of the 250 positions.

void sequence1(void) {
  unsigned char pos;
  // swing from left to right
  for(pos=0;pos<250;pos++) {
    servopulse(pos,pos);   
    servopulse(pos,pos);
  }
  // swing from right to left
  for (pos=250;pos>0;pos--) {
    servopulse(pos,pos);
    servopulse(pos,pos);
  }
}

// This second sequence will make the servos move in oposite directions. This time I want the swing
// to take about 2.5 seconds; If I'd stop at each of the 250 possible positions along the way
// I'd need to make each position hold for only 10ms. This is not possible because the "servopulse"
// routine takes about 20ms to run, so I'll simply skip steps.

void sequence2(void) {
  unsigned char pos;
  unsigned char pos2;
  // First swing
  for (pos=0;pos<250;pos=pos+2) {
    pos2=250-pos;
    servopulse(pos,pos2);
  }
  // Reverse swing
  for (pos=0;pos<250;pos=pos+2) {
    pos2=250-pos;
    servopulse(pos2,pos);
  }
}

main()
{
  TRISA = 0b11111001; // Make RA2 and RA1 outputs

  // Do the first sequence once
  sequence1();

  // Look pthe second sequence for ever
  while(1 == 1) sequence2();   

}

Offline CanabotsTopic starter

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 245
  • Helpful? 6
  • It's not a bug, it's a feature!
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #39 on: January 24, 2009, 11:42:49 AM »
Ok, it's been a while,

I finally managed to throw the circuit back together and tested the code. When I tested it, the servos would only turn in one direction and not anywhere else. I think it's because servopulse is only dictated to go in one direction, so I tried to change it with this result:
Code: [Select]
#include <pic.h>
#include "delay.h"

__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT
 & BORDIS & IESODIS & FCMDIS);

// This routine takes two parameters, from 0 to 250, that will represent 250 possible positions
// for every one of the two servos.
void servopulse(unsigned char PulseRA2)
  {
  // First pulse RA2
RA2=1;
  DelayMs(1);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  RA2=0;
  // Wait for a while; The delay is shorter then the
  // original delay to account both 2ms pulses
  DelayMs(16); 
}
void servopulse2(unsigned char PulseRA2)
{
  RA2=1;
  DelayMs(2);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  RA2=0;
  // Wait for a while; The delay is shorter then the
  // original delay to account both 2ms pulses
  DelayMs(16); 
}

// This second sequence will make the servos move in oposite directions. This time I want the swing
// to take about 2.5 seconds; If I'd stop at each of the 250 possible positions along the way
// I'd need to make each position hold for only 10ms. This is not possible because the "servopulse"
// routine takes about 20ms to run, so I'll simply skip steps.

void sequence2(void)
{
  unsigned char pos;
  unsigned char pos2;
  // First swing
  for (pos=0;pos<250;pos=pos+2)
{
    pos2=250-pos;
    servopulse(pos,pos2); 
}
  // Reverse swing
  for (pos=250;pos>250;pos=pos+2)
{
    pos2=250-pos;   
servopulse2(pos2);
  }
}

main()
{
  TRISA = 0b1111011; // Make RA2 and RA1 outputs

 
  // Loop the second sequence for ever
  while(1 == 1) sequence2();   

}

It's only meant to control one servo.

For some reason though, I always gets this error:
Error   [186] C:\My_Robots\ARM 13\Servo Center\Servo_Seq\Servo_Seq vSoR.c; 50.20 too many function arguments

Though I have no text on line 50, column 20. I don't quite get it  :-\


Thanks to any help in advance! :)



*****EDIT*****
I've fixed that error, but it still doesn't scan. I've also tried to change a few things in the code, but the servo pulses once, then spins all the way to th other end and keeps trying to turn at the limits. Here's the version of the code Im trying:
Code: [Select]
#include <pic.h>
#include "delay.h"

__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT
 & BORDIS & IESODIS & FCMDIS);



void servopulse(unsigned char PulseRA2)
  {
  // First pulse RA2
RA2=1;
  DelayMs(1);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  RA2=0;
 
  DelayMs(40); 
}

void servopulse2(unsigned char PulseRA2)
{
  RA2=1;
  DelayMs(2);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  RA2=0;
  DelayMs(40); 
}

void sequence(void) {
  unsigned char pos;
  // swing from left to right
  for(pos=0;pos<250;pos++) {
    servopulse(pos);   
    servopulse(pos);
  }
  // swing from right to left
  for (pos=250;pos>0;pos--) {
    servopulse2(pos);
    servopulse2(pos);
  }
}

main()
{
  TRISA = 0b1111011; // Make RA2 an output

 
  while(1 == 1)
{
sequence ();
}
}

« Last Edit: January 24, 2009, 09:32:39 PM by Canabots »
My robotics, electronics, software, or other stuff blog:
www.saltech.wordpress.com

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #40 on: January 25, 2009, 01:43:01 AM »
If it's an new circuit, is it the same circuit? Are you sure?
And is it the same project or have you started an new project from scratch?

Your servos are not going to the right place because you've got the timing wrong. The best way to test that (besides using an oscilloscope or an multimeter with a frequency meter function) is to use an LED and make it blink once per second.

Here's a quote from myself ;D
Quote
Open up the "delay.h" file and edit the first line, that's where the MCU clock speed is set up. Change that to read "4Mhz" (don't tuch the zeroes, change the "16" with "4")

Offline CanabotsTopic starter

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 245
  • Helpful? 6
  • It's not a bug, it's a feature!
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #41 on: January 25, 2009, 06:42:30 AM »
It's the exact same circuit as before. Also, I think I should move back to the code you wrote earlier, and understand that first.

Though the issue is similar with the code you gave me though, they servos only pulse in one direction and they keep going. Do you think you might know why?


(Yep, I did change the first line in "delay.h".   :D)

Thank you  :)
My robotics, electronics, software, or other stuff blog:
www.saltech.wordpress.com

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #42 on: January 25, 2009, 10:05:44 AM »
Have you got an LED on the circuit you're using for the servos? If I remember well you're using an PicKit1 - but that has no headers for connecting an servo. Put an LED on the circuit you're testing the servos with and make it blink once per second, to make sure your code works and timing is right.

Offline CanabotsTopic starter

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 245
  • Helpful? 6
  • It's not a bug, it's a feature!
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #43 on: January 25, 2009, 10:34:51 AM »
Ok, LEDs are hooked up.
It pulses once per second for three seconds, then it goes into a pulsing frenzy in which it will gradually slow down (this is for the code you wrote).

I think the timing is a tad off :P

And thank you very much for responding to this thread well over a month since it's been created.  I appreciate all the help.
« Last Edit: January 25, 2009, 10:36:18 AM by Canabots »
My robotics, electronics, software, or other stuff blog:
www.saltech.wordpress.com

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #44 on: January 25, 2009, 12:01:39 PM »
Can you post your current version of the code? I don't remember giving you code that would blink once per second and then go into  "pulsing frenzy". If you were to hook a LED to the servo signal line that's just about what you'd see, a blinking frenzy - but you say it starts by blinking at about once per second: that's not a servo signal.

Other then this I'm getting ready to give up :( No more ideas. It should work!

Offline CanabotsTopic starter

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 245
  • Helpful? 6
  • It's not a bug, it's a feature!
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #45 on: January 25, 2009, 12:17:32 PM »
Here it is:


Code: [Select]
#include <pic.h>
#include "delay.h"

__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT
 & BORDIS & IESODIS & FCMDIS);

// This routine takes two parameters, from 0 to 250, that will represent 250 possible positions
// for every one of the two servos.
void servopulse(unsigned char PulseRA2, unsigned char PulseRA1) {
  // First pulse RA2
  RA2=1;
  DelayMs(1);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  DelayMs(PulseRA2);
  RA2=0;
  // Now pulse RA1
  RA1=1;
  DelayMs(1);
  DelayMs(PulseRA1);
  DelayMs(PulseRA1);
  DelayMs(PulseRA1);
  DelayMs(PulseRA1);
  RA1=0;
  // Wait for a while; The delay is shorter then the
  // original delay to account both 2ms pulses
  DelayMs(16); 
}

// This is one possible first sequence for the servos. Both servos will move at the same time
// to the same positions. I want the full swing from left to right to take about 10 seconds; Since I'll
// have 250 steps for each servo, the servo will need to hold each of those 250 steps for
// (20 * 1000) / 250 = 40 ms. Since the "servopulse" routine takes about 20 ms I'll call that routine
// twice for each of the 250 positions.

void sequence1(void) {
  unsigned char pos;
  // swing from left to right
  for(pos=0;pos<250;pos++) {
    servopulse(pos,pos);   
    servopulse(pos,pos);
  }
  // swing from right to left
  for (pos=250;pos>0;pos--) {
    servopulse(pos,pos);
    servopulse(pos,pos);
  }
}

// This second sequence will make the servos move in oposite directions. This time I want the swing
// to take about 2.5 seconds; If I'd stop at each of the 250 possible positions along the way
// I'd need to make each position hold for only 10ms. This is not possible because the "servopulse"
// routine takes about 20ms to run, so I'll simply skip steps.

void sequence2(void) {
  unsigned char pos;
  unsigned char pos2;
  // First swing
  for (pos=0;pos<250;pos=pos+2) {
    pos2=250-pos;
    servopulse(pos,pos2);
  }
  // Reverse swing
  for (pos=0;pos<250;pos=pos+2) {
    pos2=250-pos;
    servopulse(pos2,pos);
  }
}

main()
{
  TRISA = 0b11111001; // Make RA2 and RA1 outputs

  // Do the first sequence once
  sequence1();

  // Look pthe second sequence for ever
  while(1 == 1){ sequence2(); }   

}

Also, I should mention that when the LEDS pulse once per second, the servos turn a little on each pulse and pauses when the LED is off. But after those three pulses, it just turns and then the pulses gradually slow down.

Anyway, I also did the test for the LED to only blink once per second and it works fine.

Thank you for all your ideas, they are all much appreciated!


My robotics, electronics, software, or other stuff blog:
www.saltech.wordpress.com

Offline CanabotsTopic starter

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 245
  • Helpful? 6
  • It's not a bug, it's a feature!
Re: Sequencing Servos and Autonomous Servo Control w/ Sharp IR
« Reply #46 on: January 26, 2009, 09:10:28 PM »
Ok then...

I CAN DO SEQUENCING OF SERVO MOVEMENTS!!!!!!!!! ;D

I dabbled a little with cosminprund's code. Here's my final result:
Code: [Select]
#include <pic.h>
#include "delay.h"

__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT
 & BORDIS & IESODIS & FCMDIS);

int j;

main()
{
TRISA = 0b111110;


while (1)
{
for(j=0;j<500;j++)
{
RA0 = 1;
DelayUs(250);
DelayUs(250);
DelayUs(250);
RA0 = 0;
DelayMs(18);
}
for(j=500;j>0;j--)
{
RA0 = 1;
DelayMs(2);
DelayUs(250);
DelayUs(100);
RA0 = 0;
DelayMs(18);
}
}  // End WHILE
}


This makes a servo move all the way to the left, then pauses. After the pause, it moves all the way to the right...then pauses...and the loop continues.

By changing the delay value in between the statement which makes the output high/low, you could make the servo go to different positions. By changing the value in the for statement, you change the duration of the pause (I think  :P ).

I sincerely thank all who helped or participated in this thread, particulairly cosminprund, for all the explanations, code, and for bearing with my inexperience in C programming. ;)

Thank you very much! :D

Canabots
« Last Edit: January 26, 2009, 09:11:00 PM by Canabots »
My robotics, electronics, software, or other stuff blog:
www.saltech.wordpress.com

 


Get Your Ad Here