I have just merged the ChibiOS library ChRt onto my arduino libraries, and the library works fine, I was hoping that I will now be able to set the execution time for a task, as required by a real time system that a task must execute within a finite time. The code in the documantation that come close to this definition is below here, as it atleast say do _something(); should have been completed before the chThdSleepUntil(time) line, but this still does not address the issue, as acording to the documentation, it is possible that if the do_something(); execution time exceeds the set time for ChThdSleep(); the thread "would stay sleeping into chThdSleepUntil() until the (very far) specified instant in the future."
msg_t my_thread(void *param) {
systime_t time = chVTGetSystemTimeX(); // T0
while (true) {
time += MS2ST(1000); // Next deadline
do_something();
chThdSleepUntil(time);
}
}
Now my question is how to I set the deadline for the do_something(); ?
ChibiOS/RT
- Giovanni
- Site Admin
- Posts: 14444
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1074 times
- Been thanked: 921 times
- Contact:
Re: ChibiOS/RT
Hi,
Look into this function:
It allows to specify a time range, the sleep ends if the time is out of the "window", so if your do_something() exceeds the interval then the sleep is not performed.
Examples here: http://www.chibios.org/dokuwiki/doku.ph ... _threading
Giovanni
Look into this function:
Code: Select all
/**
* @brief Suspends the invoking thread until the system time arrives to the
* specified value.
* @note The system time is assumed to be between @p prev and @p next
* else the call is assumed to have been called outside the
* allowed time interval, in this case no sleep is performed.
* @see chThdSleepUntil()
*
* @param[in] prev absolute system time of the previous deadline
* @param[in] next absolute system time of the next deadline
* @return the @p next parameter
*
* @api
*/
systime_t chThdSleepUntilWindowed(systime_t prev, systime_t next)
It allows to specify a time range, the sleep ends if the time is out of the "window", so if your do_something() exceeds the interval then the sleep is not performed.
Examples here: http://www.chibios.org/dokuwiki/doku.ph ... _threading
Giovanni
Re: ChibiOS/RT
Thank you very much! I appreciate your response. I will look more in the literature on Real-Time programming
It allows to specify a time range, the sleep ends if the time is out of the "window", so if your do_something() exceeds the interval then the sleep is not performed.
My main concer is how do we restrict the do something(); ececution time to a finite number of choice, say 10ms for example.
It allows to specify a time range, the sleep ends if the time is out of the "window", so if your do_something() exceeds the interval then the sleep is not performed.
My main concer is how do we restrict the do something(); ececution time to a finite number of choice, say 10ms for example.
- Giovanni
- Site Admin
- Posts: 14444
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1074 times
- Been thanked: 921 times
- Contact:
Re: ChibiOS/RT
Hi,
You need to add a check into your function internal loop and see if it should be interrupted. You can use this to keep a loop within a time window, add it to your conditions:
Giovanni
You need to add a check into your function internal loop and see if it should be interrupted. You can use this to keep a loop within a time window, add it to your conditions:
Code: Select all
/**
* @brief Checks if the current system time is within the specified time
* window.
* @note When start==end then the function returns always false because the
* time window has zero size.
*
* @param[in] start the start of the time window (inclusive)
* @param[in] end the end of the time window (non inclusive)
* @retval true current time within the specified time window.
* @retval false current time not within the specified time window.
*
* @api
*/
static inline bool chVTIsSystemTimeWithin(systime_t start, systime_t end) {
return chTimeIsInRangeX(chVTGetSystemTime(), start, end);
}
Giovanni
Re: ChibiOS/RT
Another way to do what you want is to have a thread which waits on either an event (the thread-specific event flags are easiest to understand) or a semaphore.
Then set up a regular 'tick' from either one of the hardware timers (suggested if you are using short intervals; <100msec, say) or the Chibi event timer; the 'tick handler' simply sets the event flag or semaphore.
This avoids some of your concerns about the task overrunning, since the flag to trigger the next processing happens regardless. If it is critical that your task runs exactly once per tick, use a counting semaphore to trigger it. If you can live with missing the odd trigger should the task overrun, use the event flags.
Then set up a regular 'tick' from either one of the hardware timers (suggested if you are using short intervals; <100msec, say) or the Chibi event timer; the 'tick handler' simply sets the event flag or semaphore.
This avoids some of your concerns about the task overrunning, since the flag to trigger the next processing happens regardless. If it is critical that your task runs exactly once per tick, use a counting semaphore to trigger it. If you can live with missing the odd trigger should the task overrun, use the event flags.
Who is online
Users browsing this forum: No registered users and 9 guests