Collaborate, Build, Learn

Zero

For background please take a look at the first Speedo post.

So I was all smug after the first post that I had finally found a rock solid solution to the speedo gauge mechanism. However during testing a problem came up and that was with the zero command.

The gauge is stateless which means when you first turn it on there is no way to know what position the needle is pointing. So I was sending a position 0 in the startup function to swing it back to inital position. In retospect this was stupid because it had no way of knowing where 0 was relative to where it was left last time it was switched off. After poking about, I found the commmand set in the Serial Gauge github readme and it has a zero command. Sending "!" down the serial line will trigger the gauge to step backwards the total number of steps passed in at setup (945 in my case). This means that no matter where you start the zero function will wind back to the endstop and then reset all the step counter values. Perfect except when I sent this command nothing happend.

I wrote this super simple sketch to test things out:

void setup() {
  Serial.begin(2400);
  delay(1000);
  Serial.println("!");
  delay(5000);
}

void loop() {
  Serial.println("260");
  delay(1000);
  Serial.println("500");
  delay(1000);
}

I emailed the developer, even though I bought the thing 3 years ago and much to my surprise he repsonded suggesting I reflash the firmware as the zero command was not in the very early builds. I followed the guide he sent and heeded his reminder to flash the bootloader. The process was a bit scary but worked and now at least it tried to zero when I sent the command. You could hear the motor straining but not move. I tried repeatedly sending "!" and every once it a while it would infact zero.

Looking at the SwitecX25 code I found the zero function:

void SwitecX25::zero()
{
  currentStep = steps - 1;
  for (unsigned int i=0;i<steps;i++) {
    stepDown();
    delayMicroseconds(RESET_STEP_MICROSEC);
  }
  currentStep = 0;
  targetStep = 0;
  vel = 0;
  dir = 0;
}

You can see it doesn't do much apart from repeatedly call stepDown() and zero the counters. The only thing to play with is the delay so I tried commenting it out.This seemed to stop the motor from even trying to move. If removing it made it worse then maybe increasing it would make it better. I set it from 800 to 1600 and indeed it would return to zero almost everytime. I then increased a bit further to 2000 and it seems to work everytime now. It does move quite slowly but I don't mind that at all.

Posted in Projects on Sep 05, 2017

Comments

Please sign in to comment!