Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 233 lines (198 sloc) 7.4 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
#ifndef _GRABBER_CONTROLLER_H_
#define _GRABBER_CONTROLLER_H_

#include "light.h"
#include "color.h"
#include "motion.h"
#include "sensors.h"
#include "manipulator.h"
#include "navigation.h"

//
#define STATE_INITIAL 0
#define STATE_SEARCHING 1
#define STATE_FOUND_OBJECT 4
#define STATE_MOVING_OBJECT 5

#define ROBOT_WIDTH 0.2
#define ROBOT_HEIGHT 0.3
//
#define LEFT_X ( 0.45 + ROBOT_WIDTH / 2 )
#define RIGHT_X ( 0.215 - ROBOT_WIDTH / 2 )
#define TOP_Y ( 0.50 - ROBOT_HEIGHT )
#define BOTTOM_Y ( -0.50 + ROBOT_HEIGHT )
#define SCENE_WIDTH ( RIGHT_X - LEFT_X )

//
#define MAX_SPEED 0.5
#define MAX_OMEGA 0.2

//
#define X_STEP ( ( RIGHT_X - LEFT_X ) / 10.0 )

struct grabber_controller_t
{
    int state;
    bool forward;
    
    //
    point_controller_t controller;
    position_t goal;
    
    //2*2 squares
    int map[ 4 ];
    int dropping_x[ 4 ];
    int dropping_y[ 4 ];
};

grabber_controller_t grabber_controller;

//
int get_sqare_id( float x, float y )
{
    int id = ( x > ( LEFT_X + RIGHT_X ) / 2.0 ) ? 1 : 0;
    if( y > ( TOP_Y + BOTTOM_Y ) / 2 )
        id += 2;
    return id;
}

// Prepare robot for competition.
// Must be called one time before call of controll_grabber( ).
void init_grabber_controller( )
{
    SetSensor( LIGHT_SENSOR_ID, SENSOR_LIGHT );
    SetSensor( COLOR_SENSOR_ID, SENSOR_LOWSPEED );
    SetSensor( ULTRASONIC_RIGHT_SENSOR_ID, SENSOR_LOWSPEED );
    SetSensor( ULTRASONIC_LEFT_SENSOR_ID, SENSOR_LOWSPEED );
    
    manipulator_reset_to_open( );
    update_navigation( );
    
    grabber_controller.state = STATE_INITIAL;
}

// Calculate positions of black and white squares of the scene.
// Auxiliary function. Don't call it directly from the task.
void initilize_map( )
{
  Acquire( navigation_mutex );
    float x = absolute_position.x;
    float y = absolute_position.y;
Release( navigation_mutex );

int colorID = floor_from_light( Sensor( LIGHT_SENSOR_ID ) );
int squareId = get_sqare_id( x, y );

    grabber_controller.map[ squareId ] = colorID;
    grabber_controller.map[ ( squareId + 2 ) % 4 ] = colorID;

    colorID = ( colorID == FLOOR_WHITE ) ? FLOOR_BLACK : FLOOR_WHITE;
    grabber_controller.map[ ( squareId + 1 ) % 4 ] = colorID;
    grabber_controller.map[ ( squareId + 3 ) % 4 ] = colorID;
    
    grabber_controller.dropping_x[ 0 ] = LEFT_X;
    grabber_controller.dropping_y[ 0 ] = 0.25 * ( TOP_Y + BOTTOM_Y );

    grabber_controller.dropping_x[ 1 ] = LEFT_X;
    grabber_controller.dropping_y[ 1 ] = 0.75 * ( TOP_Y + BOTTOM_Y );
    
    grabber_controller.dropping_x[ 2 ] = RIGHT_X;
    grabber_controller.dropping_y[ 2 ] = 0.25 * ( TOP_Y + BOTTOM_Y );

    grabber_controller.dropping_x[ 3 ] = RIGHT_X;
    grabber_controller.dropping_y[ 3 ] = 0.75 * ( TOP_Y + BOTTOM_Y );
}

// Initilize robot for moving and change its state.
// Auxiliary function. Don't call it directly from the task.
void set_moving_state( int state, float desX, float desY )
{
    // Called at more than one time to prevent wrong map initialisation
    // because of sensor's noise.
    initilize_map( );
    
    // Initilize robot for moving.
    grabber_controller.goal.x = desX;
    grabber_controller.goal.y = desY;
    init_point_controller( grabber_controller.controller );
    grabber_controller.state = state;
}

