moves a robot using an accelerometer
Dependencies: 4DGL-uLCD-SE PinDetect mbed
Robot.cpp@0:69997732b823, 2017-10-03 (annotated)
- Committer:
- jchin32
- Date:
- Tue Oct 03 11:41:34 2017 +0000
- Revision:
- 0:69997732b823
Moves a robot sprite using an accelerometer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jchin32 | 0:69997732b823 | 1 | #include <iostream> |
jchin32 | 0:69997732b823 | 2 | #include "uLCD_4DGL.h" |
jchin32 | 0:69997732b823 | 3 | #include "Robot.h" |
jchin32 | 0:69997732b823 | 4 | #include "mbed.h" |
jchin32 | 0:69997732b823 | 5 | |
jchin32 | 0:69997732b823 | 6 | uLCD_4DGL uLCD(p28, p27, p29); |
jchin32 | 0:69997732b823 | 7 | |
jchin32 | 0:69997732b823 | 8 | Robot::Robot() { |
jchin32 | 0:69997732b823 | 9 | xPosition = 63; |
jchin32 | 0:69997732b823 | 10 | yPosition = 63; |
jchin32 | 0:69997732b823 | 11 | } |
jchin32 | 0:69997732b823 | 12 | |
jchin32 | 0:69997732b823 | 13 | Robot::Robot(int xPos, int yPos): xPosition(xPos), yPosition(yPos) { |
jchin32 | 0:69997732b823 | 14 | } |
jchin32 | 0:69997732b823 | 15 | |
jchin32 | 0:69997732b823 | 16 | void Robot::setXPosition(int xPos) { |
jchin32 | 0:69997732b823 | 17 | xPosition = xPos; |
jchin32 | 0:69997732b823 | 18 | } |
jchin32 | 0:69997732b823 | 19 | |
jchin32 | 0:69997732b823 | 20 | void Robot::setYPosition(int yPos) { |
jchin32 | 0:69997732b823 | 21 | yPosition = yPos; |
jchin32 | 0:69997732b823 | 22 | } |
jchin32 | 0:69997732b823 | 23 | |
jchin32 | 0:69997732b823 | 24 | int Robot::getXPosition() { |
jchin32 | 0:69997732b823 | 25 | return xPosition; |
jchin32 | 0:69997732b823 | 26 | } |
jchin32 | 0:69997732b823 | 27 | |
jchin32 | 0:69997732b823 | 28 | int Robot::getYPosition() { |
jchin32 | 0:69997732b823 | 29 | return yPosition; |
jchin32 | 0:69997732b823 | 30 | } |
jchin32 | 0:69997732b823 | 31 | |
jchin32 | 0:69997732b823 | 32 | void Robot::moveForward(int numMove) { |
jchin32 | 0:69997732b823 | 33 | setYPosition(yPosition + numMove); |
jchin32 | 0:69997732b823 | 34 | } |
jchin32 | 0:69997732b823 | 35 | |
jchin32 | 0:69997732b823 | 36 | void Robot::moveBack(int numMove) { |
jchin32 | 0:69997732b823 | 37 | setYPosition(yPosition - numMove); |
jchin32 | 0:69997732b823 | 38 | } |
jchin32 | 0:69997732b823 | 39 | |
jchin32 | 0:69997732b823 | 40 | void Robot::moveRight(int numMove) { |
jchin32 | 0:69997732b823 | 41 | setXPosition(xPosition + numMove); |
jchin32 | 0:69997732b823 | 42 | } |
jchin32 | 0:69997732b823 | 43 | |
jchin32 | 0:69997732b823 | 44 | void Robot::moveLeft(int numMove) { |
jchin32 | 0:69997732b823 | 45 | setXPosition(xPosition - numMove); |
jchin32 | 0:69997732b823 | 46 | } |
jchin32 | 0:69997732b823 | 47 | |
jchin32 | 0:69997732b823 | 48 | void Robot::inputMove() { |
jchin32 | 0:69997732b823 | 49 | char direction; |
jchin32 | 0:69997732b823 | 50 | int numSpaces; |
jchin32 | 0:69997732b823 | 51 | std::cout << "Please enter a direction for the robot to move " |
jchin32 | 0:69997732b823 | 52 | << "(F, B, L, or R), followed by the integer distance: \n"; |
jchin32 | 0:69997732b823 | 53 | std::cin >> direction; |
jchin32 | 0:69997732b823 | 54 | std::cin >> numSpaces; |
jchin32 | 0:69997732b823 | 55 | switch (direction) { |
jchin32 | 0:69997732b823 | 56 | case 'F': |
jchin32 | 0:69997732b823 | 57 | moveForward(numSpaces); |
jchin32 | 0:69997732b823 | 58 | std::cout << "Robot is moving forward " << numSpaces << " units.\n"; |
jchin32 | 0:69997732b823 | 59 | break; |
jchin32 | 0:69997732b823 | 60 | case 'B': |
jchin32 | 0:69997732b823 | 61 | moveBack(numSpaces); |
jchin32 | 0:69997732b823 | 62 | std::cout << "Robot is moving backward " << numSpaces << " units.\n"; |
jchin32 | 0:69997732b823 | 63 | break; |
jchin32 | 0:69997732b823 | 64 | case 'L': |
jchin32 | 0:69997732b823 | 65 | moveLeft(numSpaces); |
jchin32 | 0:69997732b823 | 66 | std::cout << "Robot is moving left " << numSpaces << " units.\n"; |
jchin32 | 0:69997732b823 | 67 | break; |
jchin32 | 0:69997732b823 | 68 | case 'R': |
jchin32 | 0:69997732b823 | 69 | moveRight(numSpaces); |
jchin32 | 0:69997732b823 | 70 | std::cout << "Robot is moving right " << numSpaces << " units.\n"; |
jchin32 | 0:69997732b823 | 71 | break; |
jchin32 | 0:69997732b823 | 72 | default: |
jchin32 | 0:69997732b823 | 73 | std::cout << "Invalid move.\n"; |
jchin32 | 0:69997732b823 | 74 | } |
jchin32 | 0:69997732b823 | 75 | } |
jchin32 | 0:69997732b823 | 76 | |
jchin32 | 0:69997732b823 | 77 | void Robot::displayPosition() { |
jchin32 | 0:69997732b823 | 78 | std::cout << "Robot is located at x = " << xPosition |
jchin32 | 0:69997732b823 | 79 | << ", y = " << yPosition << "\n"; |
jchin32 | 0:69997732b823 | 80 | } |
jchin32 | 0:69997732b823 | 81 | |
jchin32 | 0:69997732b823 | 82 | void Robot::draw() { |
jchin32 | 0:69997732b823 | 83 | uLCD.rectangle(Robot::getYPosition() - 5, Robot::getXPosition() - 5, |
jchin32 | 0:69997732b823 | 84 | Robot::getYPosition() + 5, Robot::getXPosition() + 5, WHITE); |
jchin32 | 0:69997732b823 | 85 | uLCD.rectangle(Robot::getYPosition() - 3, Robot::getXPosition() + 4, |
jchin32 | 0:69997732b823 | 86 | Robot::getYPosition() + 3, Robot::getXPosition() + 2, WHITE); |
jchin32 | 0:69997732b823 | 87 | //uLCD.line(this.getX() - 5, this,getY(), this.getX() - 7, this.getY(), |
jchin32 | 0:69997732b823 | 88 | // WHITE); |
jchin32 | 0:69997732b823 | 89 | // uLCD.line(this.getX() + 5, this,getY(), this.getX() + 7, this.getY(), |
jchin32 | 0:69997732b823 | 90 | // WHITE); |
jchin32 | 0:69997732b823 | 91 | // uLCD.line(this.getX() - 7, this,getY(), this.getX() - 7, this.getY() - 2, |
jchin32 | 0:69997732b823 | 92 | // WHITE); |
jchin32 | 0:69997732b823 | 93 | // uLCD.line(this.getX() + 7, this,getY(), this.getX() + 7, this.getY() - 2, |
jchin32 | 0:69997732b823 | 94 | // WHITE); |
jchin32 | 0:69997732b823 | 95 | uLCD.line(Robot::getYPosition(), Robot::getXPosition() - 5, |
jchin32 | 0:69997732b823 | 96 | Robot::getYPosition(), Robot::getXPosition() - 7,WHITE); |
jchin32 | 0:69997732b823 | 97 | uLCD.circle(Robot::getYPosition() - 2, Robot::getXPosition() - 2, 2, WHITE); |
jchin32 | 0:69997732b823 | 98 | uLCD.circle(Robot::getYPosition() + 2, Robot::getXPosition() - 2, 2, WHITE); |
jchin32 | 0:69997732b823 | 99 | uLCD.circle(Robot::getYPosition(), Robot::getXPosition() - 9, 2, WHITE); |
jchin32 | 0:69997732b823 | 100 | } |
jchin32 | 0:69997732b823 | 101 | |
jchin32 | 0:69997732b823 | 102 | void Robot::erase() { |
jchin32 | 0:69997732b823 | 103 | uLCD.rectangle(Robot::getYPosition() - 5, Robot::getXPosition() - 5, |
jchin32 | 0:69997732b823 | 104 | Robot::getYPosition() + 5, Robot::getXPosition() + 5, BLACK); |
jchin32 | 0:69997732b823 | 105 | uLCD.rectangle(Robot::getYPosition() - 3, Robot::getXPosition() + 4, |
jchin32 | 0:69997732b823 | 106 | Robot::getYPosition() + 3, Robot::getXPosition() + 2, BLACK); |
jchin32 | 0:69997732b823 | 107 | uLCD.line(Robot::getYPosition(), Robot::getXPosition() - 5, |
jchin32 | 0:69997732b823 | 108 | Robot::getYPosition(), Robot::getXPosition() - 7, BLACK); |
jchin32 | 0:69997732b823 | 109 | uLCD.circle(Robot::getYPosition() - 2, Robot::getXPosition() - 2, 2, BLACK); |
jchin32 | 0:69997732b823 | 110 | uLCD.circle(Robot::getYPosition() + 2, Robot::getXPosition() - 2, 2, BLACK); |
jchin32 | 0:69997732b823 | 111 | uLCD.circle(Robot::getYPosition(), Robot::getXPosition() - 9, 2, BLACK); |
jchin32 | 0:69997732b823 | 112 | } |