#include <Environment.h>
Public Member Functions | |
void | beamRobotToState (ENV_POS *pos) |
void | doAction (unsigned int action, unsigned int delayTime, ACTION_RETURN *actionReturn) |
Environment (ENV_POS *discretization) | |
void | getCurrentState (ENV_POS *pos) |
void | getDiscretization (ENV_POS *discretization) |
bool | isValidAction (ENV_POS *pos, unsigned char action) |
void | setDiscretization (ENV_POS *discretization) |
~Environment () | |
Private Attributes | |
ENV_POS | currentPos |
ENV_POS | discretization |
FeedbackSensor * | r |
BioloidServo * | servo_x |
BioloidServo * | servo_y |
Definition at line 30 of file Environment.h.
Environment::Environment | ( | ENV_POS * | d | ) |
initializes the environment
Definition at line 18 of file Environment.c.
References currentPos, discretization, r, BioloidServo::scanBioloidBus(), servo_x, servo_y, BioloidServo::setPosition(), envPosition::x, and envPosition::y.
00018 { 00019 00020 // set the discretization 00021 this->discretization.x = d->x; 00022 this->discretization.y = d->y; 00023 00024 // initialize the wheel encoder 00025 //r = &FeedbackSensor(); 00026 00027 // initialize servos 00028 BioloidServo::scanBioloidBus(); 00029 00030 servo_x = new BioloidServo(6, 180, 450); // area of operations from 200-500 00031 servo_y = new BioloidServo(2, 320, 700); // area of operations from 450-600 00032 //servo_x = new BioloidServo(SERVO_X, 180, 450); // area of operations from 200-500 00033 //servo_y = new BioloidServo(SERVO_Y, 320, 700); // area of operations from 450-600 00034 00035 00036 // position servos to (0,0) 00037 this->currentPos.x = this->currentPos.y = 0; 00038 servo_x->setPosition(0, this->discretization.x); 00039 servo_y->setPosition(0, this->discretization.y); 00040 00041 00042 // instantiate FeedbackSensor 00043 r = new FeedbackSensor(); 00044 }
Environment::~Environment | ( | ) |
void Environment::beamRobotToState | ( | ENV_POS * | newPos | ) |
beam robot to state (pos_x, pos_y) without returning the reward
Definition at line 141 of file Environment.c.
References currentPos, discretization, servo_x, servo_y, BioloidServo::setPosition(), envPosition::x, and envPosition::y.
Referenced by ValueIteration::exploreAllFeedbacks(), and V24Control::processCommand().
00141 { 00142 00143 //BoardController::enableSerialBioloidInterface(); 00144 00145 if (newPos->x >= 0 && newPos->x < discretization.x) { 00146 servo_x->setPosition(newPos->x, discretization.x); 00147 this->currentPos.x = newPos->x; 00148 } else { 00149 newPos->x = this->currentPos.x; 00150 } 00151 00152 if (newPos->y >= 0 && newPos->x < discretization.y) { 00153 servo_y->setPosition(newPos->y, discretization.y); 00154 this->currentPos.y = newPos->y; 00155 } else { 00156 newPos->y = this->currentPos.y; 00157 } 00158 }
void Environment::doAction | ( | unsigned int | action, | |
unsigned int | delayTime, | |||
ACTION_RETURN * | actionReturn | |||
) |
performs an action within the environment and returns the successorState + reward
Definition at line 54 of file Environment.c.
References BoardController::blinkLED(), currentPos, FeedbackSensor::disableSensor(), BoardController::disableSerialInterfaces(), DOWN, FeedbackSensor::enableSensor(), BoardController::enableSerialBioloidInterface(), getCurrentState(), FeedbackSensor::getFeedback(), isValidAction(), LED_WALK, LEFT, BoardController::msDelay(), BoardController::powerOnLED(), r, actionReturn::reward, RIGHT, servo_x, servo_y, BioloidServo::setPosition(), actionReturn::state, UP, envPosition::x, and envPosition::y.
Referenced by main(), ValueIteration::moveAndSaveFeedback(), and V24Control::processCommand().
00054 { 00055 00056 ENV_POS newPos; 00057 this->getCurrentState(&newPos); 00058 00059 BoardController::powerOnLED((1<<LED_WALK), true); 00060 00061 // initialize reward 00062 actionReturn->reward = 0; 00063 00064 // Enable Bioloid Bus 00065 BoardController::enableSerialBioloidInterface(); 00066 00067 // if action is valid => select action 00068 if (this->isValidAction(&this->currentPos, action)) { 00069 00070 // compute successor state 00071 if (action == UP) { 00072 newPos.y--; 00073 } else if (action == DOWN) { 00074 newPos.y++; 00075 } else if (action == LEFT) { 00076 newPos.x--; 00077 } else if (action == RIGHT) { 00078 newPos.x++; 00079 } 00080 00081 // perform action 00082 if (newPos.x != this->currentPos.x || newPos.y != this->currentPos.y) { 00083 00084 r->enableSensor(); 00085 // enable bioloid bus 00086 servo_x->setPosition(newPos.x, this->discretization.x); 00087 servo_y->setPosition(newPos.y, this->discretization.y); 00088 00089 BoardController::msDelay(delayTime); 00090 00091 // retransmit for the case if an error occured the first time 00092 servo_x->setPosition(newPos.x, this->discretization.x); 00093 servo_y->setPosition(newPos.y, this->discretization.y); 00094 00095 r->disableSensor(); 00096 00097 // save new state information 00098 this->currentPos.x = newPos.x; 00099 this->currentPos.y = newPos.y; 00100 00101 // assemble actionReturn 00102 actionReturn->reward = r->getFeedback(); 00103 00104 } 00105 } else { 00106 BoardController::blinkLED((1<<LED_WALK), 4, 300); 00107 } 00108 00109 00110 // return current state state 00111 actionReturn->state.x = this->currentPos.x; 00112 actionReturn->state.y = this->currentPos.y; 00113 00114 BoardController::powerOnLED((1<<LED_WALK), false); 00115 00116 // Disable Bioloid Bus and enable V24 00117 BoardController::disableSerialInterfaces(); 00118 00119 00120 }
void Environment::getCurrentState | ( | ENV_POS * | _pos | ) |
returns the current state
Definition at line 123 of file Environment.c.
References currentPos, envPosition::x, and envPosition::y.
Referenced by doAction(), ValueIteration::doNextAction(), ValueIteration::getRandomAction(), ValueIteration::gotoRandomState(), ValueIteration::moveAndSaveFeedback(), V24Control::processCommand(), and V24Control::processGetGridsize().
00123 { 00124 _pos->x = this->currentPos.x; 00125 _pos->y = this->currentPos.y; 00126 }
void Environment::getDiscretization | ( | ENV_POS * | _discretization | ) |
set's the environment discretization
Definition at line 135 of file Environment.c.
References discretization, envPosition::x, and envPosition::y.
Referenced by ValueIteration::exploreAllFeedbacks(), StateSpace::getState(), ValueIteration::gotoRandomState(), ValueIteration::moveAndSaveFeedback(), V24Control::processGetState(), V24Control::processSetState(), StateSpace::setState(), StateSpace::StateSpace(), and ValueIteration::valueIterateAllStates().
00135 { 00136 _discretization->x = this->discretization.x; 00137 _discretization->y = this->discretization.y; 00138 }
bool Environment::isValidAction | ( | ENV_POS * | pos, | |
unsigned char | action | |||
) |
this function returns whether _action is a valid function in state (_row, _column) or not
Definition at line 163 of file Environment.c.
References DOWN, LEFT, RIGHT, UP, envPosition::x, and envPosition::y.
Referenced by doAction(), ValueIteration::exploreAllFeedbacks(), ValueIteration::getGreedyAction(), ValueIteration::moveAndSaveFeedback(), and ValueIteration::valueIterateAllStates().
00163 { 00164 00165 // check for valid action 00166 if (action != UP && action != DOWN && action != LEFT && action != RIGHT) return false; 00167 00168 // check if pos is within statespace range 00169 if (pos->x < 0 || pos->x >= this->discretization.x || 00170 pos->y < 0 || pos->y >= this->discretization.y) 00171 return false; 00172 00173 // check if successorstate is within the statespace 00174 if (action == UP && pos->y == 0) return false; 00175 else if (action == DOWN && pos->y >= (this->discretization.y-1)) return false; 00176 else if (action == LEFT && pos->x == 0) return false; 00177 else if (action == RIGHT && pos->x >= (this->discretization.x-1)) return false; 00178 00179 return true; 00180 }
void Environment::setDiscretization | ( | ENV_POS * | _discretization | ) |
set's the environment discretization
Definition at line 129 of file Environment.c.
References discretization, envPosition::x, and envPosition::y.
00129 { 00130 this->discretization.x = _discretization->x; 00131 this->discretization.y = _discretization->y; 00132 }
ENV_POS Environment::currentPos [private] |
Definition at line 39 of file Environment.h.
Referenced by beamRobotToState(), doAction(), Environment(), and getCurrentState().
ENV_POS Environment::discretization [private] |
Definition at line 40 of file Environment.h.
Referenced by beamRobotToState(), Environment(), getDiscretization(), and setDiscretization().
FeedbackSensor* Environment::r [private] |
Definition at line 34 of file Environment.h.
Referenced by doAction(), Environment(), and ~Environment().
BioloidServo* Environment::servo_x [private] |
Definition at line 36 of file Environment.h.
Referenced by beamRobotToState(), doAction(), and Environment().
BioloidServo* Environment::servo_y [private] |
Definition at line 37 of file Environment.h.
Referenced by beamRobotToState(), doAction(), and Environment().