Hi, I would like to get suggestion how to do exception handling for the first or last double array value with time measuring technique using millis().
Here is the simple explanation,
-
In double array, value is stored as [time, position for ID 1 servo, position for ID 2 servo, position for ID 3 servo ].
-
In the loop, a timer using milis() compares the trajectory time span in double array.
My approach for the trajectory time span is simply to subtract the previous time from the very next one in two rows in double array (See trajectory in the code)
If the timer corresponds to stored time in each row of double array, it will do stored position in that row.
Issue:
As you know, there is no value after the last row in the double array, so I can not get the trajectory time span by subtracting two points in row, I am wondering that there any approach I can try to achieve my goal? (Such as except handling for the last row in array.., But I failed a lot
)
unsigned long now = 0;
unsigned long past = 0;
const int counts = 11; // The number of counts for Time Data.
int trajectory [counts][4]{ // Sampled Goal Position's Trjectory Data.
{ 0, 360, 360, 360}, // Time[ms], ID1, ID2, ID3
{100, 340, 328, 300},
{200, 320, 259, 262},
{300, 278, 281, 289},
{400, 205, 211, 226},
{500, 238, 246, 270},
{600, 274, 286, 218},
{700, 211, 226, 269},
{800, 247, 266, 218},
{900, 281, 203, 263},
{1000,209, 234, 202},
};
int j = 0;
int cnt = 0; // Loop three times
//int k = 0;
//bool zero_check = false;
void loop() {
now = millis(); // Start.
if(now - past >= trajectory[j+1][0] - trajectory[j][0]) // Time difference between the past (past) and current time (now) 0.
{
DEBUG_SERIAL.print("Now: ");
DEBUG_SERIAL.print(now);
DEBUG_SERIAL.print("\t Past: ");
DEBUG_SERIAL.print(past);
DEBUG_SERIAL.print("\t Now-Past: ");
DEBUG_SERIAL.print(now - past);
DEBUG_SERIAL.print("\t Trajectory time span in two points:");
DEBUG_SERIAL.print(trajectory[j+1][0] - trajectory[j][0]);
DEBUG_SERIAL.print("\t Trajectory point in time :");
DEBUG_SERIAL.print(trajectory[j][0]);
DEBUG_SERIAL.print("\t ID1:");
DEBUG_SERIAL.print(trajectory[j][1]);
DEBUG_SERIAL.print("\t ID2:");
DEBUG_SERIAL.print(trajectory[j][2]);
DEBUG_SERIAL.print("\t ID3:");
DEBUG_SERIAL.print(trajectory[j][3]);
DEBUG_SERIAL.print("\t J:");
DEBUG_SERIAL.println(j);
j +=1;
past = now; // Update time.
if (j >= counts-1 ) // J = 28
{ DEBUG_SERIAL.println("######################END#######################");
DEBUG_SERIAL.print("J = ");
DEBUG_SERIAL.println(j);
j = 1;
}
}
}