0
$\begingroup$

Rosanswers logo

Hi all,

I'm now building a robot which uses interrupt to count up the number of ticks and do the calculations in arduino nano, after that I decided to use rosserial library to send the number of ticks to the raspberry pi, when I put the code which send the number of ticks inside the void loop function it works, but if I but the code inside the interrupt handler I doesn't work very well and when it work it send strange numbers for example "6074229997415956480" number of ticks .

ISR(TIMER1_COMPA_vect){

    RTicks_msg.data = EncoderR.GetTicks();
    LTicks_msg.data = EncoderL.GetTicks();
    RTicksPup.publish(&RTicks_msg);
    LTicksPup.publish(&LTicks_msg);
    nh.spinOnce();
}

any help ..?

thanks,


Originally posted by jafar_abdi on ROS Answers with karma: 221 on 2016-12-28

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Generally, it is advised to keep vector routines as small as possible and keep delay functions away from it to avoid computational clashes. Might be the case of priority too. The interrupt section says about the ISR's that:

Generally, an ISR should be as short and fast as possible. If your sketch uses multiple ISRs, only one can run at a time, other interrupts will be executed after the current one finishes in an order that depends on the priority they have. millis() relies on interrupts to count, so it will never increment inside an ISR. Since delay() requires interrupts to work, it will not work if called inside an ISR. micros() works initially, but will start behaving erratically after 1-2 ms. delayMicroseconds() does not use any counter, so it will work as normal.

Might just be a case of timing issue, since spinOnce(); is being called.


Originally posted by kartikmadhira1 with karma: 121 on 2016-12-29

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by jafar_abdi on 2016-12-29:
It seem like what you said, do you have any idea about how I can send the ticks or another approach ..?

Comment by kartikmadhira1 on 2016-12-29:
Though I'm not an expert at this, you could declare a global volatile data type which both interrupts and the loop function can share. The basic idea is to just take in the ticks and use that ticks in the loop not in the ISR.

Comment by jafar_abdi on 2016-12-29:
The problem is to calculate the odometry I need the little interval dt if I loop it I'll not be able to know it or calculate it, whereas by using ISR I fixed the time interval to be 10ms, is there any way to calculate it when using loop ..?

$\endgroup$

Your Answer

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