youkoseki.com | LEGO

There are some Perl modules for Wiimote and Lego Mindstorms NXT. I use Linux::Input::Wiimote for Wiimote, and LEGO::NXT for NXT. If you have a PC with Bluetooth and it can communicate with your Wiimote and NXT, it's easy to do this with a tiny Perl code.

Here is my sample code. Enjoy.

#!/usr/bin/perl
use strict;
use Linux::Input::Wiimote;
use Data::Dumper;
use LEGO::NXT;
use LEGO::NXT::BlueComm;
use LEGO::NXT::Constants qw(:DEFAULT);  
use Net::Bluetooth;

# set your id
my $wiimote_id = "XX:XX:XX:XX:XX:XX"; 
my $nxt_id     = "XX:XX:XX:XX:XX:XX"; 

my $nxt = LEGO::NXT->new( new LEGO::NXT::BlueComm($nxt_id,1) );
my $wii = new Linux::Input::Wiimote;

$wii->wiimote_connect($wiimote_id);

while(1){
  $wii->wiimote_update();
  if($wii->get_wiimote_keys_up){
    if($wii->get_wiimote_keys_right){
      &rotate(38,75);
    }elsif($wii->get_wiimote_keys_left){
      &rotate(75,38);
    }else{
      &rotate(75,75);
    }
  }elsif($wii->get_wiimote_keys_down){
    if($wii->get_wiimote_keys_right){
      &rotate(-38,-75);
    }elsif($wii->get_wiimote_keys_left){
      &rotate(-75,-38);
    }else{
      &rotate(-75,-75);
    }
  }elsif($wii->get_wiimote_keys_right){
    &rotate(75,-75);
  }elsif($wii->get_wiimote_keys_left){
    &rotate(-75,75);
  }elsif($wii->get_wiimote_keys_a){
    $nxt->play_sound_file($NXT_RET, 0,'! Attention.rso');
  }else{
    &stop;
  }
}

sub rotate(){
  my $speed_a = $_[0];
  my $speed_c = $_[1];
  $nxt->set_output_state( 
    $NXT_NORET, 
    $NXT_MOTOR_A, 
    $speed_a, 
    $NXT_MOTOR_ON|$NXT_REGULATED,
    $NXT_REGULATION_MODE_MOTOR_SPEED,
    0,
    $NXT_MOTOR_RUN_STATE_RUNNING,
    0);
  $nxt->set_output_state( 
    $NXT_NORET, 
    $NXT_MOTOR_C, 
    $speed_c, 
    $NXT_MOTOR_ON|$NXT_REGULATED,
    $NXT_REGULATION_MODE_MOTOR_SPEED,
    0,
    $NXT_MOTOR_RUN_STATE_RUNNING,
    0);
}

sub stop(){
  $nxt->set_output_state( 
    $NXT_NORET, 
    $NXT_MOTOR_A, 
    0, 
    $NXT_MOTOR_ON|$NXT_REGULATED,
    $NXT_REGULATION_MODE_MOTOR_SPEED,
    0,
    $NXT_MOTOR_RUN_STATE_RAMPDOWN,
    0);
  $nxt->set_output_state( 
    $NXT_NORET, 
    $NXT_MOTOR_C, 
    0, 
    $NXT_MOTOR_ON|$NXT_REGULATED,
    $NXT_REGULATION_MODE_MOTOR_SPEED,
    0,
    $NXT_MOTOR_RUN_STATE_RAMPDOWN,
    0);
}