Pages: [1]   Go Down
Author Topic: Timed Events  (Read 1153 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How long does the ramp up/down take, and how many events within that time period are required?
Logged

"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.

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8081
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void loop()
   {
   unsigned long currentTime = millis();
...
   if (rampingUp && currentTime-lastRampTime > rampInterval)
       {
      lastRampTime = currentTime;
       ramp up a step;
       if at full speed
             rampingUp = false;
       }
...
    }
     
Logged

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

New Hampshire
Offline Offline
God Member
*****
Karma: 13
Posts: 779
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You might want to look at (and possibly adapt) one of the libraries. Here's a useful one:

http://arduino.cc/playground/Code/TimedAction

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

@johnwasser I think that solution will actually work. Thinking this through, I think it is going to solve a number of issues. That simple little snip of pseudo-code has a lot of potential, and I really like how the various timing elements can be enabled or disabled. Thanks for the tip. Gonna have to read up on Millis.
Logged

Pages: [1]   Go Up
Jump to: