Help with a program

Help
2008-12-28
2013-04-29
  • sdafd sfdsdfd
    sdafd sfdsdfd
    2008-12-28

    I am writing a program with c++ and I am so stuck. I only want my robot to go froward for 2 seconds and then turn around. I copied most of this from cpptest2 and added a little of what I made. As of right now it only goes foward and then stops. If I press run again it starts turning infinatly and I have to reset the robot. Can anyone help me out? Also, can someone give me a brief overview of the .oil file. I understand most of it but there are a few things i do not. Here are my .cc and .oil files:

    turnbot.cc

    //
    // turnbot.cc
    //
    // Sample for C++ and LEJOS-OSEK
    //
    // Written 20-jan-2008 by rwk
    //
    // Copyright 2007, 2008 by Takashi Chikamasa and Robert W. Kramer
    //

    #include <Motor.h>
    //
    // To properly interface with LEJOS-OSEK, we need to use C linkage between our
    // code and LEJOS-OSEK
    //

    extern "C" {

      #include "ecrobot_interface.h"

      // Port configration for Sensor and Motor
     
      Motor motor(NXT_PORT_C);
      Motor motor2(NXT_PORT_A);

      //
      // Main task (thread) declarations
      //
      // You need one DeclareTask( ) per task/thread
      //
      int finish = 0;
      DeclareTask(MotorTask);
      DeclareTask(TurnTask);

      //
      // User tasks go here.
      //

      TASK(MotorTask)
      {

          motor.setPower(75);
          motor2.setPower(75);
          sleep(2000);
          motor.forward();
          motor2.forward();
          sleep(2000);
          motor.stop();
          motor2.stop();
          finish=1;

        //
        // your task should end like this unless it's a one-shot task
        //

        //TaskType t;
        //GetTaskID(&t);
        //ChainTask(t);
      }     

      TASK(TurnTask)
      {
        if(finish==1)
        {
        sleep(4000);
        motor.setPower(75);
        motor2.setPower(75);
        motor.forward();
        motor2.backward();
        sleep(1000);
        motor.stop();
        motor2.stop();
        finish=0;
        }
        TaskType t;
        GetTaskID(&t);
        ChainTask(t);
      }
    }

    turnbot.oil

    #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{};

      EVENT SensorEventMask {
        MASK = AUTO;
      };

      EVENT SleepEventMask {
        MASK = AUTO;
      };

      /* Definition of Task1 */
      TASK MotorTask
      {
        AUTOSTART = TRUE {
        APPMODE = appmode1;
      };
        EVENT = SensorEventMask;
        EVENT = SleepEventMask;
        PRIORITY = 1; /* Smaller value means lower priority */
        ACTIVATION = 1;
        SCHEDULE = FULL;
        STACKSIZE = 512; /* Stack size */
      };
     
      TASK TurnTask
      {
        AUTOSTART = TRUE {
        APPMODE = appmode1;
      };
        EVENT = SensorEventMask;
        EVENT = SleepEventMask;
        PRIORITY = 1; /* Smaller value means lower priority */
        ACTIVATION = 1;
        SCHEDULE = FULL;
        STACKSIZE = 512; /* Stack size */
      };
     
      /* Definition of OSEK Alarm Counter */
      COUNTER SensorMonitorCounter
      {
        MINCYCLE = 1;
        MAXALLOWEDVALUE = 10000;
        TICKSPERBASE = 1; /* One tick is equal to 1msec */
      };
      };
    };

     
    • Here are the oil spec: http://portal.osek-vdx.org/files/pdf/specs/oil25.pdf

      I am not sure if nxtOsek is fully compatible with that spec, but I think it can be a good reference fo you.

      Problem with code is that the two task start running concurrently. In TurnTask the condition is checked  and of course it's yet not fulfilled. There is no point in defining two tasks for that scenario. If you want to play with that you could use WaitForEvent, SetEvent instead of finish variable.