Last active
January 22, 2019 19:57
-
-
Save JorTurFer/ccd04acd29012c50583e8dfc65bc1607 to your computer and use it in GitHub Desktop.
UNED FP Robot PIPO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "PIPO.h" | |
int main() { | |
int nOpcion; | |
bool bSeguir; | |
int nXDestino, nYDestino; | |
Robot PIPO; | |
PIPO = Robot(); | |
PIPO.Init(); | |
bSeguir = true; | |
while (bSeguir) { | |
system("cls"); | |
printf("Indica que es lo que quieres:"); | |
printf("\n\t1-Ir a posicion"); | |
printf("\n\t2-Mantenimiento"); | |
printf("\n\t3-Salir"); | |
printf("\n"); | |
scanf("%d", &nOpcion); | |
switch (nOpcion) { | |
case 1: | |
system("cls"); | |
printf("Indica la posicion donde debe moverse: X, Y\n"); | |
scanf("%d,%d",&nXDestino,&nYDestino); | |
PIPO.IrAPosicion(nXDestino,nYDestino); | |
break; | |
case 2: | |
PIPO.Mantenimiento(); | |
break; | |
case 3: | |
bSeguir = false; | |
break; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "PIPO.h" | |
void Robot::Init() { | |
m_nPosX = 0; | |
m_nPosY = 0; | |
m_nAngle = 0; | |
m_nTotalAngles = 0; | |
m_nTotalMoves = 0; | |
} | |
void Robot::IrAPosicion(int x, int y) { | |
int nGiro,nAvance; | |
m_nPaso = 1; | |
nGiro = 0; | |
nAvance = 0; | |
system("cls"); | |
//Primero llego en X | |
if (m_nPosX != x) { | |
//Si la X es mayor, tengo que ir hacia -180 | |
if (m_nPosX > x) { | |
//Compruebo a donde esta encarado | |
if (m_nAngle != 180) { | |
nGiro = _CalcularGiro(180); | |
_Girar(nGiro); | |
} | |
} else { //Si es menor, tengo que ir hacia 0 | |
//Compruebo a donde esta encarado | |
if (m_nAngle != 0) { | |
nGiro = _CalcularGiro(0); | |
_Girar(nGiro); | |
} | |
} | |
//Me muevo lo que falte, porque ya estoy encarado | |
_Mover_Adelante(abs(m_nPosX - x)); | |
} | |
//Luego llego en Y | |
if (m_nPosY != y) { | |
//Si la X es mayor, tengo que ir hacia -180 | |
if (m_nPosY > y) { | |
//Compruebo a donde esta encarado | |
if (m_nAngle != 270) { | |
nGiro = _CalcularGiro(270); | |
_Girar(nGiro); | |
} | |
} else { //Si es menor, tengo que ir hacia 0 | |
//Compruebo a donde esta encarado | |
if (m_nAngle != 90) { | |
nGiro = _CalcularGiro(90); | |
_Girar(nGiro); | |
} | |
} | |
_Mover_Adelante(abs(m_nPosY - y)); | |
} | |
system("pause"); | |
} | |
void Robot::Mantenimiento() { | |
system("cls"); | |
printf("Distancia total recorrida: %d\n", m_nTotalMoves); | |
printf("Grados girados totales: %d\n", m_nTotalAngles); | |
system("pause"); | |
} | |
void Robot::_Mover_Adelante(int casillas) { | |
printf("Paso %d._Mover_Adelante(%d)\n",m_nPaso,casillas); | |
m_nPaso = m_nPaso + 1; | |
switch (m_nAngle) { | |
case 0: | |
m_nPosX = m_nPosX + casillas; | |
break; | |
case 90: | |
m_nPosY = m_nPosY + casillas; | |
break; | |
case 180: | |
m_nPosX = m_nPosX - casillas; | |
break; | |
case 270: | |
m_nPosY = m_nPosY - casillas; | |
break; | |
} | |
m_nTotalMoves = m_nTotalMoves + casillas; | |
} | |
void Robot::_Girar(int giro) { | |
printf("Paso %d._Girar(%d)\n", m_nPaso, giro); | |
m_nPaso = m_nPaso + 1; | |
m_nAngle = (m_nAngle + 90 * giro) % 360; | |
m_nTotalAngles = m_nTotalAngles + (90 * giro); | |
} | |
int Robot::_CalcularGiro(int angleDestino) { | |
//En caso de tener que ir a una posicion mayor girando a la derecha, sumo 360 para el calculo | |
if (m_nAngle > angleDestino) { | |
angleDestino = angleDestino + 360; | |
} | |
return (angleDestino - m_nAngle) / 90; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct Robot { | |
void Init(); | |
void IrAPosicion(int x, int y); | |
void Mantenimiento(); | |
private: | |
void _Mover_Adelante(int casillas); | |
void _Girar(int giro); | |
int _CalcularGiro(int angleDestino); | |
int m_nPaso; | |
int m_nPosX; | |
int m_nPosY; | |
int m_nAngle; | |
int m_nTotalMoves; | |
int m_nTotalAngles; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment