Author Topic: WebbotLib - question on some internals  (Read 1654 times)

0 Members and 1 Guest are viewing this topic.

Offline madmax_707Topic starter

  • Beginner
  • *
  • Posts: 5
  • Helpful? 0
WebbotLib - question on some internals
« on: February 18, 2012, 06:07:28 AM »
Hello All,

I am new to the world of programming and I have recently started building a very basic robot. I have downloaded the Web botLib and was going through some internals of its implementation.
I am actually stuck on a piece of code and trying hard to understand how it works.
The following snippet is from the manual.

#include "servos.h"
// Define two servos
SERVO left = MAKE_SERVO(FALSE, D1,1500, 500);
SERVO right = MAKE_SERVO(TRUE , D2,1500, 500);
// Create the list - remember to place an & at the
// start of each servo name, and to use PROGMEM
SERVO_LIST PROGMEM servos[] = {&left,&right};
// Create a driver for the list of servos
SERVO_DRIVER bank1 = MAKE_SERVO_DRIVER(servos);

Now in this piece of code  MAKE_SERVO is actually defined as

#define MAKE_SERVO(inverted, iopin, center, range)  { MAKE_ACTUATOR(inverted),null,iopin,center, range, 0,0,0 }

What happens to the rest of the parameters passed in the macro ?? And, why those 3 zeros after?

Now further MAKE_ACTUATOR is implemented as
#define MAKE_ACTUATOR(inverted)  MAKE_ACTUATOR_WITH_CLASS(null,inverted)

which then calls
#define MAKE_ACTUATOR_WITH_CLASS(class,inverted)  {class, -128 ,FALSE, inverted}

What happens after this ?? How are the variables populated.

Any help would be greatly appreciated as I am dying hard to understand this

Many thanks  in advance!

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: WebbotLib - question on some internals
« Reply #1 on: February 18, 2012, 12:16:33 PM »
Not quite sure WHY you need to understand the internals- - unless its just for interest. Since these are internals then they may change from one release of WebbotLib to the next - and its my problem tp make sure that you don't need to know what has changed.

but to try and answer your q then, in reverse order, :-

A general actuator (ie motor, servo etc)  has certain properties and these are defined in actuators.h
Code: [Select]
typedef struct s_actuator_common{
const struct c_actuator_driver* sclass;
DRIVE_SPEED   required_speed;
boolean   connected:1;
const boolean inverted:1;
} __ACTUATOR;

Wherever a structure/type such as this is declared then there is normally a macro, from me, to allow you to create one - these macros have some params to set specific variables or may use default values to initialise members that are of no interest to the caller.

For example:
Code: [Select]
// Define the standard constructor for an actuator
// Start with a speed of -128 so that the next setSpeed is a change - since -128 is not a valid value
#define MAKE_ACTUATOR_WITH_CLASS(class,inverted)  {class, -128 ,FALSE, inverted}

This is the same as: sclass='class'; required_speed = -128; connected=FALSE; inverted = 'inverted'


Equally given the following code in WebbotLib:-
Code: [Select]
typedef struct s_servo{
__ACTUATOR actuator; // has all the common stuff
struct s_servo_driver* driver; // The driver it is attached to, set by init'ing the driver
const IOPin * pin; // The IO pin it is connected to

// If you need to change these settings at runtime then use servoSetConfig
uint16_t center_us; // The number of microseconds pulse length to center the servo. On ideal servos it will be 1500. ie 1,5ms
uint16_t range_us; // The number of microseconds either side of center for full range. On ideal servos it be 500. Range = 1ms to 2ms.

// These are scratch variables used by the driver - DO NOT modify by hand
uint16_t min_ticks; // The no of timer ticks for minimum rotation
uint16_t max_ticks;  // The no of timer ticks for maximum rotation
uint16_t delay; // The current delay for the timer
uint16_t top; // The value of TOP for the timer it uses
const TimerCompare* channel; // The hardware PWM channel
} SERVO;

// Define the standard constructor for a servo connected to an IOPin
#define MAKE_SERVO(inverted, iopin, center, range)  { MAKE_ACTUATOR(inverted),null,iopin,center, range, 0,0,0,0,null }

Then the MAKE_SERVO macros takes some parameters and initialises the SERVO structure. Why all those zeroes etc? Well you will see that the { MAKE_ACTUATOR(inverted),null,iopin,center, range, 0,0,0,0,null } has exactly the same number of values as the SERVO structure requires - but calling the MAKE_SERVO only requires (inverted, iopin, center, range). Hence MAKE_SERVO is just a helper function that gives default values to member variables that it is pointless for the user to specify.

Hopefully makes sense - but if not then read a C coding primer. Either way: you don't need to understand in order to use it!
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 madmax_707Topic starter

  • Beginner
  • *
  • Posts: 5
  • Helpful? 0
Re: WebbotLib - question on some internals
« Reply #2 on: February 18, 2012, 06:13:57 PM »
Thanks for the quick response. It was indeed really of great help and things are now crystal clear.  Thanks once again.
Cheers!