Vamos a crear
un programa en C++ para que el LEGO NXT combine varias acciones de detección y
movimiento de manera simultánea.
Proponemos el código siguiente:
#include
#include
#include
#include
#include "nxt.h"
//using namespace std;
// Set up the NXT
Connection *connection = new Nxt_network();
Sensor *sensor1 = new Touch(IN_1, connection);
Sensor *sensor2 = new Light(IN_3, connection, LED_ON);
Sensor *sensor3 = new Sound(IN_2, connection, DBA_MODE);
Motor *motorB = new Motor(OUT_B, connection);
Motor *motorC = new Motor(OUT_C, connection);
int main()
{
try{
cout << "Try to connect to the NXT" << endl;
Server_settings settings;
connection->connect(1000, "127.0.0.1", settings); // Connect to localhost
cout << "Connected" << endl;
cout << "Press touch sensor to make motors B and C turn - hit any key to end" << endl;
while(!_kbhit()){ // Hit a key to end
if(sensor1->read()){
motorB->on(-75);
motorC->on(-75);
}
//Sensor de Luz: Lego lee el valor del sensor de luz y, además, enciende y apaga el LED de dicho sensor cada cierto tiempo.
while(!_kbhit()){ // Hit a key to end (if not, repeat and repeat next actions)
sensor2->set(LED_OFF);
cout << sensor2->print() << endl; // Returns a string with mode dependent sensor value
cout << sensor2->read() << endl; // Mode dependent value
Sleep(1000);
sensor2->set(LED_ON);
cout << sensor2->print() << endl; // Returns a string with mode dependent sensor value
cout << sensor2->read() << endl; // Mode dependent value
Sleep(1000);
}
//Sensor de sonido: Lego mide el nivel de sonido del entorno y lo muestra por pantalla.
while(!_kbhit()){//hit a key to end
cout << sensor3->print() << endl; //returns a string can be used on all sensor types
cout << sensor3->read() << endl;
}
else{
motorB->stop();
motorC->stop();
}
}
connection->disconnect();
}
catch (Nxt_exception& e){
// Some error occurred - print it out
cout << e.what() << endl;
cout << "error code: " << e.error_code() << endl;
cout << "error type: " << e.error_type() << endl;
cout << e.who() << endl;
connection->disconnect();
}
return 0;
}
0 comentarios:
Publicar un comentario