i'm working on a relatively complicated program and i'm pushing up against the edge of what i understand. i'm working on an experimental arduino firmware for RepRap to control the speed of the extruder motor as an RPM (it basically needs to go a constant speed by sensing the actual rotational speed and compensating for any friction, etc.) here's the setup:
* a dc motor, controlled via PWM
* a rotary encoder, read via quadrature encoding using pins 2/3 and an onchange interrupt
* various stepper drivers, etc.
* interrupt driven quadrature reading which sets a 'position error variable' for extruder motor. forward is positive.
* timer1 interrupt to decrement the 'position error variable' at regular intervals (this is how we get the constant speed)
* timer2 interrupt to measure the 'position error variable' at a regular interval and apply a PID algorithm to control the speed
anyway, it works great... when its just controlling the extruder speed (and nothing else is happening). however, as soon as i tell the printer to move the extruder head, it freaks out, and occasionally the firmware resets. after reading through the arduino library source last night, i think it may be a problem with the delayMicroseconds() function in the stepper movement function.
the way the stepper movement works is that it calculates a delay between steps, as well as the number of steps to take, then takes that number of steps with the appropriate delay between them. generally this delay is between 600 and 3000 usecs. the function uses the delayMicroseconds() function to wait. from reading through the function, it appears that it disables interrupts during the delay function.
so, i need a workaround. i need delays with microsecond precision, but i need them to be able to be interrupted. the interrupts should be pretty quick, so i'm not super worried about the speed.
can i do something like this: (and how much extra time would it use?)
for (i=0; i<delay_microseconds; i++)
or, should i write my own delayMicrosInterruptible() that doesnt disable the interrupts?
the code in question is all located here: http://svn.reprap.org/trunk/reprap/firmware/Arduino/GCode_Interpreter_Experimental/