8
$\begingroup$

We hope to build a simple line follower robot and we got a problem when we were discussing about PIC programming.

We planed to write a endless loop, check the sensor panel reading and do the relevant stuff for that reading.

But one of our friends told us to use a timer interrupt to generate interrupts in definite time periods and in each interrupt check the sensor panel reading and do the relevant stuff for that reading.

But we can't figure out which is best: the endless loop in main method OR timer interrupt method.

What is the best way, and why?

$\endgroup$
1
  • 1
    $\begingroup$ You forgot to ask "is there a best way?" For a simple system it's sometimes best to use the timer hardware to set a flag, then run your control loop when the flag is set. For a complex system, ISRs (and often some sort of OS kernel or at least a task-switcher) is often best. $\endgroup$
    – TimWescott
    Commented Aug 20, 2013 at 18:11

6 Answers 6

8
$\begingroup$

The processor has to execute something. You will always have an "endless" loop even if you're doing some work in an interrupt handler.

The best solution depends on exactly what you're trying to do.

The main advantage of using interrupts is they allow you to service events in real-time while your main program is doing something else. Timer interrupts specifically allow you to perform some periodic activity on a fairly accurate time-base while doing some other processing.

For most control algorithms it is useful to have some sort of a time-base (or fixed frequency). If that's all your CPU is doing you can easily do that without interrupts by either controlling the number of cycles/instructions in your control loop or polling the timer. Both methods would be simpler and more efficient than using interrupts. Interrupts are more useful in more complex systems doing many things concurrently (I/O, time keeping, scheduling etc.).

If you go with the endless loop without polling the timer or calibrating the time it takes to run one iteration of the loop you will have to be careful of situations where adding more code in your loop changes the timing and the behavior of your control system.

If you implement your control algorithm in a timer interrupt you will have to be careful you're not trying to perform more work than the CPU can handle in one time "slot" or that will again impact the behavior of your control system.

$\endgroup$
4
$\begingroup$

Further to Guy's answer, using a timer interrupt to generate accurate intervals adds determinacy.

For example, at any given time, you know exactly what the processor is doing, and all activities are performed at specified rates.

You also should schedule at determined rates as otherwise you have no basis for time constants and such like, which will be essential in your filtering.

And finally, this means that if you add additional tasks, then the existing tasks are not slowed down.


Simple polling is OK if that is all your system has to do - but while time slot scheduling is a minor overhead in a trivial system, adding it from the start allows growth and expansion without having to restructure everything later.

$\endgroup$
1
  • $\begingroup$ In some situations you can get more deterministic performance out of a loop than you can from interrupts. Down to a cycle of jitter. If all you're doing is one thing... $\endgroup$
    – Guy Sirton
    Commented Aug 19, 2013 at 21:17
4
$\begingroup$

I would just like to add few points to other posts. My personal opinion is that endless while loop has no sense in systems which read from snsors or write to actuators as both has some physical constrains, eg. There is no point in reading from sensors at 1Mhz if the sensor has stability cycle before meassurent is accurate at 100Hz. Same goes to motor drivers. You can send 1mil instructiins to move motor but it can only witstand its physical limitations in respect of inertia.

What i do in my designs is following. I create timer interrupt which triggers every 1ms or so, and then i decrement some global variables in that interrupt routine. In main while loop i check if variable is 0 and execute command that is defined by that variable. In that case i can drive motor at 125Hz, read sensor at 20Hz, and output leds at 0.5Hz.

I hope you got the point.

$\endgroup$
3
$\begingroup$

I'd also like to stress that the comparison between polling (i.e. checking if a interrupt flag has been set) and interrupt vectors (i.e. an ISR, or function that's loaded when a interrupt has occurred) should be more than just evaluating the efficiency/simplicity of executing a block of reactionary code. The interrupt controllers of most modern micros are capable of so much more, especially when considering inter-peripheral signalling (ie, DMA).

For example, a Texas Instruments TMS320F28335 microcontroller allows you to trigger your ADC's start of conversion pulse directly from a PWM's rising or falling edge. In addition to this, the micro also has a six channel DMA controller, which for instance can also be programmed to trigger off the same PWM signal (this could allow you to start a memory read via the devices' parallel bus, that's for instance connected to a high speed delta sigma converter). The same DMA controller can then be used to buffer the values read from the ADC and external delta sigma to internal RAM. The DMA controller can then be configured to only trigger a normal interrupt service routine after sampling a total of lets say 128 ADC and delta sigma readings. All this would happen without any CPU intervention (freeing you up to do other things).

This type of behind-the-scenes triggering allows you to get a lot more work done compared to when you would have had an ISR controlling all the enable flags or memory reads (due to the overhead associated with interrupt latency). The vector chained approach would allow you to sample at (let's say) 50 KHz, where an ISR driven approach would only allow you to sample at (let's say) 5 KHz.

Also as Andrew stated, using interrupt vectors are much more deterministic, or repeatable, than polling a status register.

I'd also highly recommend subscribing to Phil Koopman's blog, or at least reading the following posts:

$\endgroup$
1
$\begingroup$

Old post, still answering for other people's reference.

I am working on a similar project. Even to me a friend had suggested to use timer.

Finally my suggestion would be to use as high speed controller as possible (mine is 80MHz). Use 5us Timer Interrupt (Scheduler). Do the following in this Scheduler Interrupt Routine:

  1. CheckSensors()
  2. ClockActuators() (if using Steppers then make all the Clk pins low initially and then high the Clk for whichever motor needs to run)
  3. StateChange() / ApplicationRoutine() / SwitchContext() (for more info on State Machine refer: https://barrgroup.com/Embedded-Systems/How-To/State-Machines-Event-Driven-Systems) (to be frank, could not digest the ending part of the article but got the idea. I am using simple Switch Case in StateChange().)

In main()'s while(1) loop:

  1. Check Input from Keypad / Touch Screen / Serial Comm etc.
  2. Update Display (If this is too long and makes Keypad response slow then try to put the above step i.e checking keypad/touch/serial in interrupt.)
$\endgroup$
1
0
$\begingroup$

Though using polling or Interrupt won't cause any significant change in a simple line follower robot, but it's often good to use Interrupts.

Though Timer Interrupts are very well used in a number of cases but I would suggest, in this case, that you can better use a simple Interrupt which gets triggered when a specific input comes from the sensor (like white colour detected, and sensor gives a logic high). Doing this you will be sure about what exactly your microcontroller is doing when it encounters the white patch. Also, there is least delay between getting the input and performing the corresponding output.

Though, in the case of simple line follower you can just leave interrupts and continue with polling. But if you are multitasking with your robot (like having multiple sensors for actuating various things) you will probably need interrupts. One more benefit is that you can set up the priority for these events in interrupts. Like what would happen if you are doing task A and and an interrupt comes and asks for doing task B. You can make one of them more important by setting one of them to higher priority. Doing this, the action with more priority will be completed first.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.