Querying the motor controller on a handyboard

For my master's thesis I need a handyboard to control both a small robotic experiment and a computer that records data. There are many small glitches that I have run into and I will be posting them here so other users of the handyboard can use what I have learned.

I have written a small library to query the status of the motors and it can be downloaded below.

On/Off and Direction

There are many built in functions in interactive c to set the motor speed and direction; however, once it has been set there is no built in functionality to query the status of the motors. It is a relatively easy process to get the status by looking at the motor controller status byte 0x0e.

The status byte (8 bits) uses the top 4 bits to turn the motors on and off and the bottom 4 bits to set direction. So by looking at the byte: poke( 0x0e ) we can see what motors are set and which direction they are turning. By some simple bit shifting and masking you can easily determine the state of any given motor.

The following function is available in my library but is also put here for easy viewing.

/*************************************
 * get_motor_status( int motor )
 *
 * DESCRIPTION
 *  Queries the motor controller and returns the
 *  status of the motor in question
 *
 * PARAMETERS
 *  motor - The number of the motor port (0-3)
 *          that you want to find the status
 *          of
 *
 * RETURNS
 *   1 - Motor is on and going forward
 *   0 - Motor is off
 *  -1 - Motor is on and going backwards
 *
 */

int get_motor_status( int motor ) {

  /* Check to see if the motor is off */
  if( ((peek( 0x0e ) >> (4 + motor)) & 0x1) == 0 )
    return 0;

  /* Determine motor dircetion */
  if( ((peek( 0x0e ) >> motor) & 0x1) == 1 )
    return -1;
  else
    return 1;

} /* get_motor_status() */

Getting Motor Speed

Getting the motor speed requires using the data that controlles the Pulse Width Modulation (PWM) algorithms on the Handyboard. The speeds for motors 0 - 3 are, respectively, 0x22, 0x23, 0x24 and 0x25. By grabbing the value stored at that memory location you can get the speed the motor is set to.

The built in functions let you control the speed using a range of 0 - 100 (in both forward and backward directions) but a stock Handyboard only gives 7 graduations of control. The 7 speeds are denoted by the 7 following bit patterns:

  • 0b00000000
  • 0b00010001
  • 0b01001001
  • 0b01010101
  • 0b01010111
  • 0b01110111
  • 0b01111111
  • 0b11111111

The following code returns the value of the byte at the address for the motors speed:

/*************************************
 * get_motor_speed( int motor )
 *
 * DESCRIPTION
 *  Returns the speed of 'motor'
 *
 * PARAMTERS
 *  motor - The number of the motor port (0-3)
 *          that you want to find the status
 *          of
 *
 * RETURNS
 *  The speed of the given motor as a value
 *  between 0 and 255.
 *
 * ADDITIONAL NOTES
 *  The Handyboard by default has only 7 graduations
 *  of power control even though you can set the
 *  speed with a range 0 to 100.  The 7 speeds are
 *  represented by the following bit patterns.
 *
 *          0b00000000
 *          0b00010001
 *          0b01001001
 *          0b01010101
 *          0b01010111
 *          0b01110111
 *          0b01111111
 *          0b11111111
 *
 */
int get_motor_speed( int motor ) {
  return peek( 0x22 + motor );
} /* get_motor_speed() */

These two functions make up the library that I wrote for querying the motor controller. Feel free to use it, abuse it and give it to your friends.

Cheers,

Vince

AttachmentSize
motor_ext.c3.29 KB
Read more...


Reply

The content of this field is kept private and will not be shown publicly.