Advertisement
Guest User

TP1f

a guest
Mar 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. /* helloworld.c for TOPPERS/ATK(OSEK) */
  2. #include "kernel.h"
  3. #include "kernel_id.h"
  4. #include "ecrobot_interface.h"
  5.  
  6. #define seuil 600
  7. #define lightsensor NXT_PORT_S2
  8. #define sonarsensor NXT_PORT_S1
  9. #define touchsensor NXT_PORT_S3
  10. #define motorl NXT_PORT_B
  11. #define motord NXT_PORT_A
  12. #define etatchercheadroite 0
  13. #define etatchercheagauche 1
  14.  
  15. DeclareCounter( SysTimerCnt);
  16. DeclareAlarm( Alarme_acq);
  17. DeclareAlarm( Alarme_acqultrason);
  18. DeclareAlarm( Alarme_acqchoc);
  19. DeclareAlarm( Alarme_afficher);
  20. DeclareTask( AcqInfra);
  21. DeclareTask( SuivrePiste);
  22. DeclareTask( AcqUltrason);
  23. DeclareTask( AcqChoc);
  24. DeclareTask( Afficher);
  25. DeclareResource( Piste);
  26. DeclareResource( AvanceRecule);
  27.  
  28. typedef struct {
  29. int surPiste; // vaut 1 si sur la piste et 0 sinon
  30. } type_SurPiste;
  31. static volatile type_SurPiste donneePiste;
  32. static int etatcourant = 0;
  33. static volatile int ultrason = 1, choc = 1;
  34. static int coulavt, chocvalue, sonarvalue;
  35.  
  36. /* nxtOSEK hook to be invoked from an ISR in category 2 */
  37. void user_1ms_isr_type2(void) {
  38. StatusType ercd;
  39. /* Increment System Timer Count */
  40. ercd = SignalCounter(SysTimerCnt);
  41. if (ercd != E_OK) {
  42. ShutdownOS(ercd);
  43. }
  44. }
  45.  
  46. void ecrobot_device_initialize(void) {
  47. ecrobot_set_light_sensor_active(lightsensor);
  48. ecrobot_init_sonar_sensor(sonarsensor);
  49. }
  50.  
  51. TASK( AcqInfra) {
  52. coulavt = ecrobot_get_light_sensor(lightsensor);
  53. int surpiste;
  54. static int compteur = 1, limite = 5;
  55. if (coulavt > seuil) {
  56. surpiste = 1;
  57. compteur = 0;
  58. limite = 0;
  59. } else {
  60. surpiste = 0;
  61. if (compteur == limite) {
  62. compteur = 0;
  63. if (etatcourant == etatchercheadroite)
  64. etatcourant = etatchercheagauche;
  65. else if (etatcourant == etatchercheagauche)
  66. etatcourant = etatchercheadroite;
  67. limite += 5;
  68. }
  69. compteur++;
  70. }
  71. GetResource(Piste);
  72. donneePiste.surPiste = surpiste;
  73. ReleaseResource(Piste);
  74. ChainTask(SuivrePiste);
  75. }
  76.  
  77. TASK( SuivrePiste) {
  78. int surpiste = 0;
  79. GetResource(Piste);
  80. surpiste = donneePiste.surPiste;
  81. ReleaseResource(Piste);
  82. int avrechoc, avreultra;
  83. GetResource(AvanceRecule);
  84. avrechoc = choc;
  85. avreultra = ultrason;
  86. ReleaseResource(AvanceRecule);
  87. if (surpiste == 1) {
  88. if (avrechoc == 0 && avreultra == 0) {
  89. nxt_motor_set_speed(motorl, 70, 0);
  90. nxt_motor_set_speed(motord, 70, 0);
  91. } else {
  92. nxt_motor_set_speed(motorl, -70, 0);
  93. nxt_motor_set_speed(motord, -70, 0);
  94. }
  95. } else if (etatcourant == etatchercheadroite) {
  96. if (avrechoc == 0 && avreultra == 0) {
  97. nxt_motor_set_speed(motorl, 40, 1);
  98. nxt_motor_set_speed(motord, -40, 1);
  99. } else {
  100. nxt_motor_set_speed(motorl, -40, 1);
  101. nxt_motor_set_speed(motord, 40, 1);
  102. }
  103. } else if (etatcourant == etatchercheagauche) {
  104. if (avrechoc == 0 && avreultra == 0) {
  105. nxt_motor_set_speed(motorl, -40, 1);
  106. nxt_motor_set_speed(motord, 40, 1);
  107. } else {
  108. nxt_motor_set_speed(motorl, 40, 1);
  109. nxt_motor_set_speed(motord, -40, 1);
  110. }
  111. }
  112. TerminateTask();
  113. }
  114.  
  115. TASK( AcqUltrason) {
  116. sonarvalue = ecrobot_get_sonar_sensor(sonarsensor);
  117. if (sonarvalue <= 30) {
  118. GetResource(AvanceRecule);
  119. ultrason = 1;
  120. ReleaseResource(AvanceRecule);
  121. } else {
  122. GetResource(AvanceRecule);
  123. ultrason = 0;
  124. ReleaseResource(AvanceRecule);
  125. }
  126. TerminateTask();
  127. }
  128.  
  129. TASK( AcqChoc) {
  130. chocvalue = ecrobot_get_touch_sensor(touchsensor);
  131. if (chocvalue == 1) {
  132. GetResource(AvanceRecule);
  133. choc = 1;
  134. ReleaseResource(AvanceRecule);
  135. } else if (chocvalue == 0) {
  136. GetResource(AvanceRecule);
  137. choc = 0;
  138. ReleaseResource(AvanceRecule);
  139. }
  140. TerminateTask();
  141. }
  142.  
  143. TASK( Afficher) {
  144. display_clear(0);
  145. display_goto_xy(7, 1);
  146. display_int(coulavt, 3);
  147. display_goto_xy(7, 3);
  148. display_int(chocvalue, 3);
  149. display_goto_xy(7, 5);
  150. display_int(sonarvalue, 3);
  151. display_update();
  152. TerminateTask();
  153. }
  154.  
  155. ===================================================================
  156.  
  157. #include "implementation.oil"
  158.  
  159. CPU ATMEL_AT91SAM7S256
  160. {
  161. OS LEJOS_OSEK
  162. {
  163. STATUS = EXTENDED;
  164. STARTUPHOOK = FALSE;
  165. ERRORHOOK = FALSE;
  166. SHUTDOWNHOOK = FALSE;
  167. PRETASKHOOK = FALSE;
  168. POSTTASKHOOK = FALSE;
  169. USEGETSERVICEID = FALSE;
  170. USEPARAMETERACCESS = FALSE;
  171. USERESSCHEDULER = FALSE;
  172. };
  173.  
  174. /* Definition of application mode */
  175. APPMODE appmode1{};
  176.  
  177. COUNTER SysTimerCnt
  178. {
  179. MINCYCLE = 1;
  180. MAXALLOWEDVALUE = 10000;
  181. TICKSPERBASE = 1;
  182. };
  183.  
  184. ALARM Alarme_acq
  185. {
  186. COUNTER = SysTimerCnt;
  187. ACTION = ACTIVATETASK
  188. {
  189. TASK = AcqInfra;
  190. };
  191. AUTOSTART = TRUE
  192. {
  193. ALARMTIME = 5;
  194. CYCLETIME = 20;
  195. APPMODE = appmode1;
  196. };
  197. };
  198.  
  199. ALARM Alarme_acqultrason
  200. {
  201. COUNTER = SysTimerCnt;
  202. ACTION = ACTIVATETASK
  203. {
  204. TASK = AcqUltrason;
  205. };
  206. AUTOSTART = TRUE
  207. {
  208. ALARMTIME = 5;
  209. CYCLETIME = 50;
  210. APPMODE = appmode1;
  211. };
  212. };
  213.  
  214. ALARM Alarme_acqchoc
  215. {
  216. COUNTER = SysTimerCnt;
  217. ACTION = ACTIVATETASK
  218. {
  219. TASK = AcqChoc;
  220. };
  221. AUTOSTART = TRUE
  222. {
  223. ALARMTIME = 5;
  224. CYCLETIME = 50;
  225. APPMODE = appmode1;
  226. };
  227. };
  228.  
  229. ALARM Alarme_afficher
  230. {
  231. COUNTER = SysTimerCnt;
  232. ACTION = ACTIVATETASK
  233. {
  234. TASK = Afficher;
  235. };
  236. AUTOSTART = TRUE
  237. {
  238. ALARMTIME = 5;
  239. CYCLETIME = 100;
  240. APPMODE = appmode1;
  241. };
  242. };
  243.  
  244. RESOURCE Piste
  245. {
  246. RESOURCEPROPERTY = STANDARD;
  247. };
  248.  
  249. RESOURCE AvanceRecule
  250. {
  251. RESOURCEPROPERTY = STANDARD;
  252. };
  253.  
  254. TASK AcqInfra
  255. {
  256. AUTOSTART = FALSE;
  257. PRIORITY = 1;
  258. ACTIVATION = 1;
  259. SCHEDULE = FULL;
  260. STACKSIZE = 512;
  261. RESOURCE = Piste;
  262. };
  263.  
  264. TASK SuivrePiste
  265. {
  266. AUTOSTART = FALSE;
  267. PRIORITY = 1;
  268. ACTIVATION = 1;
  269. SCHEDULE = FULL;
  270. STACKSIZE = 512;
  271. RESOURCE = Piste;
  272. };
  273.  
  274. TASK AcqUltrason
  275. {
  276. AUTOSTART = FALSE;
  277. PRIORITY = 1;
  278. ACTIVATION = 1;
  279. SCHEDULE = FULL;
  280. STACKSIZE = 512;
  281. RESOURCE = AvanceRecule;
  282. };
  283.  
  284. TASK AcqChoc
  285. {
  286. AUTOSTART = FALSE;
  287. PRIORITY = 1;
  288. ACTIVATION = 1;
  289. SCHEDULE = FULL;
  290. STACKSIZE = 512;
  291. RESOURCE = AvanceRecule;
  292. };
  293.  
  294. TASK Afficher
  295. {
  296. AUTOSTART = FALSE;
  297. PRIORITY = 1;
  298. ACTIVATION = 1;
  299. SCHEDULE = FULL;
  300. STACKSIZE = 512;
  301. };
  302. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement