Johnathan Chin / Mbed 2 deprecated robot_tilt_move

Dependencies:   4DGL-uLCD-SE PinDetect mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Robot.cpp Source File

Robot.cpp

00001 #include <iostream>
00002 #include "uLCD_4DGL.h"
00003 #include "Robot.h"
00004 #include "mbed.h"
00005 
00006 uLCD_4DGL uLCD(p28, p27, p29);
00007 
00008 Robot::Robot() {
00009     xPosition = 63;
00010     yPosition = 63;
00011 }
00012     
00013 Robot::Robot(int xPos, int yPos): xPosition(xPos), yPosition(yPos) {
00014 }
00015 
00016 void Robot::setXPosition(int xPos) {
00017     xPosition = xPos;
00018 }
00019 
00020 void Robot::setYPosition(int yPos) {
00021     yPosition = yPos;
00022 }
00023 
00024 int Robot::getXPosition() {
00025     return xPosition;
00026 }
00027 
00028 int Robot::getYPosition() {
00029     return yPosition;
00030 }
00031 
00032 void Robot::moveForward(int numMove) {
00033     setYPosition(yPosition + numMove);
00034 }
00035 
00036 void Robot::moveBack(int numMove) {
00037     setYPosition(yPosition - numMove);
00038 }
00039 
00040 void Robot::moveRight(int numMove) {
00041     setXPosition(xPosition + numMove);
00042 }
00043 
00044 void Robot::moveLeft(int numMove) {
00045     setXPosition(xPosition - numMove);
00046 }
00047 
00048 void Robot::inputMove() {
00049     char direction;
00050     int numSpaces;
00051     std::cout << "Please enter a direction for the robot to move " 
00052         << "(F, B, L, or R), followed by the integer distance: \n";
00053     std::cin >> direction;
00054     std::cin >> numSpaces;
00055     switch (direction) {
00056     case 'F':
00057         moveForward(numSpaces);
00058         std::cout << "Robot is moving forward " << numSpaces << " units.\n";
00059         break;
00060     case 'B':
00061         moveBack(numSpaces);
00062         std::cout << "Robot is moving backward " << numSpaces << " units.\n";
00063         break;
00064     case 'L':
00065         moveLeft(numSpaces);
00066         std::cout << "Robot is moving left " << numSpaces << " units.\n";
00067         break;
00068     case 'R':
00069         moveRight(numSpaces);
00070         std::cout << "Robot is moving right " << numSpaces << " units.\n";
00071         break;
00072     default:
00073         std::cout << "Invalid move.\n";
00074     }
00075 }
00076 
00077 void Robot::displayPosition() {
00078     std::cout << "Robot is located at x = " << xPosition
00079         << ", y = " << yPosition << "\n";
00080 }
00081 
00082 void Robot::draw() {
00083     uLCD.rectangle(Robot::getYPosition() - 5, Robot::getXPosition() - 5,
00084         Robot::getYPosition() + 5, Robot::getXPosition() + 5, WHITE);
00085     uLCD.rectangle(Robot::getYPosition() - 3, Robot::getXPosition() + 4,
00086         Robot::getYPosition() + 3, Robot::getXPosition() + 2, WHITE);
00087     //uLCD.line(this.getX() - 5, this,getY(), this.getX() - 7, this.getY(),
00088 //      WHITE);
00089 //  uLCD.line(this.getX() + 5, this,getY(), this.getX() + 7, this.getY(),
00090 //      WHITE);
00091 //  uLCD.line(this.getX() - 7, this,getY(), this.getX() - 7, this.getY() - 2,
00092 //      WHITE);
00093 //  uLCD.line(this.getX() + 7, this,getY(), this.getX() + 7, this.getY() - 2,
00094 //      WHITE);
00095     uLCD.line(Robot::getYPosition(), Robot::getXPosition() - 5,
00096         Robot::getYPosition(), Robot::getXPosition() - 7,WHITE);
00097     uLCD.circle(Robot::getYPosition() - 2, Robot::getXPosition() - 2, 2, WHITE);
00098     uLCD.circle(Robot::getYPosition() + 2, Robot::getXPosition() - 2, 2, WHITE);
00099     uLCD.circle(Robot::getYPosition(), Robot::getXPosition() - 9, 2, WHITE);
00100 }
00101 
00102 void Robot::erase() {
00103     uLCD.rectangle(Robot::getYPosition() - 5, Robot::getXPosition() - 5,
00104         Robot::getYPosition() + 5, Robot::getXPosition() + 5, BLACK);
00105     uLCD.rectangle(Robot::getYPosition() - 3, Robot::getXPosition() + 4,
00106         Robot::getYPosition() + 3, Robot::getXPosition() + 2, BLACK);
00107     uLCD.line(Robot::getYPosition(), Robot::getXPosition() - 5,
00108         Robot::getYPosition(), Robot::getXPosition() - 7, BLACK);
00109     uLCD.circle(Robot::getYPosition() - 2, Robot::getXPosition() - 2, 2, BLACK);
00110     uLCD.circle(Robot::getYPosition() + 2, Robot::getXPosition() - 2, 2, BLACK);
00111         uLCD.circle(Robot::getYPosition(), Robot::getXPosition() - 9, 2, BLACK);
00112 }