Robot Agent  1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
scheduler.c
Go to the documentation of this file.
1 
11 /* -- Includes -- */
12 /* system libraries */
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/time.h>
16 #include <unistd.h>
17 /* project libraries */
18 #include "scheduler.h"
19 #include "task.h"
20 #include "timelib.h"
21 
22 /* -- Defines -- */
23 
24 /* -- Functions -- */
25 
32 {
33  // Allocate memory for Scheduler structure
34  scheduler_t *ces = (scheduler_t *) malloc(sizeof(scheduler_t));
35 
36  return ces;
37 }
38 
45 {
46  // Free memory
47  free(ces);
48 }
49 
56 {
57  // Set timers
60 }
61 
68 {
69  int sleep_time; // Sleep time in microseconds
70 
71  // Calculate time till end of the minor cycle
72  sleep_time = (ces->minor * 1000) - (int)(timelib_timer_get(ces->tv_cycle) * 1000);
73 
74  // Add minor cycle period to timer
75  timelib_timer_add_ms(&ces->tv_cycle, ces->minor);
76 
77  // Check for overrun and execute sleep only if there is no
78  if(sleep_time > 0)
79  {
80  // Go to sleep (multipy with 1000 to get miliseconds)
81  usleep(sleep_time);
82  }
83 }
84 
91 void scheduler_exec_task(scheduler_t *ces, int task_id)
92 {
93  switch(task_id)
94  {
95  // Mission
96  case s_TASK_MISSION_ID :
97  task_mission();
98  break;
99  // Navigate
100  case s_TASK_NAVIGATE_ID :
101  task_navigate();
102  break;
103  // Control
104  case s_TASK_CONTROL_ID :
105  task_control();
106  break;
107  // Refine
108  case s_TASK_REFINE_ID :
109  task_refine();
110  break;
111  // Report
112  case s_TASK_REPORT_ID :
113  task_report();
114  break;
115  // Communicate
116  case s_TASK_COMMUNICATE_ID :
118  break;
119  // Collision detection
120  case s_TASK_AVOID_ID :
121  task_avoid();
122  break;
123  // Other
124  default :
125  // Do nothing
126  break;
127  }
128 }
129 
136 {
137  /* --- Local variables (define variables here) --- */
138 
139  /* --- Set minor cycle period --- */
140  //ces->minor = ...;
141 
142  /* --- Write your code here --- */
143 
144 }
#define s_TASK_NAVIGATE_ID
Definition: task.h:117
void scheduler_exec_task(scheduler_t *ces, int task_id)
Definition: scheduler.c:91
void task_control(void)
Definition: task_control.c:20
unsigned int minor
Definition: scheduler.h:25
struct timeval tv_started
Definition: scheduler.h:27
#define s_TASK_CONTROL_ID
Definition: task.h:118
scheduler_t * scheduler_init(void)
Definition: scheduler.c:31
double timelib_timer_get(struct timeval tv)
Definition: timelib.c:43
int timelib_timer_set(struct timeval *tv)
Definition: timelib.c:28
void task_communicate(void)
#define s_TASK_AVOID_ID
Definition: task.h:122
#define s_TASK_REFINE_ID
Definition: task.h:119
void scheduler_destroy(scheduler_t *ces)
Definition: scheduler.c:44
Scheduler structure.
Definition: scheduler.h:23
#define s_TASK_REPORT_ID
Definition: task.h:120
#define s_TASK_MISSION_ID
Definition: task.h:116
struct timeval tv_cycle
Definition: scheduler.h:28
void scheduler_wait_for_timer(scheduler_t *ces)
Definition: scheduler.c:67
void scheduler_run(scheduler_t *ces)
Definition: scheduler.c:135
void task_navigate(void)
Definition: task_navigate.c:20
void scheduler_start(scheduler_t *ces)
Definition: scheduler.c:55
void timelib_timer_add_ms(struct timeval *tv, unsigned int ms)
Definition: timelib.c:90
void task_mission(void)
Definition: task_mission.c:20
void task_refine(void)
Definition: task_refine.c:20
void task_report(void)
Definition: task_report.c:20
#define s_TASK_COMMUNICATE_ID
Definition: task.h:121
void task_avoid(void)
Definition: task_avoid.c:20