I just got my stepper motor, and Phi robotics Smartlynx (L6470 based) stepper motor driver (Documentation is here http://www.robotshop.com/media/files/zip/documentation-h-elc-mdr-stp-2.zip)
It give some pseudocode, which for the most part is C, so it was no trouble figuring that out, but they missed giving me the function "smartlynxDataTransfer".. there's a function called "smartlynxWriteData", with a similar signature, but since this is my first SPI based project I'm a little lost as to what ought to be in that missing function.. the L6470 datasheet doesn't help me much either.
Here is my code.
#include <SPI.h>
const int SPI_CS = 7; //SPI cable select pin
const int STEP_PIN = 50; //pin for single stepping
// smartlynx register addresses
const byte NOP = 0b00000000;
const int SMARTLYNX_ABS_POS = 0x01;
const int SMARTLYNX_SPEED = 0x04;
const int SMARTLYNX_ACC = 0x05;
const int SMARTLYNX_DEC = 0x06;
const int SMARTLYNX_MAX_SPEED = 0x07;
const int SMARTLYNX_MIN_SPEED = 0x08;
const int SMARTLYNX_FS_SPD = 0x15;
const int SMARTLYNX_STEP_MODE = 0x16;
const int SMARTLYNX_STATUS = 0x19;
//smartlynx commands
const int SMARTLYNX_SET_PARAM = 0x00;
const int SMARTLYNX_GET_PARAM = 0x20;
const int SMARTLYNX_RUN = 0x50;
const int SMARTLYNX_MOVE = 0x40;
const int SMARTLYNX_SOFT_STOP = 0xB0;
const int SMARTLYNX_HARD_STOP = 0xB8;
const int SMARTLYNX_GET_STATUS = 0xD0;
// step size selection
const int SMARTLYNX_STEP_FULL = 1;
const int SMARTLYNX_STEP_HALF = 2;
const int SMARTLYNX_STEP_QUARTER = 3;
const int SMARTLYNX_STEP_ONE_EIGHT = 4;
const int SMARTLYNX_STEP_ONE_16TH = 5;
const int SMARTLYNX_STEP_ONE_32ND = 6;
const int SMARTLYNX_STEP_ONE_64TH = 7;
const int SMARTLYNX_STEP_ONE_128TH = 8;
void setup() {
Serial.begin(9600);
// start the SPI library:
SPI.begin();
pinMode(STEP_PIN, OUTPUT);
smartlynxSetMaxSpeed(100);
}
void loop() {
smartlynxRun(1, 100);
}
//I made these two functions, though I'll probably just get rid of them in the future...
void gpioClear(int CS_Pin) {
digitalWrite(CS_Pin, LOW);
}
void gpioSet(int CS_Pin) {
digitalWrite(CS_Pin, HIGH);
}
void spiWriteByte(byte data) {
gpioClear(SPI_CS);
SPI.transfer(data);
gpioSet(SPI_CS);
}
void smartlynxDataTransfer(uint32_t Register, uint32_t data) {
//this is the function I'm not given pseudocode for
}
void smartlynxWriteData(uint32_t data, uint8_t byteCount)
{
for (int index = 3; index >= 1; index--) {
// right shift data according to loop index
gpioClear(SPI_CS);
SPI.transfer(data >> (8 * (index - 1)));
gpioSet(SPI_CS);
}
}
void smartlynxSetMaxSpeed(uint32_t maxSpeed)
{
uint32_t data;
// data conversion for smart lynx
data = 0.065536 * maxSpeed;
gpioClear(SPI_CS); // assert chip select
// set maximum speed command
spiWriteByte(SMARTLYNX_SET_PARAM | SMARTLYNX_MAX_SPEED);
gpioSet(SPI_CS); // de-assert chip deselect
// set maximum speed of rotation
smartlynxDataTransfer(data, 2);
}
void smartlynxSetMinSpeed(uint32_t minSpeed)
{
uint32_t data;
// data conversion for smart lynx
data = 4.1943 * minSpeed; // minimum speed can be 0
gpioClear(SPI_CS);
// set minimum speed command
spiWriteByte(SMARTLYNX_SET_PARAM | SMARTLYNX_MIN_SPEED);
gpioSet(SPI_CS);
//set minimum speed of rotation
smartlynxDataTransfer(data, 2);
}
void smartlynxSetAcceleration(uint32_t acc)
{
uint32_t data;
// data conversion for smart lynx
data = 0.0687 * acc;
gpioClear(SPI_CS);
// set acceleration command
spiWriteByte(SMARTLYNX_SET_PARAM | SMARTLYNX_ACC);
gpioSet(SPI_CS);
// set acceleration value
smartlynxDataTransfer(data, 2);
}
void smartlynxSetStepSize(uint8_t stepSize)
{
gpioClear(SPI_CS);
// set step size command
spiWriteByte(SMARTLYNX_SET_PARAM | SMARTLYNX_STEP_MODE);
gpioSet(SPI_CS);
gpioClear(SPI_CS);
// set step size
spiWriteByte(stepSize);
gpioSet(SPI_CS);
}
void smartlynxSetSpeed(uint32_t runspeed)
{
gpioClear(SPI_CS);
// set speed command
spiWriteByte(SMARTLYNX_SET_PARAM | SMARTLYNX_FS_SPD);
gpioSet(SPI_CS);
// set speed
smartlynxDataTransfer(runspeed, 2);
}
// this function will put motor in continuous running mode
void smartlynxRun(uint8_t dir, uint32_t runspeed)
{
uint32_t data;
// data conversion for smart lynx
data = 67.1 * runspeed;
gpioClear(SPI_CS);
// run command
spiWriteByte(SMARTLYNX_RUN | dir);
gpioSet(SPI_CS);
// set speed
smartlynxDataTransfer(data, 3);
}
// this function will move motor by specified number of steps (stepCount)
void smartlynxMove(uint8_t dir, uint32_t stepCount)
{
gpioClear(SPI_CS);
// move command
spiWriteByte(SMARTLYNX_MOVE | dir);
gpioSet(SPI_CS);
// set number of steps to move
smartlynxDataTransfer(stepCount, 3);
}
// single step driving for manual control
void smartlynxSingleStep(void)
{
// pulse to STEP pin
gpioClear(STEP_PIN);
delayMicroseconds(1);
gpioSet(STEP_PIN);
}
// this function will stop motor by decelerating it
// to minimum speed set
void smartlynxSoftStop(void)
{
gpioClear(SPI_CS);
// soft stop command
spiWriteByte(SMARTLYNX_SOFT_STOP);
gpioSet(SPI_CS);
}
// this function will stop the motor immediately
void smartlynxHardStop(void)
{
gpioClear(SPI_CS);
// hard stop command
spiWriteByte(SMARTLYNX_HARD_STOP);
gpioSet(SPI_CS);
}
I think I'll figure the rest out once I know what should be in that function
thanks