Skip to content

Commit

Permalink
Now runs without a lot of errors. Also, the protocol description at h…
Browse files Browse the repository at this point in the history
  • Loading branch information
ab3nd committed Feb 16, 2011
1 parent e9f3994 commit fdc9394
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
6 changes: 5 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
80 changes: 78 additions & 2 deletions missile_launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,90 @@
* 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
*
*/

#include "missile_launcher.h"

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()
Expand Down Expand Up @@ -57,6 +132,7 @@ int MissileLauncher::init()
cout << driverName << " can't be detached" << endl;
return 1;
}
cout << "Detached " << driverName << endl;
}

//Claim it ourselves
Expand Down
10 changes: 10 additions & 0 deletions missile_launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */

0 comments on commit fdc9394

Please sign in to comment.