Library for using the TLC5940 as a servo controller.
TLC5940Servo.h@4:95305d4b0544, 2014-10-21 (annotated)
- Committer:
- dudanian
- Date:
- Tue Oct 21 06:14:58 2014 +0000
- Revision:
- 4:95305d4b0544
- Parent:
- 3:3b04a122e508
- Child:
- 5:56daa8c0697d
Testing inner class docs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dudanian | 0:9fc434ff7a03 | 1 | #ifndef TLC5940Servo_H |
dudanian | 0:9fc434ff7a03 | 2 | #define TLC5940Servo_H |
dudanian | 0:9fc434ff7a03 | 3 | |
dudanian | 0:9fc434ff7a03 | 4 | #include "FastPWM.h" |
dudanian | 2:1d2251574d35 | 5 | |
dudanian | 0:9fc434ff7a03 | 6 | /** |
dudanian | 0:9fc434ff7a03 | 7 | * SPI speed used by the mbed to communicate with the TLC5940 |
dudanian | 0:9fc434ff7a03 | 8 | * The TLC5940 supports up to 30Mhz. This should be kept as high |
dudanian | 0:9fc434ff7a03 | 9 | * as possible to ensure that data has time to be sent each reset cycle. |
dudanian | 0:9fc434ff7a03 | 10 | */ |
dudanian | 0:9fc434ff7a03 | 11 | #define SPI_SPEED 30000000 |
dudanian | 0:9fc434ff7a03 | 12 | |
dudanian | 0:9fc434ff7a03 | 13 | /** |
dudanian | 0:9fc434ff7a03 | 14 | * The rate at which the GSCLK pin is pulsed |
dudanian | 0:9fc434ff7a03 | 15 | * This also controls how often the reset function is called |
dudanian | 0:9fc434ff7a03 | 16 | * The rate at which the reset function is called can be calculated by: (1/GSCLK_SPEED) * 4096 |
dudanian | 2:1d2251574d35 | 17 | * |
dudanian | 2:1d2251574d35 | 18 | * Since the Servo period is 20ms (50Hz), we set this clock to 50*4096 = 204,800Hz according to the equation above |
dudanian | 2:1d2251574d35 | 19 | * Also, since this clock is so slow, there is almost no limit to the number of TLC5940s you can chain |
dudanian | 0:9fc434ff7a03 | 20 | */ |
dudanian | 0:9fc434ff7a03 | 21 | #define GSCLK_SPEED 204800 |
dudanian | 0:9fc434ff7a03 | 22 | |
dudanian | 0:9fc434ff7a03 | 23 | /** |
dudanian | 2:1d2251574d35 | 24 | * This class controls a TLC5940 PWM driver to control Servo motors. It supports sending grayscale PWM data calibrated for Servos, |
dudanian | 2:1d2251574d35 | 25 | * but it does not support error checking or writing the EEPROM. After 4096 pulses, the private member funciton reset is called by |
dudanian | 2:1d2251574d35 | 26 | * the ticker. It resets the driver by pulsing the BLANK pin. If new data has been set to be sent by the function setNewData, it |
dudanian | 2:1d2251574d35 | 27 | * is sent here. |
dudanian | 2:1d2251574d35 | 28 | * |
dudanian | 2:1d2251574d35 | 29 | * This class is a heavily modified versoin of the TLC5940 library created by Spencer Davis. This class also uses the FastPWM |
dudanian | 2:1d2251574d35 | 30 | * library by Erik Olieman to continuously pulse the GSLCK pin without CPU intervention. |
dudanian | 0:9fc434ff7a03 | 31 | */ |
dudanian | 2:1d2251574d35 | 32 | class TLC5940Servo { |
dudanian | 0:9fc434ff7a03 | 33 | public: |
dudanian | 2:1d2251574d35 | 34 | /* |
dudanian | 2:1d2251574d35 | 35 | * Servo inner class to abstract some of the details. Based off of the mbed official Servo class |
dudanian | 2:1d2251574d35 | 36 | */ |
dudanian | 2:1d2251574d35 | 37 | class Servo { |
dudanian | 2:1d2251574d35 | 38 | public: |
dudanian | 2:1d2251574d35 | 39 | Servo(); |
dudanian | 4:95305d4b0544 | 40 | /** |
dudanian | 4:95305d4b0544 | 41 | * Set the servo position, normalised to it's full range |
dudanian | 4:95305d4b0544 | 42 | * |
dudanian | 4:95305d4b0544 | 43 | * @param percent A normalised number 0.0-1.0 to represent the full range. |
dudanian | 4:95305d4b0544 | 44 | */ |
dudanian | 2:1d2251574d35 | 45 | void write(float percent); |
dudanian | 2:1d2251574d35 | 46 | void calibrate(float range=0.0005, float degrees=45.0); |
dudanian | 2:1d2251574d35 | 47 | float read(); |
dudanian | 2:1d2251574d35 | 48 | int pulsewidth(); |
dudanian | 2:1d2251574d35 | 49 | Servo& operator= (float percent); |
dudanian | 2:1d2251574d35 | 50 | operator float(); |
dudanian | 2:1d2251574d35 | 51 | private: |
dudanian | 2:1d2251574d35 | 52 | float _p; |
dudanian | 2:1d2251574d35 | 53 | float _range; |
dudanian | 2:1d2251574d35 | 54 | float _degrees; |
dudanian | 2:1d2251574d35 | 55 | int _pw; |
dudanian | 2:1d2251574d35 | 56 | }; |
dudanian | 0:9fc434ff7a03 | 57 | /** |
dudanian | 2:1d2251574d35 | 58 | * Set up the TLC5940 |
dudanian | 2:1d2251574d35 | 59 | * |
dudanian | 2:1d2251574d35 | 60 | * @param MOSI - The MOSI pin of the SPI bus |
dudanian | 2:1d2251574d35 | 61 | * @param SCLK - The SCK pin of the SPI bus |
dudanian | 2:1d2251574d35 | 62 | * @param XLAT - The XLAT pin of the TLC5940(s) |
dudanian | 2:1d2251574d35 | 63 | * @param BLANK - The BLANK pin of the TLC5940(s) |
dudanian | 2:1d2251574d35 | 64 | * @param GSCLK - The GSCLK pin of the TLC5940(s) |
dudanian | 2:1d2251574d35 | 65 | * @param number - The number of TLC5940s (optional) |
dudanian | 0:9fc434ff7a03 | 66 | */ |
dudanian | 0:9fc434ff7a03 | 67 | TLC5940Servo(PinName MOSI, PinName SCLK, PinName XLAT, PinName BLANK, |
dudanian | 2:1d2251574d35 | 68 | PinName GSCLK, const int number = 1); |
dudanian | 2:1d2251574d35 | 69 | |
dudanian | 2:1d2251574d35 | 70 | /** |
dudanian | 2:1d2251574d35 | 71 | * Destructor used to delete memory |
dudanian | 2:1d2251574d35 | 72 | */ |
dudanian | 0:9fc434ff7a03 | 73 | ~TLC5940Servo(); |
dudanian | 0:9fc434ff7a03 | 74 | |
dudanian | 2:1d2251574d35 | 75 | /** |
dudanian | 2:1d2251574d35 | 76 | * Allows calibration of the range and angles for a particular servo |
dudanian | 0:9fc434ff7a03 | 77 | * |
dudanian | 0:9fc434ff7a03 | 78 | * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds |
dudanian | 0:9fc434ff7a03 | 79 | * @param degrees Angle from centre to maximum/minimum position in degrees |
dudanian | 0:9fc434ff7a03 | 80 | */ |
dudanian | 0:9fc434ff7a03 | 81 | void calibrate(int index, float range, float degrees); |
dudanian | 2:1d2251574d35 | 82 | |
dudanian | 2:1d2251574d35 | 83 | /** |
dudanian | 2:1d2251574d35 | 84 | * Allows calibration of the range and angles for all servos |
dudanian | 0:9fc434ff7a03 | 85 | * |
dudanian | 0:9fc434ff7a03 | 86 | * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds |
dudanian | 0:9fc434ff7a03 | 87 | * @param degrees Angle from centre to maximum/minimum position in degrees |
dudanian | 0:9fc434ff7a03 | 88 | */ |
dudanian | 2:1d2251574d35 | 89 | void calibrate(float range, float degrees); |
dudanian | 2:1d2251574d35 | 90 | |
dudanian | 0:9fc434ff7a03 | 91 | /** |
dudanian | 2:1d2251574d35 | 92 | * Array index operator for reading and writing from servos |
dudanian | 2:1d2251574d35 | 93 | * |
dudanian | 2:1d2251574d35 | 94 | * @param index Index of Servo in array |
dudanian | 0:9fc434ff7a03 | 95 | */ |
dudanian | 2:1d2251574d35 | 96 | Servo& operator[](int index); |
dudanian | 0:9fc434ff7a03 | 97 | |
dudanian | 0:9fc434ff7a03 | 98 | private: |
dudanian | 0:9fc434ff7a03 | 99 | // SPI port - only MOSI and SCK are used |
dudanian | 0:9fc434ff7a03 | 100 | SPI spi; |
dudanian | 0:9fc434ff7a03 | 101 | |
dudanian | 0:9fc434ff7a03 | 102 | // PWM output using the FastPWM library by Erik Olieman |
dudanian | 0:9fc434ff7a03 | 103 | FastPWM gsclk; |
dudanian | 0:9fc434ff7a03 | 104 | |
dudanian | 0:9fc434ff7a03 | 105 | // Digital out pins used for the TLC5940 |
dudanian | 0:9fc434ff7a03 | 106 | DigitalOut blank; |
dudanian | 0:9fc434ff7a03 | 107 | DigitalOut xlat; |
dudanian | 0:9fc434ff7a03 | 108 | |
dudanian | 0:9fc434ff7a03 | 109 | // Number of TLC5940s in series |
dudanian | 0:9fc434ff7a03 | 110 | const int number; |
dudanian | 0:9fc434ff7a03 | 111 | |
dudanian | 0:9fc434ff7a03 | 112 | // Call a reset function to manage sending data and GSCLK updating |
dudanian | 0:9fc434ff7a03 | 113 | Ticker reset_ticker; |
dudanian | 0:9fc434ff7a03 | 114 | |
dudanian | 0:9fc434ff7a03 | 115 | // Has new data been loaded? |
dudanian | 0:9fc434ff7a03 | 116 | volatile bool newData; |
dudanian | 0:9fc434ff7a03 | 117 | |
dudanian | 0:9fc434ff7a03 | 118 | // Do we need to send an XLAT pulse? (Was GS data clocked in last reset?) |
dudanian | 0:9fc434ff7a03 | 119 | volatile bool need_xlat; |
dudanian | 0:9fc434ff7a03 | 120 | |
dudanian | 0:9fc434ff7a03 | 121 | // Buffers to store data until it is sent |
dudanian | 3:3b04a122e508 | 122 | Servo* servos; |
dudanian | 0:9fc434ff7a03 | 123 | |
dudanian | 0:9fc434ff7a03 | 124 | // Function to reset the display and send the next chunks of data |
dudanian | 0:9fc434ff7a03 | 125 | void reset(); |
dudanian | 0:9fc434ff7a03 | 126 | }; |
dudanian | 0:9fc434ff7a03 | 127 | |
dudanian | 0:9fc434ff7a03 | 128 | #endif |