Library for Pololu Maestro Servo Controller http://www.pololu.com/docs/0J40

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Maestro.h Source File

Maestro.h

00001 /**
00002  * @author Przemyslaw Kochanski <przemyslaw@kochanski.biz>
00003  *
00004  * @Section LICENSE
00005  *
00006  * Copyright (C) 2014 Przemyslaw Kochanski, MIT License
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00009  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00010  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00011  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00012  * furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included in all copies or
00015  * substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00018  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00019  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00020  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022  *
00023  * @section DESCRIPTION
00024  *
00025  * Library for Pololu Maestro Servo Controller
00026  * Serial Servo Commands: http://www.pololu.com/docs/0J40/5.e
00027  */
00028 
00029 #ifndef MAESTRO
00030 #define MAESTRO
00031 
00032 #include "mbed.h"
00033 
00034 /**
00035  * Compact Protocol Command Bytes
00036  */
00037 #define SET_TARGET 0x84
00038 #define SET_MULTIPLE_TARGETS 0x9F
00039 #define SET_SPEED 0x87
00040 #define SET_ACCELERATION 0x89
00041 #define SET_PWM 0x8A
00042 #define GET_POSITION 0x90
00043 #define GET_MOVING_STATE 0x93
00044 #define GET_ERRORS 0xA1
00045 #define GO_HOME 0xA2
00046 #define BAUD_RATE_IDICATION 0xAA
00047 
00048 /**
00049  * Errors bits numbers
00050  */
00051 #define SERIAL_SIGNAL_ERROR 0
00052 #define SERIAL_OVERRUN_ERROR 1
00053 #define SERIAL_RX_BUFFER_FULL_ERROR 2
00054 #define SERIAL_CRC_ERROR 3
00055 #define SERIAL_PROTOCOL_ERROR 4
00056 #define SERIAL_TIMEOUT_ERROR 5
00057 #define SCRIPT_STACK_ERROR 6
00058 #define SCRIPT_CALL_STACK_ERROR 7
00059 #define SCRIPT_PROGRAM_COUNTER_ERROR 8
00060 
00061 /**
00062  * Pololu Maestro Servo Controller
00063  */
00064 class Maestro
00065 {
00066 
00067 public:
00068 
00069     /**
00070      * Constructor
00071      *
00072      * @param tx - mbed pin to use for TX serial line
00073      * @param rx - mbed pin to use for RX serial line
00074      */
00075     Maestro(PinName tx, PinName rx);
00076 
00077     /**
00078      * Sets baud rate
00079      *
00080      * @param baud - Baud Rate to be set
00081      */
00082     void setBaudRate(uint16_t baud);
00083 
00084     /**
00085      * Sets the target of a channel to a specified value
00086      *
00087      * @param channel - number a servo channel
00088      * @param target - the pulse width to transmit in units of quarter-microseconds
00089      */
00090     void setTarget(uint8_t channel, uint16_t target);
00091 
00092     /**
00093      * Sets specified channel's servo to a specified angle
00094      *
00095      * @param channel - number a servo channel
00096      * @param angle - target angle of a servo
00097      */
00098     void setServoAngle(uint8_t channel, int8_t angle);
00099 
00100     /**
00101      * Simultaneously sets the targets for a contiguous block of channels
00102      *
00103      * @param count - number of channels in the contiguous block
00104      * @param firstChannel - lowest channel number in the contiguous block
00105      * @param target - target values (the pulse width to transmit in units of
00106      *  quarter-microseconds) for each of the channels, in order by channel number
00107      */
00108     void setMultipleTargets(uint8_t firstChannel, uint8_t count, uint16_t* targets);
00109 
00110     /**
00111      * Simultaneously sets specified contiguous block of servo channels to a specified angles
00112      *
00113      * @param count - number of servo channels in the contiguous block
00114      * @param firstChannel - lowest servo channel number in the contiguous block
00115      * @param angles - target values (the pulse width to transmit in units of
00116      *  quarter-microseconds) for each of the servo channels, in order by channel number
00117      */
00118     void setServosAngles(uint8_t firstChannel, uint8_t count, int8_t* angles);
00119 
00120     /**
00121      * Limits the speed at which a servo channel's output value changes
00122      *
00123      * @param channel - number of a servo channel
00124      * @param speed - speed of the servo in units of 0.25 us / (10 ms)
00125      */
00126     void setSpeed(uint8_t channel, uint16_t speed);
00127     
00128     /**
00129      * Limits the speed at which all servos channels output value changes
00130      *
00131      * @param speed - speed of the servo in units of 0.25 us / (10 ms)
00132      */
00133     void setSpeed(uint16_t speed);
00134 
00135     /**
00136      * Limits the acceleration of a servo channel's output
00137      *
00138      * @param channel - number of a servo channel
00139      * @param acceleration - acceleration of the servo in units of (0.25 us) / (10 ms) / (80 ms)
00140      */
00141     void setAcceleration(uint8_t channel, uint16_t acceleration);
00142     
00143     /**
00144      * Limits the acceleration of all servos channels output
00145      *
00146      * @param acceleration - acceleration of the servo in units of (0.25 us) / (10 ms) / (80 ms)
00147      */
00148     void setAcceleration(uint16_t acceleration);
00149 
00150     /**
00151      * Sets the PWM output to the specified on time and period
00152      *
00153      * @param channel - number of a servo channel
00154      * @param time - time in units of 1/48 us
00155      * @param period - period in units of 1/48 us
00156      */
00157     void setPWM(uint8_t channel, uint16_t time, uint16_t period);
00158 
00159     /**
00160      * Gets current servo position
00161      *
00162      * @param channel - number of a servo channel
00163      *
00164      * @return - current pulse width that the Maestro is transmitting on the channel
00165      */
00166     uint16_t getPosition(uint8_t channel);
00167 
00168     /**
00169      * Determine whether the servo outputs have reached their targets or are still changing
00170      *
00171      * @return - true if servos are moving, false otherwise
00172      */
00173     bool getMovingState();
00174 
00175     /**
00176      * Examine the errors that the Maestro has detected
00177      *
00178      * @return - error bits
00179      */
00180     uint16_t getErrors();
00181 
00182     /**
00183      * Send all servos and outputs to their home positions
00184      */
00185     void goHome();
00186 
00187 private:
00188 
00189     Serial serial;
00190 };
00191 
00192 #endif // Maestro