Library for using the TLC5940 as a servo controller.

Dependencies:   FastPWM

Dependents:   TLC5940ServoTest

Committer:
dudanian
Date:
Tue Oct 21 00:02:48 2014 +0000
Revision:
0:9fc434ff7a03
Child:
2:1d2251574d35
Created

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 0:9fc434ff7a03 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 0:9fc434ff7a03 17 *
dudanian 0:9fc434ff7a03 18 * Since the Servo period is 20ms (50Hz), we set this clock to 50*4096 = 204,800Hz according to the eqn above
dudanian 0:9fc434ff7a03 19 * Also, since this clock is so slow, there is no real 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 0:9fc434ff7a03 24 * This class controls a TLC5940 PWM driver to control Servo motors.
dudanian 0:9fc434ff7a03 25 * It supports sending grayscale PWM data calibrated for Servos, but it does not support error checking or writing the EEPROM.
dudanian 0:9fc434ff7a03 26 * This class uses the FastPWM library by Erik Olieman to continuously pulse the GSLCK pin without CPU intervention. After
dudanian 0:9fc434ff7a03 27 * 4096 pulses, the private member funciton reset is called by the ticker. It resets the display by pulsing the BLANK pin. If new
dudanian 0:9fc434ff7a03 28 * data has been set to be sent by the function setNewData, it is sent here.
dudanian 0:9fc434ff7a03 29 */
dudanian 0:9fc434ff7a03 30 class TLC5940Servo
dudanian 0:9fc434ff7a03 31 {
dudanian 0:9fc434ff7a03 32 public:
dudanian 0:9fc434ff7a03 33 /**
dudanian 0:9fc434ff7a03 34 * Set up the TLC5940
dudanian 0:9fc434ff7a03 35 *
dudanian 0:9fc434ff7a03 36 * @param MOSI - The MOSI pin of the SPI bus
dudanian 0:9fc434ff7a03 37 * @param SCLK - The SCK pin of the SPI bus
dudanian 0:9fc434ff7a03 38 * @param XLAT - The XLAT pin of the TLC5940(s)
dudanian 0:9fc434ff7a03 39 * @param BLANK - The BLANK pin of the TLC5940(s)
dudanian 0:9fc434ff7a03 40 * @param GSCLK - The GSCLK pin of the TLC5940(s)
dudanian 0:9fc434ff7a03 41 * @param number - The number of TLC5940s (optional)
dudanian 0:9fc434ff7a03 42 */
dudanian 0:9fc434ff7a03 43 TLC5940Servo(PinName MOSI, PinName SCLK, PinName XLAT, PinName BLANK,
dudanian 0:9fc434ff7a03 44 PinName GSCLK, const int number = 1);
dudanian 0:9fc434ff7a03 45
dudanian 0:9fc434ff7a03 46 // Destructor used to delete memory
dudanian 0:9fc434ff7a03 47 ~TLC5940Servo();
dudanian 0:9fc434ff7a03 48
dudanian 0:9fc434ff7a03 49 /** Allows calibration of the range and angles for all servos
dudanian 0:9fc434ff7a03 50 *
dudanian 0:9fc434ff7a03 51 * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds
dudanian 0:9fc434ff7a03 52 * @param degrees Angle from centre to maximum/minimum position in degrees
dudanian 0:9fc434ff7a03 53 */
dudanian 0:9fc434ff7a03 54 void calibrate(float range, float degrees);
dudanian 0:9fc434ff7a03 55
dudanian 0:9fc434ff7a03 56 /** Allows calibration of the range and angles for a particular servo
dudanian 0:9fc434ff7a03 57 *
dudanian 0:9fc434ff7a03 58 * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds
dudanian 0:9fc434ff7a03 59 * @param degrees Angle from centre to maximum/minimum position in degrees
dudanian 0:9fc434ff7a03 60 */
dudanian 0:9fc434ff7a03 61 void calibrate(int index, float range, float degrees);
dudanian 0:9fc434ff7a03 62
dudanian 0:9fc434ff7a03 63 /** Allows calibration of the range and angles for a particular servo
dudanian 0:9fc434ff7a03 64 *
dudanian 0:9fc434ff7a03 65 * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds
dudanian 0:9fc434ff7a03 66 * @param degrees Angle from centre to maximum/minimum position in degrees
dudanian 0:9fc434ff7a03 67 */
dudanian 0:9fc434ff7a03 68 void calibrate(float *range, float *degrees);
dudanian 0:9fc434ff7a03 69
dudanian 0:9fc434ff7a03 70 /**
dudanian 0:9fc434ff7a03 71 * Set the next chunk of grayscale data to be sent
dudanian 0:9fc434ff7a03 72 * @param data - Array of 16 bit shorts containing 16 12 bit grayscale data chunks per TLC5940
dudanian 0:9fc434ff7a03 73 * @note These must be in intervals of at least (1/GSCLK_SPEED) * 4096 to be sent
dudanian 0:9fc434ff7a03 74 */
dudanian 0:9fc434ff7a03 75 int& operator[](int index);
dudanian 0:9fc434ff7a03 76
dudanian 0:9fc434ff7a03 77 private:
dudanian 0:9fc434ff7a03 78 // SPI port - only MOSI and SCK are used
dudanian 0:9fc434ff7a03 79 SPI spi;
dudanian 0:9fc434ff7a03 80
dudanian 0:9fc434ff7a03 81 // PWM output using the FastPWM library by Erik Olieman
dudanian 0:9fc434ff7a03 82 FastPWM gsclk;
dudanian 0:9fc434ff7a03 83
dudanian 0:9fc434ff7a03 84 // Digital out pins used for the TLC5940
dudanian 0:9fc434ff7a03 85 DigitalOut blank;
dudanian 0:9fc434ff7a03 86 DigitalOut xlat;
dudanian 0:9fc434ff7a03 87
dudanian 0:9fc434ff7a03 88 // Number of TLC5940s in series
dudanian 0:9fc434ff7a03 89 const int number;
dudanian 0:9fc434ff7a03 90
dudanian 0:9fc434ff7a03 91 // Call a reset function to manage sending data and GSCLK updating
dudanian 0:9fc434ff7a03 92 Ticker reset_ticker;
dudanian 0:9fc434ff7a03 93
dudanian 0:9fc434ff7a03 94 // Has new data been loaded?
dudanian 0:9fc434ff7a03 95 volatile bool newData;
dudanian 0:9fc434ff7a03 96
dudanian 0:9fc434ff7a03 97 // Do we need to send an XLAT pulse? (Was GS data clocked in last reset?)
dudanian 0:9fc434ff7a03 98 volatile bool need_xlat;
dudanian 0:9fc434ff7a03 99
dudanian 0:9fc434ff7a03 100 // Buffers to store data until it is sent
dudanian 0:9fc434ff7a03 101 int* dataBuffer;
dudanian 0:9fc434ff7a03 102
dudanian 0:9fc434ff7a03 103 // Function to reset the display and send the next chunks of data
dudanian 0:9fc434ff7a03 104 void reset();
dudanian 0:9fc434ff7a03 105 };
dudanian 0:9fc434ff7a03 106
dudanian 0:9fc434ff7a03 107 #endif