Library for STMicroelectronics dSPIN L6470 stepper driver. "daisy-chain" supported.
Dependents: L6470_daisy_chain l6470
L6470SDC.h
- Committer:
- Yajirushi
- Date:
- 2015-06-09
- Revision:
- 2:2af83d3ccd97
- Parent:
- 1:db64ad30b4b3
- Child:
- 3:486fb90dc7d5
File content as of revision 2:2af83d3ccd97:
#include "mbed.h" #ifndef L6470SDC_H #define L6470SDC_H //デバッグモード(GetParamやSetParamの値をシリアル出力する) //ONにするとそれなりに動作が遅くなる //デバッグモードOFF:コメントアウトする //#define DEBUG_L6470SDC //L6470のSPI通信のクロック周波数(1MHz以上、5MHz未満であること) #define L6470_SPI_FREQ 4960000 #define CMD_NOP 0x0 #define CMD_SETPARAM 0x00 #define CMD_GETPARAM 0x20 #define CMD_RUN_PLUS 0x51 #define CMD_RUN_MINUS 0x50 #define CMD_STEP_PLUS 0x59 #define CMD_STEP_MINUS 0x58 #define CMD_ADDSTEP_PLUS 0x41 #define CMD_ADDSTEP_MINUS 0x40 #define CMD_GOTO 0x60 #define CMD_GOTO_DIR_PLUS 0x69 #define CMD_GOTO_DIR_MINUS 0x68 #define CMD_GO_UNTIL_PLUS 0x83 #define CMD_GO_UNTIL_MINUS 0x82 #define CMD_RELEASE_SW_PLUS 0x93 #define CMD_RELEASE_SW_MINUS 0x92 #define CMD_GO_HOME 0x70 #define CMD_GO_MARK 0x78 #define CMD_RESET_POS 0xD8 #define CMD_RESET_DEVICE 0xC0 #define CMD_SOFT_STOP 0xB0 #define CMD_HARD_STOP 0xB8 #define CMD_SOFT_HIZ 0xA0 #define CMD_HARD_HIZ 0xA8 #define CMD_GET_STATUS 0xD0 #define REG_NOTHING 0x0 #define REG_ABS_POS 0x01 #define REG_EL_POS 0x02 #define REG_MARK 0x03 #define REG_SPEED 0x04 #define REG_ACC 0x05 #define REG_DEC 0x06 #define REG_MAX_SPEED 0x07 #define REG_MIN_SPEED 0x08 #define REG_KVAL_HOLD 0x09 #define REG_KVAL_RUN 0x0A #define REG_KVAL_ACC 0x0B #define REG_KVAL_DEC 0x0C #define REG_INT_SPD 0x0D #define REG_ST_SLP 0x0E #define REG_FN_SLP_ACC 0x0F #define REG_FN_SLP_DEC 0x10 #define REG_K_THERM 0x11 #define REG_ADC_OUT 0x12 #define REG_OCD_TH 0x13 #define REG_STALL_TH 0x14 #define REG_FS_SPD 0x15 #define REG_STEP_MODE 0x16 #define REG_ALARM_EN 0x17 #define REG_CONFIG 0x18 #define REG_STATUS 0x19 /** Library for STMicroelectronics dSPIN L6470 stepper driver. "daisy-chain" supported. * * Example: * @code * #include "mbed.h" * #include "L6470SDC.h" * * L6470SDC l6470(USBTX, USBRX, SPI_MOSI, SPI_MISO, SPI_SCK, D10); * * int main() { * //initialize * l6470.init(); * * //set maximum speed * l6470.setMaximumSpeed(1, l6470.calcMaxSpd(100)); * * //run 300step/s,clockwise * l6470.run(1, l6470.calcSpd(300), true); * wait(5.0); * * //motor stop * l6470.stop(1); * * //motor return home position. * l6470.home(1); * } * @endcode */ class L6470SDC{ public: /** Constructor:(Serial, SPI, CS) PinName * * @param tx Serial USB_TX PinName * @param rx Serial USB_RX PinName * @param mosi SPI Master-out PinName * @param miso SPI Slave-out PinName * @param sclk SPI Clock PinName * @param csel SPI ChipSelect PinName */ L6470SDC(PinName tx, PinName rx, PinName mosi, PinName miso, PinName sclk, PinName csel); /** Constructor:(Serial, SPI, CS) PinName & DigitalOut* * * @param tx Serial USB_TX PinName * @param rx Serial USB_RX PinName * @param mosi SPI Master-out PinName * @param miso SPI Slave-out PinName * @param sclk SPI Clock PinName * @param *csel SPI ChipSelect * Example: * DigitalOut my_cs(D10); * L6470SDC stepper(USBTX, USBRX, SPI_MOSI, SPI_MISO, SPI_SCLK, &my_cs); */ L6470SDC(PinName tx, PinName rx, PinName mosi, PinName miso, PinName sclk, DigitalOut *csel); /** Constructor:(Serial, SPI, CS) Serial* & PinName * * @param *serial Serial * @param mosi SPI Master-out PinName * @param miso SPI Slave-out PinName * @param sclk SPI Clock PinName * @param csel SPI ChipSelect PinName * Example: * Serial usb_serial(USBTX, USBRX); * L6470SDC stepper(&usb_serial, SPI_MOSI, SPI_MISO, SPI_SCLK, D10); */ L6470SDC(Serial *serial, PinName mosi, PinName miso, PinName sclk, PinName csel); /** Constructor:(Serial, SPI, CS) Serial* & PinName & DigitalOut* * * @param *serial Serial * @param mosi SPI Master-out PinName * @param miso SPI Slave-out PinName * @param sclk SPI Clock PinName * @param *csel SPI ChipSelect * Example: * Serial usb_serial(USBTX, USBRX); * DigitalOut my_cs(D10); * L6470SDC stepper(&usb_serial, SPI_MOSI, SPI_MISO, SPI_SCLK, &my_cs); */ L6470SDC(Serial *serial, PinName mosi, PinName miso, PinName sclk, DigitalOut *csel); /** Constructor:(Serial, SPI, CS) Serial* & SPI* & PinName * * @param *serial Serial * @param *spi SPI * @param csel SPI ChipSelect PinName * Example: * Serial usb_serial(USBTX, USBRX); * SPI my_spi(SPI_MOSI, SPI_MISO, SPI_SCLK); * L6470SDC stepper(&usb_serial, &my_spi, D10); */ L6470SDC(Serial *serial, SPI *spi, PinName csel); /** Constructor:(Serial, SPI, CS) Serial* & SPI* & DigitalOut* * * @param *serial Serial * @param *spi SPI * @param *csel SPI ChipSelect * Example: * Serial usb_serial(USBTX, USBRX); * SPI my_spi(SPI_MOSI, SPI_MISO, SPI_SCLK); * DigitalOut my_cs(D10); * L6470SDC stepper(&usb_serial, &my_spi, &my_cs); */ L6470SDC(Serial *serial, SPI *spi, DigitalOut *csel); /** Constructor: without Debug Serial (SPI, CS) PinName * * @param mosi SPI Master-out PinName * @param miso SPI Slave-out PinName * @param sclk SPI Clock PinName * @param csel SPI ChipSelect PinName */ L6470SDC(PinName mosi, PinName miso, PinName sclk, PinName csel); /** Constructor: without Debug Serial (SPI, CS) PinName & DigitalOut* * * @param mosi SPI Master-out PinName * @param miso SPI Slave-out PinName * @param sclk SPI Clock PinName * @param *csel SPI ChipSelect * Example: * DigitalOut cs1(D10); * L6470SDC stepper(SPI_MOSI, SPI_MISO, SPI_SCLK, &cs1); */ L6470SDC(PinName mosi, PinName miso, PinName sclk, DigitalOut *csel); /** Constructor: without Debug Serial (SPI, CS) SPI* & PinName * * @param *spi SPI * @param csel SPI ChipSelect PinName * Example: * SPI spi1(SPI_MOSI, SPI_MISO, SPI_SCLK); * L6470SDC stepper(&spi1, D10); */ L6470SDC(SPI *spi, PinName csel); /** Constructor: without Debug Serial (SPI, CS) SPI* & DigitalOut* * * @param *spi SPI * @param *csel SPI ChipSelect * Example: * SPI spi1(SPI_MOSI, SPI_MISO, SPI_SCLK); * DigitalOut cs1(D10); * L6470SDC stepper(&spi1, &cs1); */ L6470SDC(SPI *spi, DigitalOut *csel); private: Serial *pc; SPI *spi; DigitalOut *cs; bool hasSerial; int motor_count; void setCmd(int motorNumber, unsigned char cmdAddress, unsigned long value, int bitLen); void setParam(int motorNumber, unsigned char regAddress, unsigned long value, int bitLen); unsigned long getParam(int motorNumber, unsigned char regAddress, int bitLen); void sendCMD(unsigned char cmd); public: /** Initialize L6470 * * @param initSPI If initialize SPI parameters.(e.g. clockspeed and mode) = true(default) */ void init(bool initSPI = true); /** Get connected(daisy-chained) motor count. * * @returns Connected(daisy-chained) motor count. If not using daisy-chain, return always 1. maybe... */ int getMotorCount(); /** Get motor Busy-flag status * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. * @returns Busy status.(false = Busy-flag 0, true = Busy-flag 1) */ bool isBusy(int motorNumber); /** Run motor rotate * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. * @param hex_speed Motor rotate speed(hex_value Step/s). -> please use "calcSpd(stepPerSecond)" method. * @param isClockwise Rotate direction. true = clockwise. false = counter-clockwise. * Example: * unsigned long calculatedValue = motor.calcSpd(200); * motor.run(1, calculatedValue, true); //200step/s, clockwise */ void run(int motorNumber, unsigned long hex_speed, bool isClockwise); /** Run motor steps * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. * @param count Steps count. If using Microstep-mode, "count" must be multiplied by Microstep-value. * @param isClockwise Rotate direction. true = clockwise. false = counter-clockwise. * Example: * motor.stop(1); * motor.setStepMode(1, 0x07); //set microstep-mode 0x07 = 1/128 microstep. * motor.step(1, 256, false); //(256/128)=2 step, counter cloclwise. */ void step(int motorNumber, unsigned int count, bool isClockwise); /** Run motor move * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. * @param stepsCount Steps count. If using Microstep-mode, "stepsCount" must be multiplied by Microstep-value. * @param isClockwise Rotate direction. true = clockwise. false = counter-clockwise. * If less than 5step -> Please use the "step(int motorNumber, unsigned int count, bool isClockwise)". * If greater than 5step -> Please use this method. */ void move(int motorNumber, unsigned long stepsCount, bool isClockwise); /** Go to motor ABS position (shortest path) * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. * @param ABSPos Motor Absolute position. * CAUTION: This method a motion to ABS position through the shortest path. Rotate direction is not constant. */ void goto1(int motorNumber, unsigned long ABSPos); /** Go to motor ABS position (Specify the rotate direction) * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. * @param ABSPos Motor Absolute position. * @param isClockwise Rotate direction. true = clockwise. false = counter-clockwise. */ void goto2(int motorNumber, unsigned long ABSPos, bool isClockwise); /** ???Run motor rotate. Until external switch status change??? * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. * @param hex_speed Motor rotate speed(hex_value Step/s). -> please use "calcSpd(stepPerSecond)" method. * @param isClockwise Rotate direction. true = clockwise. false = counter-clockwise. * @param setMark External switch status?...??.. Sorry... I could not understand. Please see L6470 datasheet. */ void goUntil(int motorNumber, unsigned long hex_speed, bool isClockwise, bool setMark); /** ???Release external switch??? * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. * @param isClockwise Rotate direction. true = clockwise. false = counter-clockwise. * @param setMark External switch status?...??.. Sorry... I could not understand. Please see L6470 datasheet. */ void releaseSwitch(int motorNumber, bool isClockwise, bool setMark); /** Go to motor home-position * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. */ void home(int motorNumber); /** Go to motor home-position [alias: home(int motorNumber)] * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. */ void zero(int motorNumber); /** Go to motor marked-position * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. * Mark Position: -> Please use setMarkPosition(int motorNumber, unsigned long value). */ void gotoMark(int motorNumber); /** Reset the ABS_POS register to zero (ABS_POS zero = home-position) * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. */ void resetHome(int motorNumber); /** Reset the ABS_POS register to zero [alias: resetHome(int motorNumber)] * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. */ void resetZero(int motorNumber); /** Reset motor device.(software reset) * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. */ void motorReset(int motorNumber); /** Stop rotation (soft-stop) * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. */ void stop(int motorNumber); /** Stop rotation. Ignore deceleration (hard-stop) * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. */ void stopImmidiate(int motorNumber); /** Stop rotation (soft-stop) state sets HighImpedance. * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. */ void stop_HighImpedance(int motorNumber); /** Stop rotation. Ignore deceleration (hard-stop) state sets HighImpedance. * * @param motorNumber Chained motor-number. If not using daisy-chain, you must be motorNumber = 1. */ void stopImmidiate_HighImpedance(int motorNumber); // calc method ----------------------------------------------------------------------------- unsigned long calcSpd(float stepPerSecond); //回転速度(step/s)をParam形式に変換 unsigned short calcAcc(float stepPerSecond_2); //加速(step/s^2)をParam形式に変換 unsigned short calcDec(float stepPerSecond_2); //減速(step/s^2)をParam形式に変換 unsigned short calcMaxSpd(float stepPerSecond); //最大回転速度(step/s)をParam形式に変換 unsigned short calcMinSpd(float stepPerSecond); //最低回転速度(step/s)をParam形式に変換 unsigned short calcIntSpd(float stepPerSecond); //加減速曲線の切り替わる速度(step/s)をParam形式に変換 unsigned short calcFullStepSpd(float stepPerSecond); //フルステップに切り替わる速度(step/s)をParam形式に変換 // set method ------------------------------------------------------------------------------ void setAbsPosition(int motorNumber, unsigned long value); //絶対座標設定 void setElecPosition(int motorNumber, unsigned short value); //マイクロステップ位置設定 void setMarkPosition(int motorNumber, unsigned long value); //ホームポジション設定 void setAcceleration(int motorNumber, unsigned short value); //加速設定 void setDeceleration(int motorNumber, unsigned short value); //減速設定 void setMaximumSpeed(int motorNumber, unsigned short value); //最大回転速度設定 void setMinimumSpeed(int motorNumber, unsigned short value); //最低回転速度設定(普通はゼロ) void setHoldingKVAL(int motorNumber, unsigned char value); //モーター停止中の電圧 void setRunningKVAL(int motorNumber, unsigned char value); //モーター駆動中の電圧 void setAccelerationKVAL(int motorNumber, unsigned char value); //モーター加速中の電圧 void setDecelerationKVAL(int motorNumber, unsigned char value); //モーター減速中の電圧 void setKVAL( //モーターの電圧(一度に設定) int motorNumber, unsigned char holdVal, unsigned char runVal, unsigned char accVal, unsigned char decVal ); void setInterpolateSpeed(int motorNumber, unsigned short value); //加減速補間を開始するスピード void setInterpolateSlope(int motorNumber, unsigned char value); //加減速補間の傾き void setAccSlopeFinal(int motorNumber, unsigned char value); //加速最終時の補間の傾き void setDecSlopeFinal(int motorNumber, unsigned char value); //減速最終時の補間の傾き void setThermoCorrect(int motorNumber, unsigned char value); //高温時の補正 void setOCThreshold(int motorNumber, unsigned char value); //オーバーカレントの電流閾値 void setStallThreshold(int motorNumber, unsigned char value); //ストールの電流閾値 void setFSSpeed(int motorNumber, unsigned short value); //フルステップ駆動に切り替えるスピード void setStepMode(int motorNumber, unsigned char value); //マイクロステッピングモード指定 void setAlermEnable(int motorNumber, unsigned char value); //アラームの有効無効 void setSystemConfig(int motorNumber, unsigned short value); //ドライバシステム設定 // get method ------------------------------------------------------------------------------ unsigned long getSpeed(int motorNumber); //現在の回転スピード unsigned short getADC(int motorNumber); //ADCの取得 unsigned short getStatus(int motorNumber); //ステータス読み取り unsigned long getAbsPosition(int motorNumber); //絶対座標 unsigned short getElecPosition(int motorNumber); //マイクロステップ位置 unsigned long getMarkPosition(int motorNumber); //ホームポジション unsigned short getAcceleration(int motorNumber); //加速 unsigned short getDeceleration(int motorNumber); //減速 unsigned short getMaximumSpeed(int motorNumber); //最大回転速度 unsigned short getMinimumSpeed(int motorNumber); //最低回転速度(普通はゼロ) unsigned char getHoldingKVAL(int motorNumber); //モーター停止中の電圧 unsigned char getRunningKVAL(int motorNumber); //モーター駆動中の電圧 unsigned char getAccelerationKVAL(int motorNumber); //モーター加速中の電圧 unsigned char getDecelerationKVAL(int motorNumber); //モーター減速中の電圧 unsigned short getInterpolateSpeed(int motorNumber); //加減速補間を開始するスピード unsigned char getInterpolateSlope(int motorNumber); //加減速補間の傾き unsigned char getAccSlopeFinal(int motorNumber); //加速最終時の補間の傾き unsigned char getDecSlopeFinal(int motorNumber); //減速最終時の補間の傾き unsigned char getThermoCorrect(int motorNumber); //高温時の補正 unsigned char getOCThreshold(int motorNumber); //オーバーカレントの電流閾値 unsigned char getStallThreshold(int motorNumber); //ストールの電流閾値 unsigned short getFSSpeed(int motorNumber); //フルステップ駆動に切り替えるスピード unsigned char getStepMode(int motorNumber); //マイクロステッピングモード unsigned char getAlermEnable(int motorNumber); //アラームの有効無効 unsigned short getSystemConfig(int motorNumber); //ドライバシステム設定 }; #endif