// Choose where grabed object will be dropped and set state to STATE_MOVING_OBJECT.
// Auxiliary function. Don't call it directly from the task.
void set_dropping_state( int objectID, float currX, float currY )
{
    manipulator_take_object( );
    int currentFloor = grabber_controller.map[ get_sqare_id( currX, currY ) ];
    int floorID = get_sqare_id( currX, currY );
    if( ( currentFloor == FLOOR_WHITE && objectID == OBJECT_RED ) ||
        ( currentFloor == FLOOR_BLACK && objectID == OBJECT_GREEN ) )
        // Change the floor.
        floorID = ( floorID + 1 ) % 4;
        
    // Initilize robot for moving.
    grabber_controller.goal.x = grabber_controller.dropping_x[ floorID ];
    grabber_controller.goal.y = grabber_controller.dropping_y[ floorID ];
    init_point_controller( grabber_controller.controller );
    grabber_controller.state = STATE_MOVING_OBJECT;
}

// Calculate new destination x coordinate.
// Auxiliary function. Don't call it directly from the task.
float get_new_x( float x )
{
    x += X_STEP;
    if( x > SCENE_WIDTH )
        x -= SCENE_WIDTH;
    return x;
}

bool left_object_detected( float x, float y, int sensorValue,
                           float& objectX, float& objectY )
{

    return false;
}

bool right_object_detected( float x, float y, int sensorValue,
                            float& objectX, float& objectY )
{

    return false;
}

// Non blocking function must be called periodically from the task.
void controll_grabber( )
{
    int object;
    float objectX, objectY;
    update_sensors( );
    
Acquire( navigation_mutex );
    float x = absolute_position.x;
    float y = absolute_position.y;
Release( navigation_mutex );
    
    switch( grabber_controller.state ) {
    case STATE_INITIAL:
    //
        grabber_controller.forward = true;
        set_moving_state( STATE_SEARCHING, x, TOP_Y );
        break;
    case STATE_SEARCHING:
    //
        object = object_from_color( sensors.color_value );
        if( object == OBJECT_RED || object == OBJECT_GREEN )
            set_dropping_state( object, x, y );
        else {
            if( left_object_detected( x, y,
                                      get_ultrasonic_distance( ULTRASONIC_LEFT_SENSOR_ID ),
                                      objectX, objectY ) ||
                right_object_detected( x, y,
                                           get_ultrasonic_distance( ULTRASONIC_RIGHT_SENSOR_ID ),
                                           objectX, objectY ) )
                set_moving_state( STATE_FOUND_OBJECT, objectX, objectY );
            else {
                if( go_to_point( grabber_controller.goal, grabber_controller.controller,
                                 MAX_SPEED, MAX_OMEGA ) ) {
                    y = grabber_controller.forward ? BOTTOM_Y : TOP_Y;
                    grabber_controller.forward = ! grabber_controller.forward;
                    set_moving_state( STATE_SEARCHING, get_new_x( x ), y );
                }
            }
        }
        break;
    case STATE_FOUND_OBJECT:
    //
        object = object_from_color( sensors.color_value );
        if( object == OBJECT_RED || object == OBJECT_GREEN )
            set_dropping_state( object, x, y );
        else {
            if( go_to_point( grabber_controller.goal, grabber_controller.controller,
                             MAX_SPEED, MAX_OMEGA ) ) {
                y = grabber_controller.forward ? BOTTOM_Y : TOP_Y;
                grabber_controller.forward = ! grabber_controller.forward;
                set_moving_state( STATE_SEARCHING, get_new_x( x ), y );
            }
        }
        break;
    case STATE_MOVING_OBJECT:
    //
        if( go_to_point( grabber_controller.goal, grabber_controller.controller,
                         MAX_SPEED, MAX_OMEGA ) ) {
            //FIXME: robot must move back a little here.
            
            manipulator_lost_object( );
            grabber_controller.forward = false;
            set_moving_state( STATE_SEARCHING, x, BOTTOM_Y );
        }
        break;
    };
}

#endif // _GRABBER_CONTROLLER_H_
Something went wrong with that request. Please try again.