Okay, I was working on my idea of controlling the robot over SSH with kermit and I hacked up a Perl script that can control the PWMs with my joystick. Disclaimer: this code is a dirty hack. First of all, I wrote it. Secondly, using Expect to control a robot through a makeshift Perl script is probably not the most ideal solution.
Code:
#!/usr/bin/perl
# Copyright 2008 Jonathan Glines
# auntieNeo@gmail.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use Expect;
use Linux::Input::Joystick;
my $login = "myLogin";
my $password = "myPassword";
my $address = "192.168.1.106"; # the address of my laptop connected to the Vex via serial
# connect via ssh
my $exp = Expect->spawn("ssh -l $login $address");
my $patidx = $exp->expect(20, 'password:');
if($patidx == 1)
{
$exp->send("$password\n"); # ssh password
}
# open the serial interface with kermit
$patidx = $exp->expect(10, '~$ '); # '~$ ' is the prompt my laptop happens to give
if($patidx == 1)
{
$exp->send("sudo kermit ~/code/scripts/vex\n"); # ~/code/scripts/vex is a simple kermit script that sets the serial connection settings
}
# enter the sudo password
$patidx = $exp->expect(5, "password for $login:");
if($patidx == 1)
{
$exp->send("$password\n"); # sudo password
}
sleep(2);
$exp->send("\r"); # send the vex a return so that we get a prompt
my $js = Linux::Input::Joystick->new('/dev/input/js0');
my $jsConstant = 0.003875; # constant that reduces Linux joystick values (32767 through -32767) to Vex joystick values (127 through -127)
while(true)
{
my @event = $js->poll(0.01);
foreach(@event)
{
my %test = %{ $_ };
if($test{'type'} == 2) # if it's a joystick axis event
{
if($test{'number'} == 1) # if the axis in question is y of the left stick (on my PSX controller)
{
$patidx = $exp->expect(5, '>'); # wait for a prompt from the Vex
if($patidx == 1)
{
$exp->send('p01' . round(($test{'value'} * $jsConstant) + 127) . "\r"); # set pwm01 to the left joystick
}
}
elsif($test{'number'} == 2) # if the axis in question is y of the right stick (on my PSX controller)
{
$patidx = $exp->expect(2, '>'); # wait for a prompt from the Vex
if($patidx == 1)
{
$exp->send('p02' . round(($test{'value'} * $jsConstant) + 127) . "\r"); # set pwm02 to the right joystick
}
}
}
}
}
$exp->soft_close();
sub round {
my($number) = shift;
return int($number + .5 * ($number <=> 0));
}
Basically what this does is it uses
Perl's Expect module to control SSH and kermit like its puppets and then
Perl's joystick module to send joystick values to the robot. I wrote a rudimentary command interface on the Vex to control the PWMs. Basically the command to set pwm01 to 25 would be "p0125". So, if I can get these PWMs to actually work, I would probably have the worlds first Vex programmed in Perl.
Btw, this thing actually works. Here's a screenshot of my desktop (which the joystick is connected to) sending messages to my laptop (which the vex is connected to). My laptop is connected to the network with wifi, so this is all wireless.
