Robot:

Line Follower Code:
include "kernel.h"
#include "kernel_id.h"
#include "ecrobot_interface.h"


#define PORT_ID NXT_PORT_S1

/* TOPPERS/ATK declarations */
DeclareCounter(SysTimerCnt);
DeclareAlarm(AlarmTask2);
DeclareEvent(EventTask2);

/* nxtOSEK hooks */
void ecrobot_device_initialize(void)
{
ecrobot_init_nxtcolorsensor(PORT_ID, NXT_COLORSENSOR); // initialize a sensor
}

void ecrobot_device_terminate(void)
{
ecrobot_term_nxtcolorsensor(PORT_ID); // terminate a sensor
}

/* nxtOSEK hook to be invoked from an ISR in category 2 */
void user_1ms_isr_type2(void)
{
(void)SignalCounter(SysTimerCnt); /* Increment OSEK Alarm Counter */
}

/* Alarm executed Task2 */
TASK(Task2)
{
int X = 100;
int Y = 0;
int Avg = 50;
U8 entr;
U8 entr_state = 0;
S16 rgb[3];
while(1)
{
WaitEvent(EventTask2);
        ClearEvent(EventTask2);

// switching the color sensor mode
entr = ecrobot_is_ENTER_button_pressed();
if (entr && !entr_state)
{
switch(ecrobot_get_nxtcolorsensor_mode(PORT_ID))
{
case NXT_COLORSENSOR:
ecrobot_set_nxtcolorsensor(PORT_ID, NXT_LIGHTSENSOR_RED);
break;
case NXT_LIGHTSENSOR_RED:
ecrobot_set_nxtcolorsensor(PORT_ID, NXT_LIGHTSENSOR_GREEN);
break;
case NXT_LIGHTSENSOR_GREEN:
ecrobot_set_nxtcolorsensor(PORT_ID, NXT_LIGHTSENSOR_BLUE);
break;
case NXT_LIGHTSENSOR_BLUE:
ecrobot_set_nxtcolorsensor(PORT_ID, NXT_LIGHTSENSOR_WHITE);
break;
case NXT_LIGHTSENSOR_WHITE:
ecrobot_set_nxtcolorsensor(PORT_ID, NXT_LIGHTSENSOR_NONE);
break;
case NXT_LIGHTSENSOR_NONE:
ecrobot_set_nxtcolorsensor(PORT_ID, NXT_COLORSENSOR_DEACTIVATE);
break;
case NXT_COLORSENSOR_DEACTIVATE:
ecrobot_set_nxtcolorsensor(PORT_ID, NXT_COLORSENSOR);
break;
default:
// Idle phase
break;
}
}
entr_state = entr;

// display different sensor data depending on the sensor mode
display_clear(0);
display_goto_xy(0, 0);
switch(ecrobot_get_nxtcolorsensor_mode(PORT_ID))
{
case NXT_COLORSENSOR:
display_string("COLOR");
display_goto_xy(0, 1);
switch(ecrobot_get_nxtcolorsensor_id(PORT_ID)) // get color number data
{
case NXT_COLOR_BLACK:
display_string("BLACK");
break;
case NXT_COLOR_BLUE:
display_string("BLUE");
break;
case NXT_COLOR_GREEN:
display_string("GREEN");
break;
case NXT_COLOR_YELLOW:
display_string("YELLOW");
break;
case NXT_COLOR_RED:
display_string("RED");
break;
case NXT_COLOR_WHITE:
display_string("WHITE");
break;
default:
display_string("UNKNOWN COLOR");
break;
}

ecrobot_get_nxtcolorsensor_rgb(PORT_ID, rgb);
display_goto_xy(0, 2);
display_string("R:-");
display_int(rgb[0],0);
display_goto_xy(0, 3);
display_string("G:-");
display_int(rgb[1],0);
display_goto_xy(0, 4);
display_string("B:-");
display_int(rgb[2],0);
if(rgb[2]<thres)
{
nxt_motor_set_speed(NXT_PORT_A, 100, 1); //Car moves forward

nxt_motor_set_speed(NXT_PORT_C, -70, 0);
}
if(rgb[2]>thres)
{
nxt_motor_set_speed(NXT_PORT_C, 100, 1);
//systick_wait_ms(100);
nxt_motor_set_speed(NXT_PORT_A, -70, 0);
}
break;
case NXT_LIGHTSENSOR_RED:
display_string("RED");
display_goto_xy(0, 1);
display_int(ecrobot_get_nxtcolorsensor_light(PORT_ID), 0); //for the colour sensor data
display_goto_xy(0, 2);
X = ecrobot_get_nxtcolorsensor_light(PORT_ID);
//for the colour sensor data

display_int(X,0);
break;
case NXT_LIGHTSENSOR_GREEN:
display_string("GREEN");
//for the colour sensor data

display_goto_xy(0, 1);
display_int(ecrobot_get_nxtcolorsensor_light(PORT_ID), 0);
display_goto_xy(0, 2);
Y = ecrobot_get_nxtcolorsensor_light(PORT_ID);
display_int(Y,0);
break;
case NXT_LIGHTSENSOR_BLUE:
display_string("BLUE");
display_goto_xy(0, 1);
display_int(ecrobot_get_nxtcolorsensor_light(PORT_ID), 0);
display_goto_xy(0, 2);
Avg = (X+Y)/2;
display_int(Avg,0);
break;
case NXT_LIGHTSENSOR_WHITE:
display_string("WHITE");
display_goto_xy(0, 1);
display_int(ecrobot_get_nxtcolorsensor_light(PORT_ID), 0); // get light sensor data
display_goto_xy(0, 2);
display_int(X, 0);
display_goto_xy(0, 3);
display_int(Y, 0);
display_goto_xy(0, 4);
display_int(Avg, 0);
break;
case NXT_LIGHTSENSOR_NONE:
display_string("LIGHT NONE");
display_goto_xy(0, 1);
display_update();
}
}

/* Background Task */
TASK(Task1)
{
SetRelAlarm(AlarmTask2, 1, 100);
while(1)
{
ecrobot_process_bg_nxtcolorsensor();// (This must be a Redundant background Task)
}
}




2. Event Driven: In case an obstacle comes in front.

#include "kernel.h"
#include "kernel_id.h"
#include "ecrobot_interface.h"

/* OSEK declarations */
DeclareCounter(Time_Counter);//declare the counter to govern the tasks
DeclareTask(Event_Dispatcher); //declare the event dispatcher task 
DeclareTask(Event_Callback);//declare the motor control task
DeclareTask(Task_LCD);//declare the display task

DeclareEvent(TouchSensorOnEvent); /* Event declaration */
DeclareEvent(TouchSensorOffEvent); /* Event declaration */ 

/* nxtOSEK hooks */
void ecrobot_device_initialize(void)
{
ecrobot_init_nxtcolorsensor(NXT_PORT_S3, NXT_LIGHTSENSOR_RED);//PORT_3 for colour sensor activation
nxt_motor_set_speed(NXT_PORT_B, 0, 1); //PORT_B for right motor....activation
nxt_motor_set_speed(NXT_PORT_C, 0, 1); //PORT_C for left motor....activation
}

void ecrobot_device_terminate(void)
{
ecrobot_term_nxtcolorsensor(NXT_PORT_S3);//PORT_3 for colour sensor demilitarisation
nxt_motor_set_speed(NXT_PORT_B, 0, 1); //PORT_B for right motor demilitarisation
nxt_motor_set_speed(NXT_PORT_C, 0, 1); //PORT_C for left motor demilitarisation
}

/* nxtOSEK hook to be invoked from an ISR in category 2 */
void user_1ms_isr_type2(void)
{
SignalCounter(Time_Counter);//Time counter incremented by one interrupt handler
}

/* Event_Dispatcher task body */
TASK(Event_Dispatcher)
{
unsigned int value;//used to store the light sensor value
static U8 TouchSensorStatus_old = 0;//used to store the touch sensor previous state
U8 TouchSensorStatus;// used to store the touch sensor current state

while(1)
{
TouchSensorStatus = ecrobot_get_touch_sensor(NXT_PORT_S1);//get the current status of the touch sensor and store it in a variable
ecrobot_process_bg_nxtcolorsensor();//probe the colour sensor in the background
value=ecrobot_get_nxtcolorsensor_light(NXT_PORT_S3);//grab the feedback value from the colour sensor
if (TouchSensorStatus == 1 && TouchSensorStatus_old == 0)//check the states of the colour sensor
{

if(value<350)//if the feedback value from the light sensor is less...do not start up the motor
{
TouchSensorStatus_old = 0;//change the state of the previous touch sensor
}
else//or else
{
TouchSensorStatus_old = TouchSensorStatus;       //change the state of the previous touch sensor
SetEvent(Event_Callback, TouchSensorOnEvent);     //trigger the event to start the motor.
}

}

else if ((TouchSensorStatus == 1 && TouchSensorStatus_old == 1) && (value<350))//checking if the system is stopped by the edge detection policy
{

TouchSensorStatus_old = 0;
SetEvent(Event_Callback, TouchSensorOffEvent);//trigger the event to stop the motor.
}

else if (TouchSensorStatus == 0 && TouchSensorStatus_old == 1)//if the system is stopped by the touch sensor
{
TouchSensorStatus_old = TouchSensorStatus;
SetEvent(Event_Callback, TouchSensorOffEvent);//trigger the event to stop the motor.
}

}
TerminateTask();//terminate task
}

/* motor sensor control task executed by OSEK Events */
TASK(Event_Callback)
{
while(1)
{
WaitEvent(TouchSensorOnEvent); /* Task is in waiting status until the ON Event comes */ 
ClearEvent(TouchSensorOnEvent);//clear the event mask bits to make it ready for the next event..
nxt_motor_set_speed(NXT_PORT_B, 40, 1);//set the right motor speed and start it up
nxt_motor_set_speed(NXT_PORT_C, 40, 1);//set the left motor speed and start it up

WaitEvent(TouchSensorOffEvent); /* Task is in waiting status until the OFF Event comes */
ClearEvent(TouchSensorOffEvent);//clear the event mask bits to make it ready for the next event.
nxt_motor_set_speed(NXT_PORT_B, 0, 1);//set the right motor speed and start it up
nxt_motor_set_speed(NXT_PORT_C, 0, 1);/*set the left motor speed and start it up*/
}

TerminateTask();//terminate the task
}

/* TaskLCD to display........ */
TASK(Task_LCD)
{
ecrobot_status_monitor("EDS");//display the present status of the machine
TerminateTask();//terminate the task
}

Comments

Popular posts from this blog

Gaming in Pygame