task main() { //initializeRobot(); waitForStart(); wait10Msec(200); servoChangeRate[elServo]=1; servoChangeRate[ringPlacer]=1; servo[elServo] = ES_POSITION_1; servo[ringPlacer] = RP_POSITION_3; servoChangeRate[elServo]=10; servoChangeRate[ringPlacer]=10; wait10Msec(200); gostraight (2500, 80); turnright (4350, 80); gostraight (3650, 80); wait10Msec(200); servo[elServo] = ES_POSITION_2; servo[ringPlacer] = RP_POSITION_2; wait10Msec(200); servo[ringPlacer] = RP_POSITION_3; wait10Msec(200); turnright(180, 80); turnleft(180, 80); turnright(180, 80); turnleft(180, 80); drival (-50, -50); wait10Msec(200); drival (0, 0); drival (0, 0); }
int main(void) { const unsigned short MainCycle = 60; Init(MainCycle); gostraight(); turnleft(); gostraight(); turnleft(); gostraight(); turnleft(); gostraight(); turnleft(); return 0; }
static void approachintersection(void * unusedpointer, unsigned long carnumber) { int cardirection; int destdirection; int car_no=carnumber; /* * Avoid unused variable and function warnings. */ (void) unusedpointer; /* * cardirection is set randomly. */ cardirection = random() % 4; destdirection = random() % 3; destdirection = (cardirection + destdirection + 1) % 4; if(cardirection-destdirection==2 || destdirection-cardirection==2) //if car wants to go straight gostraight(cardirection,car_no,destdirection); else if(destdirection-cardirection==1 || cardirection-destdirection==3) //if car wants to turn left turnleft(cardirection,car_no,destdirection); else //else the car wants to turn right turnright(cardirection,car_no,destdirection); }
static void approachintersection(void * unusedpointer, unsigned long carnumber) { int car_direction; int turn_direction; /* * Avoid unused variable and function warnings. */ (void) unusedpointer; (void) carnumber; (void) gostraight; (void) turnleft; (void) turnright; /* * cardirection is set randomly. */ car_direction = random() % 4; turn_direction = random() % 3; // we enforce the FIFO ordering for the cars approaching // the intersection from the same direction if (car_direction == 0) {// North // north P(from_north); } else if (car_direction == 1) { //East P(from_east); } else if (car_direction == 2) {//South P(from_south); } else { assert(car_direction == 3); //West P(from_west); } // Step 1: approaching an intersection // message(APPROACHING, carnumber, car_direction, turn_direction); // kprintf("car: %d, from: %c, turn_direction: %d\n", carnumber, directions[car_direction], turn_direction); // after we approached the intersection, we enter it if (turn_direction == TURN_LEFT) { //0 turnleft(car_direction, carnumber); } else if (turn_direction == TURN_RIGHT) { //1 turnright(car_direction, carnumber); } else { assert(turn_direction == GO_STRAIGHT); //2 gostraight(car_direction, carnumber); } V(join_sem); }
static void approachintersection(void * unusedpointer, unsigned long carnumber) { int cardirection; /* * Avoid unused variable and function warnings. */ (void) unusedpointer; (void) carnumber; (void) gostraight; (void) turnleft; (void) turnright; /* * cardirection is set randomly. */ //cardirection = 0 ---> from North //cardirection = 1 ---> from East //cardirection = 2 ---> from South //cardirection = 3 ---> from West cardirection = random() % 4; //move = 0 ---> go straight //move = 1 ---> go right //move = 2 ---> go left int move = random() % 3; //lock the intersections if 3 cars are already inside P(Slock); if(move == 0) gostraight(cardirection, carnumber); else if(move == 1) turnright(cardirection, carnumber); else if(move == 2) turnleft(cardirection, carnumber); V(Slock); }