Society of Robots - Robot Forum

Software => Software => Topic started by: tranzparency on December 14, 2010, 05:09:13 AM

Title: Axon source for macro #define wheel_left(position) servo(PORTE,6,position)
Post by: tranzparency on December 14, 2010, 05:09:13 AM
Try to keep this easy to understand.  I'm still a bit new to c and pointers are tripping me up.  So apologies if I'm asking this question a few weeks too early.  Using a Hitec HS-422 servo.  I've been playing with the libs that came with the axon. Got the servo scanning from 270degrees(far left) to 90degres(far right), and any location I want in between on an un-modified HS-422. Found that roughly 270degrees(far left) was around 200 and 90degrees(far right) was 1200 with 700 being center.  Wrote a simple a function in my control.c.  Works like a charm.  Keep in mind this only works by centering the servo on boot of the microcontroller by centering the servo to 700 in a seperate function.

For those other curious newbies.  After trial and error, this was my first successful run at moving the HS-422:

Code: [Select]
// defined in hardware.c
#define servo(position) servo(PORTH,2,position);

Code: [Select]
// initialized in control.c
int servo_loc = 0;

Code: [Select]
/* control.c */

// center servo during axon init
center_servo();

// turn far left
turn servo(200);

// turn far right
turn_servo(1200);

// somewhere in between
turn_servo(470);

// center the HS-422
void center_servo(void)
{
for(unsigned short int i=0; i<10; i++)
{
servo(700);
delay_ms(20);
}
// set current location
servo_loc = 700;
}

// move the HS-422 to desired location between 200(left) & 1200(right)
void turn_servo(int new_location)
{
// turning right
if (new_location > servo_loc)
{
for(unsigned short int i=servo_loc; i<new_location; i=i+10)
{
                        // move servo
servo(i);
// set current servo location
servo_loc = i;
delay_ms(20);
}
}
else
{
for(unsigned short int i=servo_loc; i>new_location; i=i-10)
{
                        // move servo
servo(i);
// set current servo location
servo_loc = i;
}
}
}

But I have 2 servos now.  And duplicating code is out of the question.  So wanted to create this function from a seperate HS-422 file:
turnServo(*servo, currentLocation, newLocation)

Then from control.c I can call:
turnServo(*servo1, currentLocation, 670)
turnServo(*servo2, currentLocation, 420)
[OR array of pointers to each servos]

My problem is not getting pointers in the general sense of c.  It is that the hardware.c file has a macro function: #define wheel_left(position) servo(PORTE,6,position).  I chased servo() down to SoR_Utils.  It is also a macro to (PORT_ON... etc).  PORT_ON is also a macro to 'port_letter |=  (1<<number)'. I hit a wall there.  Not sure what else to do from here.

As you can image with my limited knowledge of pointers, I was in the spaghetti. Couldn't figure out the specifc items I'd need to write a core function or struct(?) that I can create a pointer "from" to pass through the code to my turnServo() in HS-422.c.  Any ideas?

And if your curious, yes.  I've heard of and used Webbotlib.  Love it.  Worked like a champ for me.  But my goal is to understand how to roll my own by using the axon code to slowly code closer to the microcontroller.  Been digging through Webbotlib as well.  Haven't found the secret sauce yet.  Thanks.

Title: Re: Axon source for macro #define wheel_left(position) servo(PORTE,6,position)
Post by: waltr on December 15, 2010, 08:39:44 AM
Quote
My problem is not getting pointers in the general sense of c. 
Yes, pointers in C are a bit hard to learn but are essential for writing C code. What you need is a good C tutorial that covers pointers. The only one I know of off hand in in the original C book:
"The C Programming Language" by Brian Kernighan and Dennis Ritchie
http://en.wikipedia.org/wiki/The_C_Programming_Language_%28book%29 (http://en.wikipedia.org/wiki/The_C_Programming_Language_%28book%29)
and is still available
http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628 (http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628)
I bought my copy in the 1980's and still use it.

Maybe someone can point you to a good on-line tutorial.
Title: Re: Axon source for macro #define wheel_left(position) servo(PORTE,6,position)
Post by: Webbot on December 15, 2010, 08:52:03 AM
What you could do in the meantime is to give each servo a number ie
Code: [Select]
#define LEFT_WHEEL 1
#define RIGHT_WHEEL 2
Then add a routine that sends the position to the correct servo ie:
Code: [Select]
void turn_servo(int servoNumber, unsigned int position){
   switch(servoNumber){
      case LEFT_WHEEL:
         servo(PORTH,2,position);
         break;
      case RIGHT_WHEEL:
         servo(PORTE,1,position);
         break;
   }
}

Then your main code specifies either LEFT_WHEEL or RIGHT_WHEEL as the first argument to turn_servo
Title: Re: Axon source for macro #define wheel_left(position) servo(PORTE,6,position)
Post by: robots-in-brighton on December 15, 2010, 11:46:55 AM
This video tutorial on C pointers one has its admirers...

Pointer Fun C (http://www.youtube.com/watch?v=mnXkiAKbUPg#)



admin edit: invalid youtube link, I fixed it
Title: Re: Axon source for macro #define wheel_left(position) servo(PORTE,6,position)
Post by: robots-in-brighton on December 15, 2010, 03:54:02 PM
Thanks, you beat me to it. I thought I was pushing my luck, but the preview looked OK. What was my mistake?
Title: Re: Axon source for macro #define wheel_left(position) servo(PORTE,6,position)
Post by: Admin on December 15, 2010, 09:46:29 PM
Thanks, you beat me to it. I thought I was pushing my luck, but the preview looked OK. What was my mistake?
Next time, just paste the link without any forum bracket code around the link. The forum figures it out and embeds the video for you :P