Goal: Simulate the reactions of a nervous / lonely puppy using servo's + the mbed LPC1768.
Dependencies: mbed-rtos-edited mbed Servo C12832
nervousPuppy.cpp
- Committer:
- SeanDoyle
- Date:
- 2015-01-14
- Revision:
- 3:74dfce05dd99
- Parent:
- 2:8415bea33a95
- Child:
- 4:2b47356f4b7d
File content as of revision 3:74dfce05dd99:
#include "nervousPuppy.h" /** * Constructor - contains running loop */ nervousPuppy::nervousPuppy(){ bool isRunning = true; while(isRunning){ if(shutdown()){//TurnOff isRunning = !isRunning; } else if(isScared()){//MoveBack scared = true; playerError = playerDistance - SCARED; if(calculateAngle("Vertical") != 0)changePosition("tilt",calculateAngle("Vertical")); else if(calculateAngle("Horizontal") != 0)changePosition("rotate",calculateAngle("Horizontal")); else isRunning = false; scared = false; } else if(isLonely()){// MoveForward lonely = true; playerError = playerDistance - LONELY; if(calculateAngle("Vertical") !=0)changePosition("tilt",calculateAngle("Vertical")); else if(calculateAngle("Horizontal") != 0)changePosition("rotate",calculateAngle("Horizontal")); else isRunning = false; lonely = false; } } } /** * Calculates the angle required to bring the 'puppy' to a 'safe distance' * Returns 0 if it cannot find a 'safe distance' */ float nervousPuppy::calculateAngle(string axis){ float deltaDist = 0.0; float limiter,y; if(axis == "Vertical") limiter = SERVO_TILT_LIMIT; else limiter = SERVO_ROTATE_LIMIT; for(float theta = 0.0; theta < limiter; ++theta){ y = RADIUS*Sin(theta); deltaDist = sqrt(pow(y,2) + pow(playerDistance,2)); if(scared){ if((deltaDist - SCARED) > SCARED) return theta; }else if(lonely){ if((deltaDist - LONELY) < LONELY) return theta; } } return 0.0; } /** * Move 'puppy' to the calculated 'safe' point */ void nervousPuppy::changePosition(string servo,float angle){ if(servo == "tilt"){} else if(servo == "rotate"){} } /** * Thread -> Running sonar to detect player */ void nervousPuppy::detectPlayer(){ } /** check if we shutdown **/ bool nervousPuppy::shutdown(){ if(playerDistance < SHUTDOWN)return true; else return false; } /** check if player is to far away **/ bool nervousPuppy::isLonely(){ if(playerDistance > LONELY)return true; else return false; } /** check if player is to close **/ bool nervousPuppy::isScared(){ if(playerDistance < SCARED)return true; else return false; } /** get player distance value **/ float nervousPuppy::getPlayerDistance(){ return playerDistance; } /** set player distance value **/ void nervousPuppy::setPlayerDistance(float dist){ playerDistance = dist; } int main(){ nervousPuppy(); }