A little info on the basic project.
I have been getting an Arduino based data recorder set up, now it's time to work on the reader. These are 2 separate systems that will take an input data set from one articulated arm with sensors, and then an SD card will be transferred to the second system that will use the data that was recorded to reproduce the associated movement on the powered arm. Essentially, puppeteering VIA SD card file.
I am sampling the positions at 1/2 second intervals, and I need to import the data at the same rate. Currently there are only 6 values, 0-1023(separated by a comma) per line. The number of values will increase greatly as the project grows , but I can add to it after I get things working .
I'm hoping for some advice on importing the data. Possibly into an array like this?
How to read numbers from a txt file on an SD card into an array!
What would be the best way to import and separate the data to be used in my code?
Will I be better off setting this up with millis instead of delays? I don't know which one will give me more precise intervals of reading vs using data.
Hopefully I got my idea across. I am definitely not a coder, but I'm doing my best to learn and work this out myself. I just need some tips to point me in the right direction.
#include <PID_v1.h>
#include <SPI.h>
#include <SD.h>
int actual1;
int demand1;
int actual2;
int demand2;
int actual3;
int demand3;
int actual4;
int demand4;
int actual5;
int demand5;
int actual6;
int demand6;
File myFile;
void data(int);
double Pk = 10; //speed it gets there
double Ik = 0; // smaller number, more quickly controller reacts to load changes but the greater the risk of oscillations. 0 = off
double Dk = 1; // bigger number, more the controller dampens oscillations higher can harm performance
double Setpoint1, Input1, Output1;
double Setpoint2, Input2, Output2;
double Setpoint3, Input3, Output3;
double Setpoint4, Input4, Output4;
double Setpoint5, Input5, Output5;
double Setpoint6, Input6, Output6;
PID PID1(&Input1, &Output1, &Setpoint1, Pk, Ik, Dk, DIRECT);
PID PID2(&Input2, &Output2, &Setpoint2, Pk, Ik, Dk, DIRECT);
PID PID3(&Input3, &Output3, &Setpoint3, Pk, Ik, Dk, DIRECT);
PID PID4(&Input4, &Output4, &Setpoint4, Pk, Ik, Dk, DIRECT);
PID PID5(&Input5, &Output5, &Setpoint5, Pk, Ik, Dk, DIRECT);
PID PID6(&Input6, &Output6, &Setpoint6, Pk, Ik, Dk, DIRECT);
void setup() {
pinMode (A0, INPUT); // Section-1 up down
pinMode (A1, INPUT); // Section-2 up down
pinMode (A2, INPUT); // Head up down
pinMode (A3, INPUT); // Section-1 left right
pinMode (A4, INPUT); // Section-2 left right
pinMode (A5, INPUT); // Head left right
pinMode (2, OUTPUT); // L298N-1 section 1 up down
pinMode (3, OUTPUT);
pinMode (4, OUTPUT); // L298N-2 section 2 up down
pinMode (5, OUTPUT);
pinMode (6, OUTPUT); // L298N-3 head up down
pinMode (7, OUTPUT);
pinMode (8, OUTPUT); // BTS7960-1 section 1 right left
pinMode (9, OUTPUT);
pinMode (10, OUTPUT); // BTS7960-2 section 2 right left
pinMode (11, OUTPUT);
pinMode (12, OUTPUT); // BTS7960-3 head right left
pinMode (13, OUTPUT);
Serial.begin(9600);
PID1.SetMode(AUTOMATIC);
PID1.SetOutputLimits(-255,255);
PID1.SetSampleTime(10);
PID2.SetMode(AUTOMATIC);
PID2.SetOutputLimits(-255,255);
PID2.SetSampleTime(10);
PID3.SetMode(AUTOMATIC);
PID3.SetOutputLimits(-255,255);
PID3.SetSampleTime(10);
PID4.SetMode(AUTOMATIC);
PID4.SetOutputLimits(-255,255);
PID4.SetSampleTime(10);
PID5.SetMode(AUTOMATIC);
PID5.SetOutputLimits(-255,255);
PID5.SetSampleTime(10);
PID6.SetMode(AUTOMATIC);
PID6.SetOutputLimits(-255,255);
PID6.SetSampleTime(10);
data();
}
void loop() {
unsigned long currentMillis = millis();
// ** start PID 1 **
actual1 = analogRead(A0);
actual1 = map(actual1,0,1023,-255,255);
Input1 = actual1;
Setpoint1 = 0;
PID1.Compute();
Serial.print(Output1);
Serial.print(",");
if (Output1 < 0) {
Output1 = abs(Output1);
analogWrite(2,Output1);
analogWrite(3,0);
}
else if (Output1 >= 0) {
Output1 = abs(Output1);
analogWrite(3,Output1);
analogWrite(2,0);
}
else {
analogWrite(2,0);
analogWrite(3,0);
}
// ** start PID 2 **
actual2 = analogRead(A1);
actual2 = map(actual2,0,1023,-255,255);
Input2 = actual2;
Setpoint2 = 0;
PID2.Compute();
Serial.print(Output2);
Serial.print(",");
if (Output2 < 0) {
Output5 = abs(Output2);
analogWrite(4,Output2);
analogWrite(5,0);
}
else if (Output2 >= 0) {
Output5 = abs(Output2);
analogWrite(5,Output2);
analogWrite(4,0);
}
else {
analogWrite(4,0);
analogWrite(5,0);
}
// ** start PID 3 **
actual3 = analogRead(A2);
actual3 = map(actual3,0,1023,-255,255);
Input3 = actual3;
Setpoint3 = 0;
PID3.Compute();
Serial.print(Output3);
Serial.print(",");
if (Output3 < 0) {
Output3 = abs(Output3);
analogWrite(6,Output3);
analogWrite(7,0);
}
else if (Output3 >= 0) {
Output3 = abs(Output3);
analogWrite(7,Output3);
analogWrite(6,0);
}
else {
analogWrite(6,0);
analogWrite(7,0);
}
// ** start PID 4 **
actual4 = analogRead(A3);
actual4 = map(actual5,0,1023,-255,255);
Input4 = actual4;
Setpoint4 = 0;
PID4.Compute();
Serial.print(Output4);
Serial.print(",");
if (Output4 < 0) {
Output5 = abs(Output4);
analogWrite(8,Output4);
analogWrite(9,0);
}
else if (Output5 >= 0) {
Output4 = abs(Output4);
analogWrite(9,Output4);
analogWrite(8,0);
}
else {
analogWrite(8,0);
analogWrite(9,0);
}
// ** start PID 5 **
actual5 = analogRead(A4);
actual5 = map(actual5,0,1023,-255,255);
Input5 = actual5;
Setpoint5 = 0;
PID5.Compute();
Serial.print(Output5);
Serial.print(",");
if (Output5 < 0) {
Output5 = abs(Output5);
analogWrite(10,Output5);
analogWrite(11,0);
}
else if (Output5 >= 0) {
Output5 = abs(Output5);
analogWrite(11,Output5);
analogWrite(10,0);
}
else {
analogWrite(10,0);
analogWrite(11,0);
}
// ** start PID 6 **
actual6 = analogRead(A5);
actual6 = map(actual6,0,1023,-255,255);
Input6 = actual6;
Setpoint6 = 0;
PID6.Compute();
Serial.println(Output6);
if (Output6 < 0) {
Output6 = abs(Output6);
analogWrite(12,Output6);
analogWrite(13,0);
}
else if (Output6 >= 0) {
Output6 = abs(Output6);
analogWrite(13,Output6);
analogWrite(12,0);
}
else {
analogWrite(12,0);
analogWrite(13,0);
}
delay(50);
}