generating ~1kHz interrupt without altering PWMs or millis()
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
-
- Posts: 6
- Joined: Thu Mar 20, 2014 1:34 pm
generating ~1kHz interrupt without altering PWMs or millis()
I am wanting to track speed of several motors using 1 pulse per rev and need to use 3 PWMs. I am using the default pwm frequency of ~1kHz but higher would be fine. But generating about 1 kHz (not critical) interrupt to get counts (milliseconds) per rev seems difficult to set up. I even considered connecting a PWM output back to an interrupt input and setting to interrupt on one edge. Would prefer not to change millis() timing but I could adjust values for that.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: generating ~1kHz interrupt without altering PWMs or mill
You just need to set up a compare interrupt on your PWM. It won't interfere with the regular millisecond timer.
Which arduino are you using?
Which PWM pins?
Which arduino are you using?
Which PWM pins?
-
- Posts: 6
- Joined: Thu Mar 20, 2014 1:34 pm
Re: generating ~1kHz interrupt without altering PWMs or mill
"Just set up a compare interrupt..."
I need to figure out how to do that. Any more detail on that?
This is for Uno board.
I am using PWM from pins 9, 10, and 11. Pins 9 and 10 are the motors I need to monitor speed on. Basically I want to increase PWM to motors if they slow down or stall so they keep moving when loaded but don't go too fast when lightly loaded. I don't need precise speed control but just prevent stalling.
I need to figure out how to do that. Any more detail on that?
This is for Uno board.
I am using PWM from pins 9, 10, and 11. Pins 9 and 10 are the motors I need to monitor speed on. Basically I want to increase PWM to motors if they slow down or stall so they keep moving when loaded but don't go too fast when lightly loaded. I don't need precise speed control but just prevent stalling.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: generating ~1kHz interrupt without altering PWMs or mill
Wait - How will a PWM interrupt help you with this? It won't tell you that the motors are slowing down. The interrupt will occur at a constant rate regardless of what the motors are doing.jim27 wrote:Basically I want to increase PWM to motors if they slow down or stall so they keep moving when loaded but don't go too fast when lightly loaded.
-
- Posts: 6
- Joined: Thu Mar 20, 2014 1:34 pm
Re: generating ~1kHz interrupt without altering PWMs or mill
I am not sure how a PWM interrupt will help... I am a bit confused by the timers and interrupts, especially how they are programmed by default for arduino and what side effects reprogramming them might have. I had thought that an interrupt on PWM timer rollover might work for the 8 bit counter and be approx 1 kHz, the same as the PWM frequency but I am not sure about that.
Here's more explanation of what I planned to do. It may not be the best but it seemed (reasonably) simple since I don't have much experience with arduino.
The gear motors have 1 pulse per rev output sensors. Max speed is about 200 rpm. This translates to about 300 mS per pulse. If I have a 1 mS interrupt and check the level of the pulse in the interrupt routine and increment during the pulse I will have the time of the pulse (within a millisecond which is plenty good).
The issue as I see it is that I need to use the PWMs and use the timers for interrupts at the same time. If I can't do it internally then I could connect the PWM pin to an interrupt and get interrupts at the PWM rate, which is close to 1 mS and close enough.
I have seen info on reprogramming the timers but it typically causes issues for the PWM and millis() functions and seems more complicated than needed.
Here's more explanation of what I planned to do. It may not be the best but it seemed (reasonably) simple since I don't have much experience with arduino.
The gear motors have 1 pulse per rev output sensors. Max speed is about 200 rpm. This translates to about 300 mS per pulse. If I have a 1 mS interrupt and check the level of the pulse in the interrupt routine and increment during the pulse I will have the time of the pulse (within a millisecond which is plenty good).
The issue as I see it is that I need to use the PWMs and use the timers for interrupts at the same time. If I can't do it internally then I could connect the PWM pin to an interrupt and get interrupts at the PWM rate, which is close to 1 mS and close enough.
I have seen info on reprogramming the timers but it typically causes issues for the PWM and millis() functions and seems more complicated than needed.
- adafruit_support_bill
- Posts: 88087
- Joined: Sat Feb 07, 2009 10:11 am
Re: generating ~1kHz interrupt without altering PWMs or mill
It sounds like you are doing it the hard way. Instead of using an interrupt to poll the pulse, you should use the pulse to generate an interrupt.The gear motors have 1 pulse per rev output sensors. Max speed is about 200 rpm. This translates to about 300 mS per pulse. If I have a 1 mS interrupt and check the level of the pulse in the interrupt routine and increment during the pulse I will have the time of the pulse (within a millisecond which is plenty good).
You can connect your motor's pulse output to one of the Arduino's external interrupt pins and configure it for rising or falling edge interrupts. Then you just need to check millis() in each interrupt to figure out the time.
http://arduino.cc/en/Reference/attachInterrupt
-
- Posts: 6
- Joined: Thu Mar 20, 2014 1:34 pm
Re: generating ~1kHz interrupt without altering PWMs or mill
Good point. Minor issue is that I still need to worry about the stalled case when no interrupts occur to update speed. I could save the current millis() value on each interrupt and check in the main loop for no or very slow speed by comparing to the current millis() value. If too much time passes with no update I know it is stalled. Thanks!
Please be positive and constructive with your questions and comments.