disabling with DMX

This commit is contained in:
Drake Marino 2024-10-30 22:17:04 -05:00
parent b92ed1a431
commit a2e8c50392

View File

@ -115,10 +115,7 @@ void setup() {
void loop() { void loop() {
if (DMXSerial.noDataSince() < 5000) {
targetPosition = (float)(DMXSerial.read(DMX_CHANNEL) - 101) / 100.0f; // 0 is disabled. position values are 1-201, 101 is center, <101 is CW, >101 is CCW. 1 & 201 are 360 degrees
digitalWrite(R_EN_PIN, HIGH);
digitalWrite(L_EN_PIN, HIGH);
if (micros() < prevSample) { if (micros() < prevSample) {
// micros timer rolled over, so reset sample timer: // micros timer rolled over, so reset sample timer:
@ -126,6 +123,12 @@ void loop() {
prevSample = micros(); prevSample = micros();
currentPosition = encoderToPosition(encoderPosition); currentPosition = encoderToPosition(encoderPosition);
} else if (micros() - prevSample >= SAMPLE_RATE) { } else if (micros() - prevSample >= SAMPLE_RATE) {
if (DMXSerial.noDataSince() < 5000) {
int dmxVal = DMXSerial.read(DMX_CHANNEL);
if (dmxVal != 0) {
targetPosition = (float)(dmxVal - 101) / 100.0f; // 0 is disabled. position values are 1-201, 101 is center, <101 is CW, >101 is CCW. 1 & 201 are 360 degrees
digitalWrite(R_EN_PIN, HIGH);
digitalWrite(L_EN_PIN, HIGH);
// this can happen faster than control loop so that (TBD) smoothing function can be added // this can happen faster than control loop so that (TBD) smoothing function can be added
prevPosition = currentPosition; prevPosition = currentPosition;
double dt = (float (micros() - prevSample)) / (1.0e6f); // time since last sample (s) double dt = (float (micros() - prevSample)) / (1.0e6f); // time since last sample (s)
@ -157,7 +160,6 @@ void loop() {
Serial.print(" error: ");Serial.print(remainingDistance); Serial.print(" error: ");Serial.print(remainingDistance);
Serial.println(); Serial.println();
*/ */
}
if (pid.Compute()) { if (pid.Compute()) {
// newPWM = motorPWM; // newPWM = motorPWM;
@ -207,11 +209,21 @@ void loop() {
// Serial.print(", mPWM: "); // Serial.print(", mPWM: ");
// Serial.println(motorPWM); // Serial.println(motorPWM);
} }
// make sure DMX is still alive:
} else { } else {
digitalWrite(R_EN_PIN, LOW); digitalWrite(R_EN_PIN, LOW);
digitalWrite(L_EN_PIN, LOW); digitalWrite(L_EN_PIN, LOW);
digitalWrite(LPWM_PIN, LOW);
digitalWrite(RPWM_PIN, LOW);
} }
} else {
digitalWrite(R_EN_PIN, LOW);
digitalWrite(L_EN_PIN, LOW);
digitalWrite(LPWM_PIN, LOW);
digitalWrite(RPWM_PIN, LOW);
}
}
// react to Serial input (new target position): // react to Serial input (new target position):
// if (recvWithEndMarker()) {targetPosition = readNewTarget();} // if (recvWithEndMarker()) {targetPosition = readNewTarget();}