Library for using the TLC5940 as a servo controller.

Dependencies:   FastPWM

Dependents:   TLC5940ServoTest

Committer:
dudanian
Date:
Tue Oct 21 06:06:30 2014 +0000
Revision:
3:3b04a122e508
Parent:
2:1d2251574d35
Child:
4:95305d4b0544
Typo in last commit.

Who changed what in which revision?

UserRevisionLine numberNew 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 2:1d2251574d35 40 void write(float percent);
dudanian 2:1d2251574d35 41 void calibrate(float range=0.0005, float degrees=45.0);
dudanian 2:1d2251574d35 42 float read();
dudanian 2:1d2251574d35 43 int pulsewidth();
dudanian 2:1d2251574d35 44 Servo& operator= (float percent);
dudanian 2:1d2251574d35 45 operator float();
dudanian 2:1d2251574d35 46 private:
dudanian 2:1d2251574d35 47 float _p;
dudanian 2:1d2251574d35 48 float _range;
dudanian 2:1d2251574d35 49 float _degrees;
dudanian 2:1d2251574d35 50 int _pw;
dudanian 2:1d2251574d35 51 };
dudanian 0:9fc434ff7a03 52 /**
dudanian 2:1d2251574d35 53 * Set up the TLC5940
dudanian 2:1d2251574d35 54 *
dudanian 2:1d2251574d35 55 * @param MOSI - The MOSI pin of the SPI bus
dudanian 2:1d2251574d35 56 * @param SCLK - The SCK pin of the SPI bus
dudanian 2:1d2251574d35 57 * @param XLAT - The XLAT pin of the TLC5940(s)
dudanian 2:1d2251574d35 58 * @param BLANK - The BLANK pin of the TLC5940(s)
dudanian 2:1d2251574d35 59 * @param GSCLK - The GSCLK pin of the TLC5940(s)
dudanian 2:1d2251574d35 60 * @param number - The number of TLC5940s (optional)
dudanian 0:9fc434ff7a03 61 */
dudanian 0:9fc434ff7a03 62 TLC5940Servo(PinName MOSI, PinName SCLK, PinName XLAT, PinName BLANK,
dudanian 2:1d2251574d35 63 PinName GSCLK, const int number = 1);
dudanian 2:1d2251574d35 64
dudanian 2:1d2251574d35 65 /**
dudanian 2:1d2251574d35 66 * Destructor used to delete memory
dudanian 2:1d2251574d35 67 */
dudanian 0:9fc434ff7a03 68 ~TLC5940Servo();
dudanian 0:9fc434ff7a03 69
dudanian 2:1d2251574d35 70 /**
dudanian 2:1d2251574d35 71 * Allows calibration of the range and angles for a particular servo
dudanian 0:9fc434ff7a03 72 *
dudanian 0:9fc434ff7a03 73 * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds
dudanian 0:9fc434ff7a03 74 * @param degrees Angle from centre to maximum/minimum position in degrees
dudanian 0:9fc434ff7a03 75 */
dudanian 0:9fc434ff7a03 76 void calibrate(int index, float range, float degrees);
dudanian 2:1d2251574d35 77
dudanian 2:1d2251574d35 78 /**
dudanian 2:1d2251574d35 79 * Allows calibration of the range and angles for all servos
dudanian 0:9fc434ff7a03 80 *
dudanian 0:9fc434ff7a03 81 * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds
dudanian 0:9fc434ff7a03 82 * @param degrees Angle from centre to maximum/minimum position in degrees
dudanian 0:9fc434ff7a03 83 */
dudanian 2:1d2251574d35 84 void calibrate(float range, float degrees);
dudanian 2:1d2251574d35 85
dudanian 0:9fc434ff7a03 86 /**
dudanian 2:1d2251574d35 87 * Array index operator for reading and writing from servos
dudanian 2:1d2251574d35 88 *
dudanian 2:1d2251574d35 89 * @param index Index of Servo in array
dudanian 0:9fc434ff7a03 90 */
dudanian 2:1d2251574d35 91 Servo& operator[](int index);
dudanian 0:9fc434ff7a03 92
dudanian 0:9fc434ff7a03 93 private:
dudanian 0:9fc434ff7a03 94 // SPI port - only MOSI and SCK are used
dudanian 0:9fc434ff7a03 95 SPI spi;
dudanian 0:9fc434ff7a03 96
dudanian 0:9fc434ff7a03 97 // PWM output using the FastPWM library by Erik Olieman
dudanian 0:9fc434ff7a03 98 FastPWM gsclk;
dudanian 0:9fc434ff7a03 99
dudanian 0:9fc434ff7a03 100 // Digital out pins used for the TLC5940
dudanian 0:9fc434ff7a03 101 DigitalOut blank;
dudanian 0:9fc434ff7a03 102 DigitalOut xlat;
dudanian 0:9fc434ff7a03 103
dudanian 0:9fc434ff7a03 104 // Number of TLC5940s in series
dudanian 0:9fc434ff7a03 105 const int number;
dudanian 0:9fc434ff7a03 106
dudanian 0:9fc434ff7a03 107 // Call a reset function to manage sending data and GSCLK updating
dudanian 0:9fc434ff7a03 108 Ticker reset_ticker;
dudanian 0:9fc434ff7a03 109
dudanian 0:9fc434ff7a03 110 // Has new data been loaded?
dudanian 0:9fc434ff7a03 111 volatile bool newData;
dudanian 0:9fc434ff7a03 112
dudanian 0:9fc434ff7a03 113 // Do we need to send an XLAT pulse? (Was GS data clocked in last reset?)
dudanian 0:9fc434ff7a03 114 volatile bool need_xlat;
dudanian 0:9fc434ff7a03 115
dudanian 0:9fc434ff7a03 116 // Buffers to store data until it is sent
dudanian 3:3b04a122e508 117 Servo* servos;
dudanian 0:9fc434ff7a03 118
dudanian 0:9fc434ff7a03 119 // Function to reset the display and send the next chunks of data
dudanian 0:9fc434ff7a03 120 void reset();
dudanian 0:9fc434ff7a03 121 };
dudanian 0:9fc434ff7a03 122
dudanian 0:9fc434ff7a03 123 #endif