From fdc93942e9a63ec9d0fabec0dc95511bc24f986b Mon Sep 17 00:00:00 2001 From: Abe Shultz Date: Wed, 16 Feb 2011 11:10:34 -0500 Subject: [PATCH] Now runs without a lot of errors. Also, the protocol description at http://www.amctrl.com/rocketlauncher.html is wrong. --- main.cpp | 6 +++- missile_launcher.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++-- missile_launcher.h | 10 ++++++ 3 files changed, 93 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index 61b8b83..8cd03be 100644 --- a/main.cpp +++ b/main.cpp @@ -18,7 +18,11 @@ int main(int argc, char* argv[]) { MissileLauncher mc; if(mc.init() == 0){ - mc.fire(); + mc.turn(LEFT, 1.5); + mc.turn(UP, 1); + mc.turn(RIGHT, 1.5); + mc.turn(DOWN, 1); + //mc.fire(); }else{ cerr << "Failed to initialize missile launcher" << endl; } diff --git a/missile_launcher.cpp b/missile_launcher.cpp index 4db2d16..a45039d 100644 --- a/missile_launcher.cpp +++ b/missile_launcher.cpp @@ -4,7 +4,7 @@ * Created on: Feb 11, 2011 * Author: ams * - * Creates and maintains info about a Dream Cheeky Missile Launcher. + * OO interface to the Dream Cheeky Missile Launcher * */ @@ -12,7 +12,82 @@ void MissileLauncher::fire() { - cout << "Fire ze missiles!!!" << endl; + cout << "Fire zee missiles!!!" << endl; + //need to wait 6.77 seconds to fire + struct timespec toWait; + toWait.tv_sec = 6; + toWait.tv_nsec = 770000000; + + const struct timespec* delay = &toWait; + struct timespec* remainder; + + //Start launch sequence and wait for launch + if (sendMsg(FIRE) < 0) + { + cerr << "Could not fire." << endl; + return; + } + nanosleep(delay, remainder); + + //Stop, so no more missiles launch + stop(); +} + +void MissileLauncher::turn(MissileCmd direction, double delay) +{ + if(direction == FIRE) + { + fire(); + } + else if(direction == STOP) + { + stop(); + } + else + { + //Convert the delay into a timespec + struct timespec toWait; + toWait.tv_sec = (int)delay; + toWait.tv_nsec = (delay - toWait.tv_sec) * 100000000; + + const struct timespec* delay = &toWait; + struct timespec* remainder; + + //Start turn and wait for movement + sendMsg(direction); + nanosleep(delay, remainder); + + //Stop turning + stop(); + } +} + +void MissileLauncher::stop(){ + int ret = sendMsg(STOP); + if(ret < 0) + { + cerr << "Cannot stop, error: " << ret << endl; + } +} + +int MissileLauncher::sendMsg(MissileCmd control) +{ + char msg[8]; + + for( int ii = 0; ii < 8; ii++) + { + msg[ii] = 0x0; + } + + //send control message + msg[0] = control; + int ret = usb_control_msg(launcher, 0x21, 0x9, 0x200, 0, msg, 8, 1000); + if(ret < 0 ) + { + cerr << "Cannot send command, error:" << ret << endl; + } + + return ret; } void MissileLauncher::deinit() @@ -57,6 +132,7 @@ int MissileLauncher::init() cout << driverName << " can't be detached" << endl; return 1; } + cout << "Detached " << driverName << endl; } //Claim it ourselves diff --git a/missile_launcher.h b/missile_launcher.h index b0392db..1f6fff1 100644 --- a/missile_launcher.h +++ b/missile_launcher.h @@ -13,14 +13,24 @@ using namespace std; +typedef enum +{ + FIRE = 0x10, LEFT = 0x4, RIGHT = 0x8, UP = 0x1, DOWN = 0x2, STOP = 0x0 +} MissileCmd; + class MissileLauncher { public: + //Setup and teardown int init(); void deinit(); + //Motion commands void fire(); + void stop(); + void turn(MissileCmd direction, double duration); private: usb_dev_handle* launcher; + int sendMsg(MissileCmd cmd); }; #endif /* MISSILE_LAUNCHER_H_ */