#include <stdio.h>
#ifdef __WIN32__
#include <windows.h>
#else
#include <unistd.h>
#define Sleep( msec ) usleep(( msec ) * 1000 )
#endif
#define L_MOTOR_PORT OUTPUT_C
#define L_MOTOR_EXT_PORT EXT_PORT__NONE_
#define R_MOTOR_PORT OUTPUT_B
#define R_MOTOR_EXT_PORT EXT_PORT__NONE_
#define IR_CHANNEL 0
#define SPEED_LINEAR 75
#define SPEED_CIRCULAR 50
int max_speed;
#define DEGREE_TO_COUNT( d ) (( d ) * 260 / 90 )
int app_alive;
enum {
MODE_REMOTE,
MODE_AUTO,
};
int mode;
enum {
MOVE_NONE,
MOVE_FORWARD,
MOVE_BACKWARD,
TURN_LEFT,
TURN_RIGHT,
TURN_ANGLE,
STEP_BACKWARD,
};
int moving;
int command;
int angle;
uint8_t ir, touch;
enum { L, R };
uint8_t motor[ 3 ] = { DESC_LIMIT, DESC_LIMIT, DESC_LIMIT };
static void _set_mode( int value )
{
if ( value == MODE_AUTO ) {
mode = MODE_AUTO;
} else {
mode = MODE_REMOTE;
}
}
static void _run_forever( int l_speed, int r_speed )
{
}
static void _run_to_rel_pos( int l_speed, int l_pos, int r_speed, int r_pos )
{
}
static void _run_timed( int l_speed, int r_speed, int ms )
{
}
static int _is_running( void )
{
FLAGS_T state = TACHO_STATE__NONE_;
if ( state != TACHO_STATE__NONE_ ) return ( 1 );
if ( state != TACHO_STATE__NONE_ ) return ( 1 );
return ( 0 );
}
static void _stop( void )
{
}
int app_init( void )
{
char s[ 16 ];
} else {
printf(
"LEFT motor (%s) is NOT found.\n",
ev3_port_name( L_MOTOR_PORT, L_MOTOR_EXT_PORT, 0, s ));
return ( 0 );
}
} else {
printf(
"RIGHT motor (%s) is NOT found.\n",
ev3_port_name( R_MOTOR_PORT, R_MOTOR_EXT_PORT, 0, s ));
return ( 0 );
}
command = moving = MOVE_NONE;
_set_mode( MODE_REMOTE );
} else {
printf( "IR sensor is NOT found.\n" );
return ( 0 );
}
printf( "IR remote control:\n"
"RED UP & BLUE UP - forward\n"
"RED DOWN & BLUE DOWN - backward\n"
"RED UP | BLUE DOWN - left\n"
"RED DOWN | BLUE UP - right\n"
"To switch between IR remote control and\n"
"self-driving" );
printf( " use the TOUCH sensor.\n" );
} else {
touch = DESC_LIMIT;
printf( " press UP on the EV3 brick.\n" );
}
printf( "Press BACK on the EV3 brick for EXIT...\n" );
return ( 1 );
}
{
for ( ; ; ) {
command = MOVE_NONE;
_set_mode(( mode == MODE_REMOTE ) ? MODE_AUTO : MODE_REMOTE );
}
}
{
CORO_LOCAL uint8_t keys, pressed = EV3_KEY__NONE_;
for ( ; ; ) {
pressed = keys;
if ( pressed & EV3_KEY_BACK ) {
command = MOVE_NONE;
app_alive = 0;
} else if ( pressed & EV3_KEY_UP ) {
command = MOVE_NONE;
_set_mode(( mode == MODE_REMOTE ) ? MODE_AUTO : MODE_REMOTE );
}
}
}
{
for ( ; ; ) {
val = IR_REMOTE__NONE_;
switch ( val ) {
case RED_UP_BLUE_UP:
command = MOVE_FORWARD;
break;
case RED_DOWN_BLUE_DOWN:
command = MOVE_BACKWARD;
break;
case RED_UP:
case RED_UP_BLUE_DOWN:
case BLUE_DOWN:
command = TURN_LEFT;
break;
case BLUE_UP:
case RED_DOWN_BLUE_UP:
case RED_DOWN:
command = TURN_RIGHT;
break;
case IR_REMOTE__NONE_:
case RED_UP_RED_DOWN:
case BLUE_UP_BLUE_DOWN:
case BEACON_MODE_ON:
command = MOVE_NONE;
break;
}
}
}
{
for ( ; ; ) {
prox = 0;
if ( prox == 0 ) {
command = MOVE_NONE;
} else if ( prox < 20 ) {
front = prox;
angle = -30;
do {
command = TURN_ANGLE;
prox = 0;
if ( prox < front ) {
if ( angle < 0 ) {
angle = 60;
} else {
command = STEP_BACKWARD;
}
}
} while (( prox > 0 ) && ( prox < 40 ) && ( mode == MODE_AUTO ));
} else {
command = MOVE_FORWARD;
}
}
}
{
speed_linear = max_speed * SPEED_LINEAR / 100;
speed_circular = max_speed * SPEED_CIRCULAR / 100;
for ( ; ; ) {
_wait_stopped = 0;
switch ( command ) {
case MOVE_NONE:
_stop();
_wait_stopped = 1;
break;
case MOVE_FORWARD:
_run_forever( -speed_linear, -speed_linear );
break;
case MOVE_BACKWARD:
_run_forever( speed_linear, speed_linear );
break;
case TURN_LEFT:
_run_forever( speed_circular, -speed_circular );
break;
case TURN_RIGHT:
_run_forever( -speed_circular, speed_circular );
break;
case TURN_ANGLE:
_run_to_rel_pos( speed_circular, DEGREE_TO_COUNT( -angle )
, speed_circular, DEGREE_TO_COUNT( angle ));
_wait_stopped = 1;
break;
case STEP_BACKWARD:
_run_timed( speed_linear, speed_linear, 1000 );
_wait_stopped = 1;
break;
}
moving = command;
if ( _wait_stopped ) {
command = moving = MOVE_NONE;
}
}
}
int main( void )
{
printf( "Waiting the EV3 brick online...\n" );
printf( "*** ( EV3 ) Hello! ***\n" );
app_alive = app_init();
while ( app_alive ) {
Sleep( 10 );
}
printf( "*** ( EV3 ) Bye! ***\n" );
return ( 0 );
}