Robots suiveurs 3 - Code Contrôle au Clavier
Révision datée du 17 avril 2020 à 10:23 par Poulain (discussion | contributions) (Page créée avec « <nowiki> #include <webots/Robot.hpp> #include <iostream> // Added a new include file #include <webots/Motor.hpp> #include <webots/Keyboard.hpp> #define TIME_STEP 64 #de... »)
#include <webots/Robot.hpp>
#include <iostream>
// Added a new include file
#include <webots/Motor.hpp>
#include <webots/Keyboard.hpp>
#define TIME_STEP 64
#define MAX_SPEED 6.28
// All the webots classes are defined in the "webots" namespace
using namespace webots;
int main(int argc, char **argv) {
Robot *robot = new Robot();
// get a handler to the motors and set target position to infinity (speed control)
Motor *leftMotor = robot->getMotor("left wheel motor");
Motor *rightMotor = robot->getMotor("right wheel motor");
leftMotor->setPosition(INFINITY);
rightMotor->setPosition(INFINITY);
// set up the motor speeds at 10% of the MAX_SPEED.
leftMotor->setVelocity(0);
rightMotor->setVelocity(0);
Keyboard keyboard = Keyboard();
keyboard.enable(50);
while (robot->step(TIME_STEP) != -1){
int key = keyboard.getKey();
//Fonctionne aussi :
/*
if(key ==keyboard.CONTROL+'M')
printf("You can control the drone with your computer keyboard:\n");
*/
//std::cout << key << std::endl;
if (key == -1) {
leftMotor->setVelocity(0);
rightMotor->setVelocity(0);
}
while (key > 0) {
//std::cout << "touch is pressed M" << std::endl;
switch (key) {
case 'Z':
leftMotor->setVelocity(MAX_SPEED);
rightMotor->setVelocity(MAX_SPEED);
break;
case 'S':
leftMotor->setVelocity(-MAX_SPEED);
rightMotor->setVelocity(-MAX_SPEED);
break;
case 'Q':
leftMotor->setVelocity(-0.49*MAX_SPEED);
rightMotor->setVelocity(0.49*MAX_SPEED);
break;
case 'D':
leftMotor->setVelocity(0.49*MAX_SPEED);
rightMotor->setVelocity(-0.49*MAX_SPEED);
break;
case 'A':
leftMotor->setVelocity(0.5*MAX_SPEED);
rightMotor->setVelocity(0.75*MAX_SPEED);
break;
case 'E':
leftMotor->setVelocity(0.75*MAX_SPEED);
rightMotor->setVelocity(0.5*MAX_SPEED);
break;
default:
leftMotor->setVelocity(0);
rightMotor->setVelocity(0);
break;
}
key = keyboard.getKey();
}
//std::cout << "touch is pressed M" << std::endl;
}
delete robot;
return 0;
}