Ramp Test
Ramp Test
h>
#include <Adafruit_PWMServoDriver.h>
int pulse_;
int sv_;
int last_pulse;
#define run_every(t) for (static uint16_t last_; \
(uint16_t)(uint16_t(millis()) - last_) >= (t); \
last_ += (t))
// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
//-----------------------------------------------------------
// sqr()
// square funtion
// it's a function not a macro like sq()
//-----------------------------------------------------------
float sqr(float a) {
return a * a;
}
float target[NumServos]; // target position
float p[NumServos]; // current position
float v[NumServos]; // velocity: (change in p per step)
float vmax[NumServos]; // max change in p per step
float amax[NumServos]; // max change in v per step
byte group[NumServos]; // 0 = move independently; 1..n = move together
#define SERVOMIN 150 // This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 600 // This is the 'maximum' pulse length count (out of 4096)
float g[NumServos];
float v1, a1, tmax, sign1, p0;
if (a1 == 0) {
v[servo] = (target[servo] - p[servo]) / tmax;
} else {
v1 = vmax[servo] * g[servo];
p0 = 2 * v[servo] + abs(v[servo]) * v[servo] / (2 * a1);
a1 = sign(target[servo] - p[servo] - p0) * a1;
if (a1 * (target[servo] - p[servo]) < 0) {
a1 = sign(a1) * abs(v[servo]) * v[servo] / (2 * (target[servo] -
p[servo]));
a1 = constrain(a1, -amax[servo], amax[servo]);
}
void setup() {
Serial.begin(9600);
Serial.println("4 channel Servo test!");
pwm.begin();
pwm.setOscillatorFrequency(27000000);
pwm.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates
for (byte servo = 0; servo < NumServos; servo++) {
p[servo] = InitialPosition[servo];
v[servo] = 0;
target[servo] = p[servo];
amax[servo] = 0.1;
vmax[servo] = 10.0;
pwm.setPWM(servo, 0, p[servo]);
group[servo] = 0;
}
delay(10);
vmax[0] = 200;
amax[0] = 0;
group[0] = 1;
vmax[1] = 200;
amax[1] = 0;
group[1] = 1;
vmax[2] = 200;
amax[2] = 1.0;
group[2] = 0;
vmax[3] = 200;
amax[3] = 0;
group[3] = 0;
vmax[4] = 200;
amax[4] = 1.0;
group[4] = 1;
vmax[5] = 200;
amax[5] = 1.0;
group[5] = 1;
}
// You can use this function if you'd like to set the pulse length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. It's not precise!
void loop() {
bool move_ = 0;
// Drive each servo one at a time using setPWM()
if (Serial.available()) {
String c = Serial.readString();
int ind = c.indexOf("s");
if (ind != -1) {
Serial.println("ind:" + String(ind) + String(c[ind + 1]));
int sv_ = c[ind + 1] - (char)('0');
int pulse_ = c.substring(ind + 2).toInt();
Serial.println("sv" + String(sv_) + " pos: " + String(pulse_));
pos[sv_] = pulse_;
move_ = 1;
//pwm.setPWM(sv_, 0, pulse_);
}
}