Products > Embedded Computing

Why? Arduino Servo. Pot. LED

<< < (4/4)

sokoloff:
Another approach, designed to keep the latency through the loop() function constant is to move a maximum of a fixed number of steps in the servo positioning function:

--- Code: ---//#define USE_HYSTERESIS
#define HYSTERESIS_X 1
#define MAX_STEP_X 1

void servo1x(int pos) {
#ifdef USE_HYSTERESIS
  if (constrain(pos, pos1-HYSTERESIS_X, pos1+HYSTERESIS_X) == pos) {
    digitalWrite(13, LOW);
    return;
  }
#endif
  pos  = constrain(pos, servo1start, servo1stop);  // Constrain to servo limits
  if (pos == pos1) {             // Nothing to do but turn off the LED
    digitalWrite(13, LOW);
    return;
  }
  pos1 = constrain(pos, pos1-MAX_STEP_X, pos1+MAX_STEP_X); // Constrain to max of MAX_STEP_X away from current position.
  digitalWrite(13, HIGH);           // turn the LED on (HIGH is the voltage level)
  servo1.write(pos1);             // tell servo to move towards target position passed in variable 'pos'
}

--- End code ---
And move the delay into the loop function:

--- Code: ---void loop() {
  // Do whatever you're doing now

  // Read the pot

  // Move Servo X
  servo1x(pot);

  // Update LCD

  // Whatever else
 
  // Add a delay *only* once in loop:
  delay(dlyx);
}

--- End code ---
This makes the system more responsive when the servo has a long commanded move. It also allows "diagonal movement" for a system with multiple servos. (Basically, the X motion happens continuously but only one "step" at a time each time through the loop and the LCD keeps updating, the pot is still being read, if there are other servos, they can also be moving at the same time, etc.)

The hysteresis processing code is designed to reject a small amount of noise in the pot measurement (at the expense of final accuracy). It's optional and not compiled in in the sample code above.

metrologist:

--- Quote from: ebastler on February 10, 2019, 07:53:17 pm ---
--- Quote from: metrologist on February 10, 2019, 06:31:58 pm ---
--- Quote from: ebastler on February 10, 2019, 04:09:15 pm ---Are you saying you just realized this, or knew it all along and posted this as a little quiz?  ::)

--- End quote ---

I think there is no good answer to this kind of question, but I wouldn't attempt to quiz the forum.

--- End quote ---

Hmm, I indeed think there would have been a "good answer".

You asked a question to the forum, I suggested an answer, you (instead of saying "thanks") responded "Oh, I think it's simple". Which makes me wonder whether you were feeling the need to explain something to me, namely the solution to a little quiz you had devised.

The "good answer" would have been: "No, I was really at a loss, but this may indeed be the root cause. Thank you."

--- End quote ---

Oh, sorry. I don't seem to be much good at anything. It was very much conversational and unfolding as we spoke, so to say... OK, yes, I just realized it as I posted, or as you posted. I guess an engineer would have known as readily as one would know to put on their pants.

Spikes on the ground line was not what I was thinking, but thanks. That might actually be what is going on rather than a weak sagging regulator, but I don't know. I was just perplexed there for a sec as I twisted the servo and the dac reading on the LCD responded proportionately along with the LED.

metrologist:

--- Quote from: sokoloff on February 10, 2019, 10:33:29 pm ---Another approach, designed to keep the latency through the loop() function constant is to move a maximum of a fixed number of steps in the servo positioning function:

--- End quote ---

OK, that's interesting and I'll have to digest it and see how it works. I know my coding style is still very simpleton. This code was pieced together as a quick esc test. The servo function had a delay to control the servo speed. It was for a robot that all worked in tandem, but the function is blocking so nothing else works well with it.

I've been trying to think of the micro as spinning very fast and looking for a way to trigger shorter commands, such as every second check thermistor, every n seconds move servo one step, only write the part of the LCD when it changes, and put a timer on the marching time dot so it works with a RTC, etc. It seems you could have the effect of multitasking.

I ordered 20 servos for another 4 leg robot project so there will be time to explore this soon.

sokoloff:

--- Quote from: metrologist on February 11, 2019, 07:49:38 am ---OK, that's interesting and I'll have to digest it and see how it works. I know my coding style is still very simple.
--- End quote ---
"Simple and working" or "simple and making progress" beats "complicated and not working" or "never written" every day of the week, so don't sweat it a bit.

Sorry for so many followup posts above, but I first focused on the micro problem (no pun intended) and only later thought "hey, if he's calling it 'servo-x', that probably means there's another servo in the mix and he probably doesn't want blocking movement"

metrologist:
Well, your thought pretty much is spot on...

I called the function servo1x because servo1 caused an error that took me forever to discover. I already declared servo1 as the servo object.

This was actually a two-servo robot and the servos worked in sequence rather than tandem like I was thinking.

Navigation

[0] Message Index

[*] Previous page

There was an error while thanking
Thanking...
Go to full version