Hi, i am using IntervalTimer and SPIslave libraries to make a system.
The system's schematic diagram is
What i'm trying to do is read Encoder value from Motor Encoder and IMU signal from rp2040 nano (SPI) and save these data into SD.
It works fine without using interrupt (put code in loop), but if i put code into interval timer, Encoder values are shifted.
Left chart is logged encoder data when i put code into loop, right one is encoder data when i use interval timer.
Starting point (0 degree) is fixed so it does not change. I rotated the motor few times and returned to starting point (0 degree)
While i'm using interval timer, it seems encoder values are wrong and shifted(not back to 0degree).
I'm not and expert in programming, maybe I miss something. But i assumed there is conflict between intevaltimer and SPISlave_T4 libraries.
It would be really appreciated if any helps.
The system's schematic diagram is
What i'm trying to do is read Encoder value from Motor Encoder and IMU signal from rp2040 nano (SPI) and save these data into SD.
It works fine without using interrupt (put code in loop), but if i put code into interval timer, Encoder values are shifted.
Left chart is logged encoder data when i put code into loop, right one is encoder data when i use interval timer.
Starting point (0 degree) is fixed so it does not change. I rotated the motor few times and returned to starting point (0 degree)
While i'm using interval timer, it seems encoder values are wrong and shifted(not back to 0degree).
I'm not and expert in programming, maybe I miss something. But i assumed there is conflict between intevaltimer and SPISlave_T4 libraries.
It would be really appreciated if any helps.
Code:
#include <Encoder.h>
#include <SD.h>
#include <SPI.h>
#include <IntervalTimer.h>
#include <TimeLib.h>
#include "SPISlave_T4.h"
SPISlave_T4<&SPI, SPI_16_BITS> mySPI;
Encoder myEnc(0, 1);
IntervalTimer dataTimer;
#define LED_1 7
#define LED_2 5
#define SIGNAL_PIN 9
#define uint16 32678.0
#define gear_ratio 6.0
#define pulse 7040.0
float x_a,y_a,z_a;
const int chipSelect = BUILTIN_SDCARD;
double newPosition;
void setup()
{
Serial.begin(115200);
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
while (1) { analogWrite(LED_2,255);
// No SD card, so don't do anything more - stay stuck here
}
}
Serial.println("card initialized.");
delay(100);
mySPI.onReceive(myFunc);
mySPI.begin();
dataTimer.begin(Controller, 5000);
}
void loop() {
/*
newPosition = (myEnc.read()/(4.0*pulse*gear_ratio/360));
File dataFile = SD.open("loop_test.csv", FILE_WRITE);
if (dataFile) {
dataFile.print(newPosition);
dataFile.print(",");
dataFile.print(x_a);
dataFile.print(",");
dataFile.print(y_a);
dataFile.print(",");
dataFile.println(z_a);
//dataFile.println(newENC/(4.0*7040.0*6.0/360));
dataFile.close();
// print to the serial port too:
Serial.println(newPosition);
//Serial.print(" ");
//Serial.println(newENC/(4.0*7040.0*6.0/360));
}
else {
// if the file isn't open, pop up an error:
analogWrite(LED_1,255);
// wait for a second
}
*/
Serial.println(newPosition);
}
void Controller() {
newPosition = (myEnc.read()/(4.0*pulse*gear_ratio/360));
File dataFile = SD.open("timer_test.csv", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(newPosition);
dataFile.print(",");
dataFile.print(x_a);
dataFile.print(",");
dataFile.print(y_a);
dataFile.print(",");
dataFile.println(z_a);
dataFile.close();
// print to the serial port too:
}
else {
// if the file isn't open, pop up an error:
// wait for a second
}
}
void myFunc() {
while (mySPI.active()) {
if (mySPI.available()) {
mySPI.pushr(2);
x_a = (mySPI.popr());
mySPI.pushr(0);
y_a = (mySPI.popr());
mySPI.pushr(0);
z_a = (mySPI.popr());
}
}
}