Don't ad-block us - support your favorite websites. We have safe, unobstrusive, robotics related ads that you actually want to see - see here for more.
0 Members and 1 Guest are viewing this topic.
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart){ // this loop needs to execute once per 20ms. // my code in here. if (loopCount >= 49){ loopCount = 0; // is it ok to manually reset loopCount? // this code executes every 50 cycles. (ie, once per 50x20ms. (1 second)) // my code in here. } return (20000 + loopStart - clockGetus()); // 20000us == 20ms. Return 20ms after function starts.}
do{ breathe(); if(delay!=0){ while(!clockHasElapsed(loopStart,delay)){ // Allow interrupts to breathe breathe(); } } loopStart = clockGetus(); delay = appControl(++loopCount,loopStart); }while(1);
(hint to all you users of WebbotLib: time to give something back to Webbot in the form of HowTo documentation...)
my program needs a main function that lasts exactly 20ms.
TICK_COUNT lastTime; // the last loop time// Called once - so store the clockTICK_COUNT appInitSoftware(TICK_COUNT loopStart){ lastTime = loopCount;}TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart){ if(clockHasElapsed(lastTime, 20000)){ // 20ms mas elapsed since last processed // so do what you wanna do lastTime = loopCount; } return 0; // just keep calling me}
Can I reset loopCount
Can I change which hardware timer the system clock is using?
Just in defense of WebbotLib . . .Its so much easier to balance interrupts and timers and software PWM simultaneously using WebbotLib. You don't even need to really understand how any of it works to get something effective working.
1. Store the previous value of lastTime and check if you are belng called quicker/slower than this ieCode: [Select]TICK_COUNT lastTime; // the last loop time// Called once - so store the clockTICK_COUNT appInitSoftware(TICK_COUNT loopStart){ lastTime = loopCount;}TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart){ if(clockHasElapsed(lastTime, 20000)){ // 20ms mas elapsed since last processed // so do what you wanna do lastTime = loopCount; } return 0; // just keep calling me}
Or alternatively you could measure the lastTime and the loopCount and return a value to say 'pause for the remainder of 20ms'
Or even better: if you only want someting to happen every 20ms or so then use the scheduler and let it happend via interrupts