Quote:
|
Originally Posted by chakorules
What is the loop time for OperatorControl function?
See ZIP attached. This simple timing program should capture and measure the loop time of OperatorControl function and print that information to screen for me, instead every time the print-to-screen prints the variable count to screen, it increments 1,2,3,4,5, hits about 16, then it jumps to 1568,1569,1570 to something like 1700, then finally enters the If statement, prints the count variable one final time, resets the timer, resets the count = 0, then starts all over.
Can someone help set me straight?
|
Here's the problem... and this may not have been documented as well as it could have been. I noticed that your OperatorControl function just does a few statements, then exits.
When a program starts up the Initialize function is called first, then depending on the field controls (or dongle in the competition port) either the Autonomous function or OperatorControl function is called. When the OperatorControl function exits, the system just runs it again. It does this in case a program accidentally exits.
It is the intent (here's the part not documented well) that the programmer write an infinite loop in the OperatorControl function that never exits. So your program was running, getting the timer value and ending. Then the system restarted it.
So to write a program for the controller with EasyC you should put a loop into the OperatorControl function so it doesn't exit - and just keep reading the OI values, doing your stuff, and setting motor values.
The 26ms loop time that is a characteristic of the default code is changed quite a bit in EasyC. What happens is that EasyC will automatically get values from the OI and send motor every 26ms behind the scenes. So when you do a SetPWM repeatedly, the last value you set when the 26ms time comes up will be sent to the motors, even if you set it 100 times during that 26ms period.
The sensors (digital and analog IO) happens as fast as you can read/write data to those ports. There is no 26ms interval when reading or writing those.
If you want to write a PID loop and get the loop time for your integral term, a good way to do that is with a timer. It will count in 1ms increments and you can factor the time each time through the loop into your calculations. Another was is to only do the calculations at longer intervals - again a timer will help with that.
This may be confusing, especially after using the default code. If you have any other questions, I'll be happy to try to answer them.
Also, the PrintToScreen function calls the Microchip printf function which is pretty standard - you could look at the user guide that Microchip has. It should correctly print long variables, as you said, using the %ld directive.
You can also call printf yourself by putting it inside of an User Code block from the EasyC palette.