update c mac steppers to take args and remove windows.h

This commit is contained in:
2023-12-04 17:54:36 +00:00
parent b9e91a14ab
commit e188f0f2f4
2 changed files with 69 additions and 81 deletions

View File

@@ -1,7 +1,6 @@
// Leib Ramp Stepper program with millis function adapted for MacOS // Leib Ramp Stepper program with millis function adapted for MacOS
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <windows.h>
#include <math.h> #include <math.h>
#include <time.h> #include <time.h>
@@ -52,57 +51,52 @@ const float minSpeed = 1.0;
const float maxPermissSpeed = 20.0; const float maxPermissSpeed = 20.0;
const float maxAccel = 10.0; const float maxAccel = 10.0;
int main() int main(int argc, char *argv[1])
{ {
unsigned long currentMillis = millis(); unsigned long currentMillis = millis();
prevStepTime = 0; prevStepTime = 0;
long positionToMoveTo; long positionToMoveTo;
while(true) sscanf(argv[1], "%ld", &positionToMoveTo);
printf(" Time (s), Speed (steps/s), Accel (steps/s^2), Posit'n (steps), Step time (ticks)\n");
goToPosition(positionToMoveTo);
/* -------------------------------------------------------------- */
/* Start of pre-computation code - only executed once per profile */
// STEP 1
// Define number of steps in acceleration phase using Equation (3)
accelSteps = (long)(( maxPermissSpeed * maxPermissSpeed - minSpeed * minSpeed) / ( 2.0 * (float)maxAccel)); // Equation 4 but need to consider initial speed
stepsToGo = computeStepsToGo();
maxSpeed = maxPermissSpeed;
if (2 * accelSteps > stepsToGo)
{ {
printf("Enter position to move to in profile (or 999 to terminate)\n"); // STEP 2
scanf("%ld", &positionToMoveTo); // Define maximum speed in profile and number of steps in acceleration phase
if (positionToMoveTo==999) break; // Use Equations (4) and (5)
printf(" Time (s), Speed (steps/s), Accel (steps/s^2), Posit'n (steps), Step time (ticks)\n"); maxSpeed = sqrt(minSpeed * minSpeed + stepsToGo * maxAccel); // Modified version of eq. 5
accelSteps = (long)(stepsToGo / 2);
}
goToPosition(positionToMoveTo); // STEPS 3 and 5
// Calculate initial value of and p1 and R Set p = p1
p1 = (float)ticksPerSec / sqrt( minSpeed * minSpeed + 2 * maxAccel); // Eq 17 incorporating initial velocity
p = p1;
R = (float) maxAccel / ((float)ticksPerSec * (float)ticksPerSec); // Eq 19
ps = ((float)ticksPerSec) / maxSpeed; // STEP 4 Eq 7 in paper
/* -------------------------------------------------------------- */ /* End of pre-computation code */
/* Start of pre-computation code - only executed once per profile */ /* -------------------------------------------------------------- */
// STEP 1 millisAtStart = millis(); /* Needed only to tabulate speed vs. time */
// Define number of steps in acceleration phase using Equation (3)
accelSteps = (long)(( maxPermissSpeed * maxPermissSpeed - minSpeed * minSpeed) / ( 2.0 * (float)maxAccel)); // Equation 4 but need to consider initial speed /* Timed loop for stepping, and associated coding */
stepsToGo = computeStepsToGo(); while(stepsToGo > 0)
maxSpeed = maxPermissSpeed; {
if (2 * accelSteps > stepsToGo) currentMillis = millis();
if (currentMillis - prevStepTime >= p)
{ {
// STEP 2 moveOneStep();
// Define maximum speed in profile and number of steps in acceleration phase prevStepTime = currentMillis;
// Use Equations (4) and (5) computeNewSpeed();
maxSpeed = sqrt(minSpeed * minSpeed + stepsToGo * maxAccel); // Modified version of eq. 5
accelSteps = (long)(stepsToGo / 2);
}
// STEPS 3 and 5
// Calculate initial value of and p1 and R Set p = p1
p1 = (float)ticksPerSec / sqrt( minSpeed * minSpeed + 2 * maxAccel); // Eq 17 incorporating initial velocity
p = p1;
R = (float) maxAccel / ((float)ticksPerSec * (float)ticksPerSec); // Eq 19
ps = ((float)ticksPerSec) / maxSpeed; // STEP 4 Eq 7 in paper
/* End of pre-computation code */
/* -------------------------------------------------------------- */
millisAtStart = millis(); /* Needed only to tabulate speed vs. time */
/* Timed loop for stepping, and associated coding */
while(stepsToGo > 0)
{
currentMillis = millis();
if (currentMillis - prevStepTime >= p)
{
moveOneStep();
prevStepTime = currentMillis;
computeNewSpeed();
}
} }
} }
return 0; return 0;

View File

@@ -2,7 +2,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <windows.h>
#include <math.h> #include <math.h>
#include <time.h> #include <time.h>
@@ -48,50 +47,45 @@ const float minSpeed = 1.0; // in steps/s
const float maxPermissSpeed = 20.0; // in steps/s const float maxPermissSpeed = 20.0; // in steps/s
const float maxAccel = 10.0; // in steps/s^2 const float maxAccel = 10.0; // in steps/s^2
int main() int main(int argc, char *argv[])
{ {
unsigned long currentMillis = millis(); unsigned long currentMillis = millis();
prevStepTime = 0; prevStepTime = 0;
long positionToMoveTo; long positionToMoveTo;
while(true) sscanf(argv[1], "%ld", &positionToMoveTo);
printf(" Time (s), Speed (steps/s), Accel (steps/s^2), Posit'n (steps), Step time (ticks)\n");
goToPosition(positionToMoveTo);
/* -------------------------------------------------------------- */
/* Start of pre-computation code - only executed once per profile */
float maxInterval = ((float)ticksPerSec) / minSpeed;
ps = ((float)ticksPerSec) / maxPermissSpeed;
deltaP = (maxInterval - ps) / accelSteps;
stepsToGo = computeStepsToGo();
maxSpeed = maxPermissSpeed;
if (2 * accelSteps > stepsToGo)
{ {
printf("Enter position to move to in profile (or 999 to terminate)\n"); accelSteps = (long)(stepsToGo / 2);
scanf("%ld", &positionToMoveTo); }
if (positionToMoveTo==999) break;
printf(" Time (s), Speed (steps/s), Accel (steps/s^2), Posit'n (steps), Step time (ticks)\n");
goToPosition(positionToMoveTo); p = maxInterval;
ps = ((float)ticksPerSec) / maxSpeed;
/* End of pre-computation code */
/* -------------------------------------------------------------- */
millisAtStart = millis(); /* Needed only to tabulate speed vs. time */
/* -------------------------------------------------------------- */ /* Timed loop for stepping */
/* Start of pre-computation code - only executed once per profile */ while(stepsToGo > 0)
{
float maxInterval = ((float)ticksPerSec) / minSpeed; currentMillis = millis();
ps = ((float)ticksPerSec) / maxPermissSpeed; if (currentMillis - prevStepTime >= p)
deltaP = (maxInterval - ps) / accelSteps;
stepsToGo = computeStepsToGo();
maxSpeed = maxPermissSpeed;
if (2 * accelSteps > stepsToGo)
{ {
accelSteps = (long)(stepsToGo / 2); moveOneStep();
} prevStepTime = currentMillis;
computeNewSpeed();
p = maxInterval;
ps = ((float)ticksPerSec) / maxSpeed;
/* End of pre-computation code */
/* -------------------------------------------------------------- */
millisAtStart = millis(); /* Needed only to tabulate speed vs. time */
/* Timed loop for stepping */
while(stepsToGo > 0)
{
currentMillis = millis();
if (currentMillis - prevStepTime >= p)
{
moveOneStep();
prevStepTime = currentMillis;
computeNewSpeed();
}
} }
} }
return 0; return 0;