Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* helloworld.c for TOPPERS/ATK(OSEK) */
- #include "kernel.h"
- #include "kernel_id.h"
- #include "ecrobot_interface.h"
- #define seuil 600
- #define lightsensor NXT_PORT_S2
- #define sonarsensor NXT_PORT_S1
- #define touchsensor NXT_PORT_S3
- #define motorl NXT_PORT_B
- #define motord NXT_PORT_A
- #define etatchercheadroite 0
- #define etatchercheagauche 1
- DeclareCounter( SysTimerCnt);
- DeclareAlarm( Alarme_acq);
- DeclareAlarm( Alarme_acqultrason);
- DeclareAlarm( Alarme_acqchoc);
- DeclareAlarm( Alarme_afficher);
- DeclareTask( AcqInfra);
- DeclareTask( SuivrePiste);
- DeclareTask( AcqUltrason);
- DeclareTask( AcqChoc);
- DeclareTask( Afficher);
- DeclareResource( Piste);
- DeclareResource( AvanceRecule);
- typedef struct {
- int surPiste; // vaut 1 si sur la piste et 0 sinon
- } type_SurPiste;
- static volatile type_SurPiste donneePiste;
- static int etatcourant = 0;
- static volatile int ultrason = 1, choc = 1;
- static int coulavt, chocvalue, sonarvalue;
- /* nxtOSEK hook to be invoked from an ISR in category 2 */
- void user_1ms_isr_type2(void) {
- StatusType ercd;
- /* Increment System Timer Count */
- ercd = SignalCounter(SysTimerCnt);
- if (ercd != E_OK) {
- ShutdownOS(ercd);
- }
- }
- void ecrobot_device_initialize(void) {
- ecrobot_set_light_sensor_active(lightsensor);
- ecrobot_init_sonar_sensor(sonarsensor);
- }
- TASK( AcqInfra) {
- coulavt = ecrobot_get_light_sensor(lightsensor);
- int surpiste;
- static int compteur = 1, limite = 5;
- if (coulavt > seuil) {
- surpiste = 1;
- compteur = 0;
- limite = 0;
- } else {
- surpiste = 0;
- if (compteur == limite) {
- compteur = 0;
- if (etatcourant == etatchercheadroite)
- etatcourant = etatchercheagauche;
- else if (etatcourant == etatchercheagauche)
- etatcourant = etatchercheadroite;
- limite += 5;
- }
- compteur++;
- }
- GetResource(Piste);
- donneePiste.surPiste = surpiste;
- ReleaseResource(Piste);
- ChainTask(SuivrePiste);
- }
- TASK( SuivrePiste) {
- int surpiste = 0;
- GetResource(Piste);
- surpiste = donneePiste.surPiste;
- ReleaseResource(Piste);
- int avrechoc, avreultra;
- GetResource(AvanceRecule);
- avrechoc = choc;
- avreultra = ultrason;
- ReleaseResource(AvanceRecule);
- if (surpiste == 1) {
- if (avrechoc == 0 && avreultra == 0) {
- nxt_motor_set_speed(motorl, 70, 0);
- nxt_motor_set_speed(motord, 70, 0);
- } else {
- nxt_motor_set_speed(motorl, -70, 0);
- nxt_motor_set_speed(motord, -70, 0);
- }
- } else if (etatcourant == etatchercheadroite) {
- if (avrechoc == 0 && avreultra == 0) {
- nxt_motor_set_speed(motorl, 40, 1);
- nxt_motor_set_speed(motord, -40, 1);
- } else {
- nxt_motor_set_speed(motorl, -40, 1);
- nxt_motor_set_speed(motord, 40, 1);
- }
- } else if (etatcourant == etatchercheagauche) {
- if (avrechoc == 0 && avreultra == 0) {
- nxt_motor_set_speed(motorl, -40, 1);
- nxt_motor_set_speed(motord, 40, 1);
- } else {
- nxt_motor_set_speed(motorl, 40, 1);
- nxt_motor_set_speed(motord, -40, 1);
- }
- }
- TerminateTask();
- }
- TASK( AcqUltrason) {
- sonarvalue = ecrobot_get_sonar_sensor(sonarsensor);
- if (sonarvalue <= 30) {
- GetResource(AvanceRecule);
- ultrason = 1;
- ReleaseResource(AvanceRecule);
- } else {
- GetResource(AvanceRecule);
- ultrason = 0;
- ReleaseResource(AvanceRecule);
- }
- TerminateTask();
- }
- TASK( AcqChoc) {
- chocvalue = ecrobot_get_touch_sensor(touchsensor);
- if (chocvalue == 1) {
- GetResource(AvanceRecule);
- choc = 1;
- ReleaseResource(AvanceRecule);
- } else if (chocvalue == 0) {
- GetResource(AvanceRecule);
- choc = 0;
- ReleaseResource(AvanceRecule);
- }
- TerminateTask();
- }
- TASK( Afficher) {
- display_clear(0);
- display_goto_xy(7, 1);
- display_int(coulavt, 3);
- display_goto_xy(7, 3);
- display_int(chocvalue, 3);
- display_goto_xy(7, 5);
- display_int(sonarvalue, 3);
- display_update();
- TerminateTask();
- }
- ===================================================================
- #include "implementation.oil"
- CPU ATMEL_AT91SAM7S256
- {
- OS LEJOS_OSEK
- {
- STATUS = EXTENDED;
- STARTUPHOOK = FALSE;
- ERRORHOOK = FALSE;
- SHUTDOWNHOOK = FALSE;
- PRETASKHOOK = FALSE;
- POSTTASKHOOK = FALSE;
- USEGETSERVICEID = FALSE;
- USEPARAMETERACCESS = FALSE;
- USERESSCHEDULER = FALSE;
- };
- /* Definition of application mode */
- APPMODE appmode1{};
- COUNTER SysTimerCnt
- {
- MINCYCLE = 1;
- MAXALLOWEDVALUE = 10000;
- TICKSPERBASE = 1;
- };
- ALARM Alarme_acq
- {
- COUNTER = SysTimerCnt;
- ACTION = ACTIVATETASK
- {
- TASK = AcqInfra;
- };
- AUTOSTART = TRUE
- {
- ALARMTIME = 5;
- CYCLETIME = 20;
- APPMODE = appmode1;
- };
- };
- ALARM Alarme_acqultrason
- {
- COUNTER = SysTimerCnt;
- ACTION = ACTIVATETASK
- {
- TASK = AcqUltrason;
- };
- AUTOSTART = TRUE
- {
- ALARMTIME = 5;
- CYCLETIME = 50;
- APPMODE = appmode1;
- };
- };
- ALARM Alarme_acqchoc
- {
- COUNTER = SysTimerCnt;
- ACTION = ACTIVATETASK
- {
- TASK = AcqChoc;
- };
- AUTOSTART = TRUE
- {
- ALARMTIME = 5;
- CYCLETIME = 50;
- APPMODE = appmode1;
- };
- };
- ALARM Alarme_afficher
- {
- COUNTER = SysTimerCnt;
- ACTION = ACTIVATETASK
- {
- TASK = Afficher;
- };
- AUTOSTART = TRUE
- {
- ALARMTIME = 5;
- CYCLETIME = 100;
- APPMODE = appmode1;
- };
- };
- RESOURCE Piste
- {
- RESOURCEPROPERTY = STANDARD;
- };
- RESOURCE AvanceRecule
- {
- RESOURCEPROPERTY = STANDARD;
- };
- TASK AcqInfra
- {
- AUTOSTART = FALSE;
- PRIORITY = 1;
- ACTIVATION = 1;
- SCHEDULE = FULL;
- STACKSIZE = 512;
- RESOURCE = Piste;
- };
- TASK SuivrePiste
- {
- AUTOSTART = FALSE;
- PRIORITY = 1;
- ACTIVATION = 1;
- SCHEDULE = FULL;
- STACKSIZE = 512;
- RESOURCE = Piste;
- };
- TASK AcqUltrason
- {
- AUTOSTART = FALSE;
- PRIORITY = 1;
- ACTIVATION = 1;
- SCHEDULE = FULL;
- STACKSIZE = 512;
- RESOURCE = AvanceRecule;
- };
- TASK AcqChoc
- {
- AUTOSTART = FALSE;
- PRIORITY = 1;
- ACTIVATION = 1;
- SCHEDULE = FULL;
- STACKSIZE = 512;
- RESOURCE = AvanceRecule;
- };
- TASK Afficher
- {
- AUTOSTART = FALSE;
- PRIORITY = 1;
- ACTIVATION = 1;
- SCHEDULE = FULL;
- STACKSIZE = 512;
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement