Skip to content

Commit

Permalink
Better comments and a "move until I say otherwise" mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ab3nd committed Feb 16, 2011
1 parent fdc9394 commit 5c3cfca
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
3 changes: 2 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ int main(int argc, char* argv[])
if(mc.init() == 0){
mc.turn(LEFT, 1.5);
mc.turn(UP, 1);
mc.fire();
mc.turn(RIGHT, 1.5);
mc.turn(DOWN, 1);
//mc.fire();
//
}else{
cerr << "Failed to initialize missile launcher" << endl;
}
Expand Down
56 changes: 36 additions & 20 deletions missile_launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,52 @@ void MissileLauncher::fire()

void MissileLauncher::turn(MissileCmd direction, double delay)
{
if(direction == FIRE)
if (direction == FIRE)
{
fire();
}
else if(direction == STOP)
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();
if (delay = 0)
{
/* Start moving. This means it keeps moving until you tell it to stop.
* Good for feedback loops, bad for not hitting the stops and abusing
* the slip clurch.
*/
sendMsg(direction);
}
else
{
/* Move for the specified time period, then stop.
* This is good for making little scripts, but assumes you know
* the pose of the gun. It's a shame there's little to no feedback from it.
*/
//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(){
void MissileLauncher::stop()
{
int ret = sendMsg(STOP);
if(ret < 0)
if (ret < 0)
{
cerr << "Cannot stop, error: " << ret << endl;
}
Expand All @@ -74,15 +90,15 @@ int MissileLauncher::sendMsg(MissileCmd control)
{
char msg[8];

for( int ii = 0; ii < 8; ii++)
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 )
if (ret < 0)
{
cerr << "Cannot send command, error:" << ret << endl;
}
Expand Down

0 comments on commit 5c3cfca

Please sign in to comment.