Modified the original DRV2605 library written by Bryce Williams. Team specific uint8_t kick_start_my_heart(int intensity, int duration) function is added to the library, otherwise unchanged. FLY ARMY!!!
Fork of DRV2605 by
- include "mbed.h"
- include "DRV2605.h" DRV2605 haptics(p9, p10); *NEEDE FOR KCIKSTART*
PwmOut base(p26); *NEEDED FOR BOOSTER CIRCUIT, if required*
int main() { base.period(.000001);1MHz base=.5;50% duty cycle
*FOLLOWING NEEDED FOR KICKSTART*
haptics.init(3.3); Motor initialization will cause a 1 second vibration
wait(2);
uint8_t stats=haptics.kick_start_my_heart(10, 1); Intensity range 1-10, duratio range 1-8
printf("dig %X/n",haptics.init(3.3)); }
DRV2605.h@0:3b2b4f34aaca, 2015-10-21 (annotated)
- Committer:
- electromotivated
- Date:
- Wed Oct 21 01:02:59 2015 +0000
- Revision:
- 0:3b2b4f34aaca
- Child:
- 1:224404f39d98
v1 Upload;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
electromotivated | 0:3b2b4f34aaca | 1 | /* |
electromotivated | 0:3b2b4f34aaca | 2 | Bryce Williams 10/16/2015 |
electromotivated | 0:3b2b4f34aaca | 3 | |
electromotivated | 0:3b2b4f34aaca | 4 | Library for the TI DRV2605L 2 to 5.2 V Haptic Driver for LRA and ERM |
electromotivated | 0:3b2b4f34aaca | 5 | With Effect Library and Smart-Loop Architecture |
electromotivated | 0:3b2b4f34aaca | 6 | |
electromotivated | 0:3b2b4f34aaca | 7 | References: |
electromotivated | 0:3b2b4f34aaca | 8 | http://www.ti.com/product/DRV2605L/description&lpos=Middle_Container&lid=Alternative_Devices |
electromotivated | 0:3b2b4f34aaca | 9 | http://www.ti.com/lit/ds/symlink/drv2605l.pdf (Datasheet) |
electromotivated | 0:3b2b4f34aaca | 10 | http://www.ti.com/lit/an/sloa189/sloa189.pdf (Setup Guide; SLOA189) |
electromotivated | 0:3b2b4f34aaca | 11 | */ |
electromotivated | 0:3b2b4f34aaca | 12 | |
electromotivated | 0:3b2b4f34aaca | 13 | #ifndef DRV2605_H |
electromotivated | 0:3b2b4f34aaca | 14 | #define DRV2605_H |
electromotivated | 0:3b2b4f34aaca | 15 | |
electromotivated | 0:3b2b4f34aaca | 16 | #include "mbed.h" |
electromotivated | 0:3b2b4f34aaca | 17 | |
electromotivated | 0:3b2b4f34aaca | 18 | /****************************************************************************** |
electromotivated | 0:3b2b4f34aaca | 19 | ***** DRV2605 Addresses |
electromotivated | 0:3b2b4f34aaca | 20 | ******************************************************************************/ |
electromotivated | 0:3b2b4f34aaca | 21 | #define SLAVE_ADDR_7_BIT 0x5A // 7-bit slave address |
electromotivated | 0:3b2b4f34aaca | 22 | |
electromotivated | 0:3b2b4f34aaca | 23 | /****************************************************************************** |
electromotivated | 0:3b2b4f34aaca | 24 | ****** DRV2605 REGISTERS ****************************************************** |
electromotivated | 0:3b2b4f34aaca | 25 | ******************************************************************************/ |
electromotivated | 0:3b2b4f34aaca | 26 | #define STATUS 0x00 |
electromotivated | 0:3b2b4f34aaca | 27 | #define MODE 0x01 |
electromotivated | 0:3b2b4f34aaca | 28 | #define REAL_TIME_PLAYBACK 0x02 |
electromotivated | 0:3b2b4f34aaca | 29 | #define LIBRARY_SELECTION 0x03 |
electromotivated | 0:3b2b4f34aaca | 30 | #define WAVEFORM_SEQUENCER_1 0x04 |
electromotivated | 0:3b2b4f34aaca | 31 | #define WAVEFORM_SEQUENCER_2 0x05 |
electromotivated | 0:3b2b4f34aaca | 32 | #define WAVEFORM_SEQUENCER_3 0x06 |
electromotivated | 0:3b2b4f34aaca | 33 | #define WAVEFORM_SEQUENCER_4 0x07 |
electromotivated | 0:3b2b4f34aaca | 34 | #define WAVEFORM_SEQUENCER_5 0x08 |
electromotivated | 0:3b2b4f34aaca | 35 | #define WAVEFORM_SEQUENCER_6 0x09 |
electromotivated | 0:3b2b4f34aaca | 36 | #define WAVEFORM_SEQUENCER_7 0x0A |
electromotivated | 0:3b2b4f34aaca | 37 | #define WAVEFORM_SEQUENCER_8 0x0B |
electromotivated | 0:3b2b4f34aaca | 38 | #define GO 0x0C |
electromotivated | 0:3b2b4f34aaca | 39 | #define OVERDRIVE_TIME_OFFSET 0x0D |
electromotivated | 0:3b2b4f34aaca | 40 | #define POSITIVE_SUSTAIN_TIME_OFFSET 0x0E |
electromotivated | 0:3b2b4f34aaca | 41 | #define NEGATIVE_SUSTAIN_TIME_OFFSET 0x0F |
electromotivated | 0:3b2b4f34aaca | 42 | #define BRAKE_TIME_OFFSET 0x10 |
electromotivated | 0:3b2b4f34aaca | 43 | #define AUDIO_TO_VIBE_CONTROL 0x11 |
electromotivated | 0:3b2b4f34aaca | 44 | #define AUDIO_TO_VIBE_MINIMUM_INPUT_LEVEL 0x12 |
electromotivated | 0:3b2b4f34aaca | 45 | #define AUDIO_TO_VIBE_MAXIMUM_INPUT_LEVEL 0x13 |
electromotivated | 0:3b2b4f34aaca | 46 | #define AUDIO_TO_VIBE_MINIMUM_OUPUT_DRIVE 0x14 |
electromotivated | 0:3b2b4f34aaca | 47 | #define AUDIO_TO_VIBE_MAXIMUM_OUTPUT_DRIVE 0x15 |
electromotivated | 0:3b2b4f34aaca | 48 | #define RATED_VOLTAGE 0x16 |
electromotivated | 0:3b2b4f34aaca | 49 | #define OVERDRIVE_CLAMP_VOLTAGE 0x17 |
electromotivated | 0:3b2b4f34aaca | 50 | #define AUTO_CALIBRATION_COMPENSATION_RESULT 0x18 |
electromotivated | 0:3b2b4f34aaca | 51 | #define AUTO_CALIBRATION_BACK_EMF_RESULT 0x19 |
electromotivated | 0:3b2b4f34aaca | 52 | #define FEEDBACK_CONTROL 0x1A |
electromotivated | 0:3b2b4f34aaca | 53 | #define CONTROL 0x1B |
electromotivated | 0:3b2b4f34aaca | 54 | #define CONTROL2 0x1C |
electromotivated | 0:3b2b4f34aaca | 55 | #define CONTROL3 0x1D |
electromotivated | 0:3b2b4f34aaca | 56 | #define CONTROL4 0x1E |
electromotivated | 0:3b2b4f34aaca | 57 | #define CONTROL5 0x1F |
electromotivated | 0:3b2b4f34aaca | 58 | #define LRA_OPEN_LOOP_PERIOD 0x20 |
electromotivated | 0:3b2b4f34aaca | 59 | #define VBAT_VOLTAGE_MONITOR 0x21 |
electromotivated | 0:3b2b4f34aaca | 60 | #define LRA_RESONANCE_PERIOD 0x22 |
electromotivated | 0:3b2b4f34aaca | 61 | |
electromotivated | 0:3b2b4f34aaca | 62 | class DRV2605{ |
electromotivated | 0:3b2b4f34aaca | 63 | |
electromotivated | 0:3b2b4f34aaca | 64 | public: |
electromotivated | 0:3b2b4f34aaca | 65 | //// modes defines the possible modes of the DRV2605L |
electromotivated | 0:3b2b4f34aaca | 66 | enum Mode{ |
electromotivated | 0:3b2b4f34aaca | 67 | INTERNAL_TRIG, // 0x00: Waveforms fired by Setting GO bit in Register 0x0C |
electromotivated | 0:3b2b4f34aaca | 68 | EXTERNAL_EDGE, // 0x01: Rising Edge on IN/TRIG pin set GO Bit. |
electromotivated | 0:3b2b4f34aaca | 69 | EXTERNAL_LEVEL, // 0x02: GO bit follows state of edge on IN/TRIG pin. |
electromotivated | 0:3b2b4f34aaca | 70 | PWM_ANALOG, // 0x03: PWM or Analog Signal accepted at IN/TRIG pin. |
electromotivated | 0:3b2b4f34aaca | 71 | AUDIO_TO_VIBE, // 0x04: An AC-coupled audio signal is accepted at the IN/TRIG pin. |
electromotivated | 0:3b2b4f34aaca | 72 | RTP, // 0x05: Real- Time Playback |
electromotivated | 0:3b2b4f34aaca | 73 | DIAG, // 0x06: Set to perform actuator diagnostics |
electromotivated | 0:3b2b4f34aaca | 74 | AUTO_CAL, // 0x07: Set to perform auto calibration of device for actuator |
electromotivated | 0:3b2b4f34aaca | 75 | STANDBY = 0x40, // 0x40: Set Device to Software Standby (Low- Power Mode) |
electromotivated | 0:3b2b4f34aaca | 76 | RESET = 0x80, // 0x80: Reset Device (equivalent of power cycling the device) |
electromotivated | 0:3b2b4f34aaca | 77 | }; |
electromotivated | 0:3b2b4f34aaca | 78 | |
electromotivated | 0:3b2b4f34aaca | 79 | //// FeedBack_Controls Fields Bitmasks (Register Addr: 0x1A) |
electromotivated | 0:3b2b4f34aaca | 80 | enum Actuator_Type{ERM = 0, LRA = 0x80}; // bit-7 |
electromotivated | 0:3b2b4f34aaca | 81 | enum Brake_Factor{x1 = 0x00, x2 = 0x10, x3 = 0x20, |
electromotivated | 0:3b2b4f34aaca | 82 | x4 = 0x40, x6 = 0x80, x8 = 0x50, |
electromotivated | 0:3b2b4f34aaca | 83 | x16 = 0x60, DISABLE = 0x70}; // bit-6..4 |
electromotivated | 0:3b2b4f34aaca | 84 | enum Loop_Gain{LOW = 0x00, |
electromotivated | 0:3b2b4f34aaca | 85 | MED = 0x04, |
electromotivated | 0:3b2b4f34aaca | 86 | HIGH = 0x08, |
electromotivated | 0:3b2b4f34aaca | 87 | VERY_HIGH = 0x0C}; // bit-3..2 |
electromotivated | 0:3b2b4f34aaca | 88 | |
electromotivated | 0:3b2b4f34aaca | 89 | enum Library{EMPTY, A, B, C, D, E, |
electromotivated | 0:3b2b4f34aaca | 90 | LRA_LIB, F}; // ROM Waveform Library Selections |
electromotivated | 0:3b2b4f34aaca | 91 | |
electromotivated | 0:3b2b4f34aaca | 92 | /** |
electromotivated | 0:3b2b4f34aaca | 93 | Constructor for DRV2605 Objects |
electromotivated | 0:3b2b4f34aaca | 94 | */ |
electromotivated | 0:3b2b4f34aaca | 95 | DRV2605(PinName sda, PinName scl); |
electromotivated | 0:3b2b4f34aaca | 96 | |
electromotivated | 0:3b2b4f34aaca | 97 | /** |
electromotivated | 0:3b2b4f34aaca | 98 | Write value to specified register of device |
electromotivated | 0:3b2b4f34aaca | 99 | @param reg The device register to write |
electromotivated | 0:3b2b4f34aaca | 100 | @param value The value to write to the register |
electromotivated | 0:3b2b4f34aaca | 101 | */ |
electromotivated | 0:3b2b4f34aaca | 102 | void i2cWriteByte(char reg, char value); |
electromotivated | 0:3b2b4f34aaca | 103 | |
electromotivated | 0:3b2b4f34aaca | 104 | /** |
electromotivated | 0:3b2b4f34aaca | 105 | Read value from register of device |
electromotivated | 0:3b2b4f34aaca | 106 | @param reg The device register to read |
electromotivated | 0:3b2b4f34aaca | 107 | @return The result |
electromotivated | 0:3b2b4f34aaca | 108 | */ |
electromotivated | 0:3b2b4f34aaca | 109 | uint8_t i2cReadByte(char reg); |
electromotivated | 0:3b2b4f34aaca | 110 | |
electromotivated | 0:3b2b4f34aaca | 111 | /** |
electromotivated | 0:3b2b4f34aaca | 112 | Place device into specified mode |
electromotivated | 0:3b2b4f34aaca | 113 | @param mode The mode to place device into |
electromotivated | 0:3b2b4f34aaca | 114 | */ |
electromotivated | 0:3b2b4f34aaca | 115 | void mode(Mode mode); |
electromotivated | 0:3b2b4f34aaca | 116 | |
electromotivated | 0:3b2b4f34aaca | 117 | /** |
electromotivated | 0:3b2b4f34aaca | 118 | TODO: Expand to allow initialization for LRAs and Closed Loop operation |
electromotivated | 0:3b2b4f34aaca | 119 | Initialize the device for Open- Loop ERM mode using specified ROM |
electromotivated | 0:3b2b4f34aaca | 120 | Waveform Library as specified in Section 9.3 of Device Datasheet. |
electromotivated | 0:3b2b4f34aaca | 121 | See also Device Setup Guide 1.6.1 ERM Initialization Example |
electromotivated | 0:3b2b4f34aaca | 122 | @param actuator_peak_voltage The Peak Voltage Rating of Actuator |
electromotivated | 0:3b2b4f34aaca | 123 | @param lib The ROM Waveform Library to use |
electromotivated | 0:3b2b4f34aaca | 124 | */ |
electromotivated | 0:3b2b4f34aaca | 125 | int init(float actuator_peak_voltage, Library lib = B); |
electromotivated | 0:3b2b4f34aaca | 126 | |
electromotivated | 0:3b2b4f34aaca | 127 | /** |
electromotivated | 0:3b2b4f34aaca | 128 | Runs diagnostics on the Actuator and Device and returns the results. |
electromotivated | 0:3b2b4f34aaca | 129 | The results indicate if an actuator is detected, over- current events, |
electromotivated | 0:3b2b4f34aaca | 130 | etc. Refer to STATUS Register (0x00) in device datasheet for more |
electromotivated | 0:3b2b4f34aaca | 131 | description register values. |
electromotivated | 0:3b2b4f34aaca | 132 | |
electromotivated | 0:3b2b4f34aaca | 133 | Note: This should be run if the user is having trouble getting the actuator |
electromotivated | 0:3b2b4f34aaca | 134 | to work. |
electromotivated | 0:3b2b4f34aaca | 135 | |
electromotivated | 0:3b2b4f34aaca | 136 | @return The results of the diagnostics (i.e. Status Reg (0x00)) |
electromotivated | 0:3b2b4f34aaca | 137 | */ |
electromotivated | 0:3b2b4f34aaca | 138 | uint8_t diagnostics(); |
electromotivated | 0:3b2b4f34aaca | 139 | |
electromotivated | 0:3b2b4f34aaca | 140 | /** |
electromotivated | 0:3b2b4f34aaca | 141 | Play single waveform from ROM Library as outlined in Section 9.3.2.1 |
electromotivated | 0:3b2b4f34aaca | 142 | of Device Datasheet. |
electromotivated | 0:3b2b4f34aaca | 143 | The library used is the one that is currently written |
electromotivated | 0:3b2b4f34aaca | 144 | to the Library_Selection Register (0x03). This library |
electromotivated | 0:3b2b4f34aaca | 145 | is set in the init(Library lib) method, but can be |
electromotivated | 0:3b2b4f34aaca | 146 | changed manually. |
electromotivated | 0:3b2b4f34aaca | 147 | @param waveform_effect The Waveform Effect Library Index value to play |
electromotivated | 0:3b2b4f34aaca | 148 | (valid values are 1 to 123) |
electromotivated | 0:3b2b4f34aaca | 149 | */ |
electromotivated | 0:3b2b4f34aaca | 150 | void play_waveform(int waveform_effect); |
electromotivated | 0:3b2b4f34aaca | 151 | |
electromotivated | 0:3b2b4f34aaca | 152 | /** |
electromotivated | 0:3b2b4f34aaca | 153 | Load Wave Sequence into DRV2605 Sequence Registers |
electromotivated | 0:3b2b4f34aaca | 154 | @param effect1... effect8 The effect to play. Valid inputs are |
electromotivated | 0:3b2b4f34aaca | 155 | 0 to 123; 0: Stop Condition, |
electromotivated | 0:3b2b4f34aaca | 156 | 1- 123: Waveform Index |
electromotivated | 0:3b2b4f34aaca | 157 | */ |
electromotivated | 0:3b2b4f34aaca | 158 | void load_waveform_sequence(int effect1 = 0, int effect2 = 0, |
electromotivated | 0:3b2b4f34aaca | 159 | int effect3 = 0, int effect4 = 0, |
electromotivated | 0:3b2b4f34aaca | 160 | int effect5 = 0, int effect6 = 0, |
electromotivated | 0:3b2b4f34aaca | 161 | int effect7 = 0, int effect8 = 0); |
electromotivated | 0:3b2b4f34aaca | 162 | |
electromotivated | 0:3b2b4f34aaca | 163 | /** |
electromotivated | 0:3b2b4f34aaca | 164 | Plays the currently loaded waveform or waveform sequence. |
electromotivated | 0:3b2b4f34aaca | 165 | Call this after calling play_waveform() or after calling |
electromotivated | 0:3b2b4f34aaca | 166 | load_waveform_sequence() |
electromotivated | 0:3b2b4f34aaca | 167 | Preconditions: User must have already loaded waveform(s) |
electromotivated | 0:3b2b4f34aaca | 168 | using play_waveform() or load_waveform_sequence() |
electromotivated | 0:3b2b4f34aaca | 169 | */ |
electromotivated | 0:3b2b4f34aaca | 170 | void play(); |
electromotivated | 0:3b2b4f34aaca | 171 | |
electromotivated | 0:3b2b4f34aaca | 172 | /** |
electromotivated | 0:3b2b4f34aaca | 173 | TODO: Add Closed Loop Calibration |
electromotivated | 0:3b2b4f34aaca | 174 | |
electromotivated | 0:3b2b4f34aaca | 175 | Run basic DRV2605L Auto- Calibration as detailed in Section 2 of |
electromotivated | 0:3b2b4f34aaca | 176 | the Device Setup Guide for OPEN- LOOP ONLY. |
electromotivated | 0:3b2b4f34aaca | 177 | This must be done before using the device in closed- loop mode |
electromotivated | 0:3b2b4f34aaca | 178 | (unless cal has been done before with values stored in non-volatile |
electromotivated | 0:3b2b4f34aaca | 179 | mem; see datasheet for more info). |
electromotivated | 0:3b2b4f34aaca | 180 | |
electromotivated | 0:3b2b4f34aaca | 181 | NOTE: It is NOT recommended to store cal values into device |
electromotivated | 0:3b2b4f34aaca | 182 | non-volatile memory as this can be done only once. Thus do not |
electromotivated | 0:3b2b4f34aaca | 183 | use this feature unless the device is being used in a final |
electromotivated | 0:3b2b4f34aaca | 184 | project AND values have been confirmed to result in satisfactory |
electromotivated | 0:3b2b4f34aaca | 185 | performance). |
electromotivated | 0:3b2b4f34aaca | 186 | |
electromotivated | 0:3b2b4f34aaca | 187 | This uses many of the default device register values such as |
electromotivated | 0:3b2b4f34aaca | 188 | the default DRIVE_TIME . |
electromotivated | 0:3b2b4f34aaca | 189 | |
electromotivated | 0:3b2b4f34aaca | 190 | @param actuator_peak_voltage The maximum/peak voltage rating of the actuator |
electromotivated | 0:3b2b4f34aaca | 191 | */ |
electromotivated | 0:3b2b4f34aaca | 192 | uint8_t auto_cal_open_loop(float actuator_peak_voltage); |
electromotivated | 0:3b2b4f34aaca | 193 | |
electromotivated | 0:3b2b4f34aaca | 194 | private: |
electromotivated | 0:3b2b4f34aaca | 195 | I2C i2c; |
electromotivated | 0:3b2b4f34aaca | 196 | |
electromotivated | 0:3b2b4f34aaca | 197 | }; |
electromotivated | 0:3b2b4f34aaca | 198 | |
electromotivated | 0:3b2b4f34aaca | 199 | #endif |