View Single Post
  #29  
Old 10-14-2011, 01:24 PM
jpearman's Avatar
jpearman jpearman is offline
Senior Member
VEX # 8888
 
Join Date: Apr 2011
Location: Los Angeles
Posts: 3,104
Re: RobotC programming tips

In the last post we introduced AbortTimeSlice, RobotC also has the command EndTimeSlice which, with a little digging around in the system headers, turns out to do exactly the same thing as AbortTimeSlice.

For the next experiment Iím going to delete the AbortTimeSlice command completely and see what the result is, hereís the modified code.

Code:
// Flash the first LED
task flashLed_1()
{
    while( true )
        {
        // toggle output
        SensorValue[ LED_1 ] = 1 - SensorValue[ LED_1 ];
        }
}
And the waveform showing what the two LEDs are doing.



So not much changed but it is different from before. flashLed_1 did not voluntarily give control back to the task scheduler, however, the task scheduler was watching the clock and after a while said enough is enough and let the main task run. LED_2 controlled by the main task is still changing but not as consistently as before.

One more example for today, lets now make flashLed_1 a more important task, the correct terminology is a higher priority task. We do this by providing a second parameter to the StartTask command in the following way.

Code:
StartTask( flashLed_1, 10 );
Normal priority tasks are started with a priority of 7, if we provide a number which is higher then that task will have a higher priority (bigger number = more important).

Hereís the modified code.

Code:
// Main task - flash the second LED
task main()
{
    // Start the other task
    StartTask( flashLed_1, 10 );

    // Do our own thing
    while( true )
        {
        // toggle output
        SensorValue[ LED_2 ] = 1 - SensorValue[ LED_2 ];
        
        wait1Msec( 2 );
        }
}
And hereís the waveform.



You will notice that the main task (blue waveform) never runs, this is because the higher priority task does not give up control and therefore the task scheduler does not let the less important task run.

Thatís it for now, I will post a little more to complete this over the weekend.
Attached Images
File Type: jpg TEK00008.jpg (13.1 KB, 3151 views)
File Type: jpg TEK00009.jpg (16.5 KB, 3150 views)
Reply With Quote