Go Down

Topic: Timed Events (Read 1 time) previous topic - next topic

Mr_Fixit

Best place to explain this is to describe what I am doing. My robot has an MD25 motor controller, and due to the structure of this robot, I have found that I need to speed up and speed down the motors or else I have an issue with balance. A sudden start or stop can cause bad things to happen. So, I decided it is prudent to ramp up and ramp down the motor speed. To do this best, I would typically have a timed event so that other processing can happen and then trigger the updates to occur at set intervals.

I have looked at some of the functions that may be able to help with this, but most of them only work if you trigger a check, which kinda defeats the intent here because there is no telling what other functions may be running at the time. The other thing is that this timed event only needs to happen DURING the ramp up or ramp down time frames.

I had hoped there could be some threaded function that can be activated and deactivated, but dont think Arduinos support threading... Anyway, suggestions are very welcome and appreciated.

The simple solution is to have a loop with a delay(x) with x being whatever the interval should be. However I dont want to halt other processes while ramping.

AWOL

How long does the ramp up/down take, and how many events within that time period are required?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

johnwasser

No threads but there are timers and timer interrupts.

Another possibility, if you can avoid the 'delay' functions, is to have events scheduled in loop():

Code: [Select]

void loop()
  {
  unsigned long currentTime = millis();
...
  if (rampingUp && currentTime-lastRampTime > rampInterval)
      {
     lastRampTime = currentTime;
      ramp up a step;
      if at full speed
            rampingUp = false;
      }
...
   }

     
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

jraskell

delay() is almost NEVER the simple solution.  The reality is, most of the time it causes more problems than it tries to solve (and rarely even solves the problem it was being used to solve).

If you're building even a remotely complicated robot, then you need to familiarize yourself with timing your own events, as johnwasser explains above.  millis() is your friend, and delay() is your enemy.

Mr_Fixit

That really is variable. I wrote the code so that the ramp step can be adjusted. So, number of steps = 128/StepSize. Time between steps should be about 150-200ms. I dont have firm answers yet because I am still working on the mechanical aspects of the robot.

Try to picture this in your head... hope this description is not too horrid.
The robot body is one of those large rat balls (Put the rat in, it rolls around the floor). There is an axel through the middle and a motor on the axel drives the ball. Most the electronics and battery reside on a tray that swings left and right to provide steering. There is a support arm that forms an arc across the top and on top of that are a number of sensors. The support arm is fixed to the axel, and the tray with electronics provides counter weight to keep the arm upright. Too much acceleration will either cause the sensor arm to crash into the floor, or cause the bot to swing wildly.


Go Up