In order to operate 3 stepper motor simultaneously, I have to sent serial data to the firmware of the stepper motor drivers via RS-485. I use Arduino Mega to send that stream of strings after reading from an SD card. The bottleneck is the speed at which the firmware needs that strings to drive the motors.
I have to send the commands to 3 different drivers at every milliseconds intervals. So, I use an interrupt in the purpose. The format of the strings are such that <11 character here>, thus, i can identify these characters enclosed with the start & end markers.
Since I'm beginner in the field, I try to use a combination of example codes attached as follows. When I use this method to drive the motors via SD card (comprising all the sequential commands in a txt file), I realize that recieving & sending the 11 characters for just the first driver takes 1 milllisecond.
I wonder the possibility of reading at faster speeds from SD card. Any suggestions or any source that I can begin with?
Previously, I read related forum posts. But, unfortunetely, I could not find a method that I can understand and implement.
#include <SdFat.h>
SdFat sd;
SdFile myFile;
const int chipSelect = 53;
const byte numChars = 11;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup() {
Serial1.begin(115200);
Serial.begin(115200);
Serial1.setTimeout(1);
Serial.setTimeout(1);
// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
if (!myFile.open("test.txt", O_READ)) {
sd.errorHalt("opening test.txt for read failed");
}
//set timer1 interrupt at 1kHz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 250hz increments
OCR1A = 250 - 1; // = 0.001*(16*10^6) / (1*64) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 for x1 prescaler
TCCR1B |= ((1 << CS10) | ( 1 << CS11)); //64 prescale
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
//myFile.close();
}
ISR(TIMER1_COMPA_vect)
{ //timer1 interrupt 1
recvWithStartEndMarkers(); // command for the first stepper
showNewData(); // send commands from serial for the first stepper
recvWithStartEndMarkers(); // command for the second stepper
showNewData(); // send commands from serial for the second stepper
recvWithStartEndMarkers(); // command for the third stepper
showNewData(); // send commands from serial for the third stepper
}
void loop() {
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
while ((myFile.available() >= 0) && newData == false) {
char rc = myFile.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial1.print(String(receivedChars));
Serial1.print("\r\n");
Serial.print(String(receivedChars));
Serial.print("\r\n");
newData = false;
}
}