I have a background project going while I am waiting for parts to arrive.
I have been playing with speedystepper up to this point, but looking further down the road I will need to operate the motor at the same time as a display and various inputs and output. Speed stepper doesn't make this the easiest thing in the world.
At that point i tought i would have a look at a other libraries. Now being in the resonably early stages I tought i would setup a program to print various bit of data out over the serial port.
The .run() command is called every 10 microseconds on timer interrupt so I don't have to remeber to run it. The function called by interrupt
This doesn't work. Remove the 2 serial prints and it's fine.
The next step is to intergrate an encoder into the system. The user interface needs to be arranged as well so i suspect there maybe another encoder or button and a display - or maybe a touch screen. Either way it will most likely be over SPI.
I am working on testing with the encoder which is why there is some code relating to that in there.
Idealy, I would check the encoder once the motor had been stepped as it would save doing an extra call, more testing is needed at this point but will value any input and expierence .
Complete code that i was testing with:
I have been playing with speedystepper up to this point, but looking further down the road I will need to operate the motor at the same time as a display and various inputs and output. Speed stepper doesn't make this the easiest thing in the world.
At that point i tought i would have a look at a other libraries. Now being in the resonably early stages I tought i would setup a program to print various bit of data out over the serial port.
The .run() command is called every 10 microseconds on timer interrupt so I don't have to remeber to run it. The function called by interrupt
Code:
void run_motor(){
stepper.run();
run_count++;
Serial.print("Run Motor; ");
Serial.println(run_count);
}
The next step is to intergrate an encoder into the system. The user interface needs to be arranged as well so i suspect there maybe another encoder or button and a display - or maybe a touch screen. Either way it will most likely be over SPI.
I am working on testing with the encoder which is why there is some code relating to that in there.
Idealy, I would check the encoder once the motor had been stepped as it would save doing an extra call, more testing is needed at this point but will value any input and expierence .
Complete code that i was testing with:
Code:
#include <SPI.h>
#include <AccelStepper.h>
IntervalTimer motor_timer;
const byte CS_pin = 10; //Chip select pin for manual switching
uint16_t rawData = 0; //bits from the encoder (16 bits, but top 2 has to be discarded)
float degAngle = 0; //Angle in degrees
float startAngle = 0; //starting angle for reference
float correctedAngle = 0; //tared angle value (degAngle-startAngle)
float totalAngle = 0; //total accumulated angular displacement
float numberofTurns = 0;
float rpmCounter = 0; //counts the number of turns for a certain period
float revsPerMin = 0; //RPM
int quadrantNumber = 0;
int previousquadrantNumber = 0;
uint16_t startRaw = 0;
const int step_pin = 2;
const int direct = 5;
const int enable_pin = 6;
uint16_t command = 0b1111111111111111; //read command (0xFFF)
int run_count = 0;
int old_run_counter = 0;
int target = 24000;
int loop_count = 0;
AccelStepper stepper(1, step_pin, direct, true);
AccelStepper setEnablePin(enable_pin);
void setup(){
SPI.begin();
Serial.begin(115200);
Serial.println("AS5048A - Magnetic position encoder");
pinMode(CS_pin, OUTPUT); //CS pin - output
//Checking the initial angle
readRegister(); //the first read of the encoder
startAngle = degAngle; //correct the angle - most likely going to use raw data
startRaw = rawData;
pinMode(enable_pin, OUTPUT); //motor enable pin
pinMode(direct, OUTPUT); //direction pin
pinMode(step_pin, OUTPUT); //step pin
stepper.setMaxSpeed(12500);
stepper.setAcceleration(12500);
stepper.enableOutputs();
stepper.setCurrentPosition(0);
motor_timer.begin(run_motor, 10);
}
void loop() {
Serial.println("Moving to target");
stepper.moveTo(target);
while (stepper.isRunning()){
//do stuff here
loop_count++;
}
Serial.print("First loop: ");
Serial.println(loop_count);
noInterrupts();
//do no interrupt stuff here
Serial.println("Hold Off Interrupt");
//delay(2000);
old_run_counter = run_count;
run_count = 0;
loop_count = 0;
interrupts();
Serial.print("Run Motor; ");
Serial.println(old_run_counter);
Serial.println("Moving to 0");
stepper.moveTo(0);
while (stepper.isRunning()){
//do stuff here
loop_count++;
}
Serial.print("Second loop: ");
Serial.println(loop_count);
loop_count = 0;
}
void readRegister()
{
SPI.beginTransaction(SPISettings(3000000, MSBFIRST, SPI_MODE1));
//--sending the command
digitalWrite(CS_pin, LOW);
SPI.transfer16(command);
digitalWrite(CS_pin, HIGH);
//delay(10);
//--receiving the reading
digitalWrite(CS_pin, LOW);
rawData = SPI.transfer16(command);
digitalWrite(CS_pin, HIGH);
SPI.endTransaction();
rawData = rawData & 0b0011111111111111; //removing the top 2 bits (PAR and EF)
int q = rawData - startRaw;
q = abs(q);
degAngle = (float)rawData / 16384.0 * 360.0; //16384 = 2^14, 360 = 360 degrees
Serial.print("Raw Data: ");
Serial.print(rawData);
Serial.print(" | ");
Serial.println(q);
//Serial.print("Deg: ");
//Serial.println(degAngle, 6);
}
void run_motor(){
stepper.run();
run_count++;
//Serial.print("Run Motor; ");
//Serial.println(run_count);
}