PDA

View Full Version : Making a Pan Tilt work with Joy stick



Daniel
12-11-2007, 01:14 PM
Does anybody know how to make the Phidget servo 4 motor work with the computer joyu stick? O need a little help on it. Thanks Daniel my e mail [email protected]

Alex
12-11-2007, 01:49 PM
Hey Daniel,

The Phidget 4motor servo controller interfaces with the computer via USB through an API. Research how to take readings from a joystick using your computer and bring those values into the programming language you are using with the Phidget 4motor controller.

I used DirectX Direct Input a coupe years back. When I downloaded the DirectX SDK, I found a really cool example that showed me how to interface with a joystick using C#.

I think this is the link (it's been updated since I downloaded it):

http://www.microsoft.com/downloads/details.aspx?familyid=86cf7fa2-e953-475c-abde-f016e4f7b61a&displaylang=en

Alex
12-14-2007, 11:39 AM
Here's a short little clip of us controlling a robotic arm via DirectX Direct Input:


http://www.youtube.com/watch?v=-e6Ggcxavns

darkback2
02-22-2008, 10:49 AM
I work with Max/MSP...so this may not apply for you,

You could take the x and y data from the joystick using the (hi) object. This will probably come in as values either from 0 - 1 or 0 - 128 for both X, and Y. First convert this data into 0 - 180 for the full range of the servo. You can simply connect that data stream directly to the phidgetservo object, the X assigned to the servo output used for panning, and the Y assigned to the servo output for tilt...Or you could find the change in X and Y coupled with a threshold, and use that. The latter would be a bit harder to get working, but would offer much more valuable information, and stable control.

I'll try to post a program using the mouse instead of a joystick this weekend. I would use a joystick, but the data from the hi object varies from device to device.

darkback2
02-22-2008, 09:33 PM
Ok...so I kind of went overboard with this one. First of all today while I was supposed to be teaching the kidlets, I got wiicontrol working as a Mac Application using MAX/MSP. You can see it work in the youtube video below. Next, I made a mouse controlled version for the Mac called Mousecontrol. Finally I moved mousecontrol over to my PC and got that working as an EXE file called mini.
Here are some screen shots of Wiicontrol:
http://myhitechlife.net/screenshot01.jpg
http://myhitechlife.net/screenshot02.jpg
Check out the video here:

http://www.youtube.com/watch?v=FqBCA3eWPZ0
You can download Wiicontrol (mac only):
www.myhitechlife.net/robotchoppers/wiicontrol.zip
You can download mousecontrol (pc version)
www.myhitechlife.net/mickey.zip

Hope this helps.

Alex
02-25-2008, 09:37 AM
That is too cool db, thanks:) I love seeing these Wiimote applications in action!

Too bad I don't have a Mac:(

ZeeGee
02-28-2008, 01:44 PM
I actually just did this recently.

My setup: Logitech PS3 controller with USB interface, SSC-32 controller

My program is based on this C# USB HID Device component here: http://www.codeproject.com/KB/cs/USB_HID.aspx

Basically the sample program will let you read data from the HID device (the joystick), then I move the stick around to see which part of data corresponds to which channel, then I modified the program to send command to SSC-32 to control the position of the servos

My original idea is to use a PC based console to send command to Nokia N810 over wifi, then N810relays the command to SSC-32 using a bluetooth-serial adapter, but I found there's some delay in command transmission with this approach. Considering wifi & bt both uses 2.4G band and have similar RF power, I might just hook the bluetooth-serial adapter to PC directly

DresnerRobotics
02-28-2008, 02:07 PM
Would you mind sharing your project folder/file on your USB joystick/ssc-32 program? I'm trying to learn C# and would love to see a working example with the SSC-32.

You can email it to me at [email protected]

Thanks!

Alex
02-28-2008, 02:10 PM
You can email it to me at [email protected]

Or upload it here as an attachment:)

We're also looking into adding a Software Projects section here in the TRC. It's just a matter of finding the one we like the most. That'll be another great resource here!

DresnerRobotics
02-28-2008, 02:14 PM
Uploading would probably be preferable anyway, gmail can be a bitch about attachments (it's antivirus has denied people sending projects before)

ZeeGee
02-28-2008, 02:17 PM
sure, folks. I'll attach it here once I get home from work.

DresnerRobotics
02-28-2008, 02:33 PM
Woo! Thanks a ton!

Learning by example is a great way to go for code ;)

ZeeGee
02-28-2008, 10:04 PM
So here's the code in the attachment. I did not get a chance to work on it, so it basically won't work out-of-box on your PC because it requires certain set up.

The code receives data from a USB HID (Human Interface Device, in my case, the joystick), and send corresponding command to a network socket. So to make it work for you, you'll need to send command to serial port instead. I have not got a chance to look into that approach, but I see there's a serial port component in C# library.

I built this code with MS Visual C# Express Edition.

Any questions, please let me know.

indy007
06-09-2008, 01:24 PM
Well I got my hands on my first phidgets servo controller over the weekend. I enjoyed Alex W.'s joystick tutorial, but ran into some hurdles integrating it into another project. I just finished this up, and figured out how easy it really is. My version is based on the "motion" sample from AForge, because I wanted a webcam feed/motion detection, joystick control, and phidgets info on the same form. AFAIK, "motion" is what the WaterHobo is based on.

So here it goes:

Get Alex W's CSharpUSBJoystick tutorial. (convert to C# 2008 if neccessary, makes no difference)
Open the USBJoystick.cs file, select all, and copy it to your clipboard.

Open your current project.
Add a new class file.
Paste the clipboard contents into that file.
Change the namespace entry on line 20 from CSharpUSBJoystick to your project's namespace (in my adapted usage, it's simply "motion").

So now you have the USBJoystick class setup on your project. Time to use it.

In your primary form, right under the public class Form1 : blah blah blah { add the lines:

USB_Joystick js;
Phidgets.Servo servo = new Phidgets.Servo(); // optional, varies with servo controller

Under the procedure for the main form, before InitializeComponent(), at the very start, add:

try
{
js = newUSB_Joystick(0, 1000, .20, this);
}
catch (Exception)
{
throw;
}
js.PresetValues(); // possibly optional?

// Failure to include this exception handler means lots of software crashes.
// This was learned the hard way, and cost me a few hours.
// I didn't notice these lines when dissecting CSharpUSBJoystick originally. Do not skip it!

Okay that should have the js object created and ready to roll. Now comes an important part, how to poll the joystick for new information.

Create a timer control on your form. Set it to enabled, and go with a low interval (100 works for me). We want to check the joystick positions, and adjust the servo positions accordingly. Double click on that timer and open up the code window. Add this to the timer function:

js.GetData();
updatePositions(); // optional, use whatever method to update your servo positions you like

Now your options to react to the joystick are up to you. Here's how I did it, using 2 servos for pan (0) and tilt (1). The servo and joystick labels simply show their current positions on the form. This technique was liberally taken from Alex W.'s tutorial. You can react to the joystick movements directly from the js.State objects, or copy them to a form control and react to that.

private void updatePositions()
{
Servo0label.Text = servo.servos[0].Position.ToString();
Servo1label.Text = servo.servos[1].Position.ToString();
joystickXlabel.Text = js.State.X.ToString();
joystickYlabel.Text = js.State.Y.ToString();
double ServoStep = 0.2;
servo.servos[0].Position = ((111+js.State.X)*ServoStep);
servo.servos[1].Position = ((119+js.State.Y)*ServoStep);
}

// 111 & 119 represent my "center" positions for the servos.
// on-the-fly adjustments are unncessary with my fixed mount setup.
// servo step is set for 0-1000 joystick axis ranges

This should be readily adaptable to any controller that's got C# functionality, and any of the phidgets gear you have attached. I finished it all up in 1 afternoon, with only highschool level prior coding experience, and absolutely none using MS Visual Studio C# Express. With these snippets, anybody can get joystick control running in minutes. Alex is my hero.

Adrenalynn
06-09-2008, 01:32 PM
Thanks for posting that back! That's great info! +rep for you!

Any chance that you might post your merged project back into the downloads? Sounds super interesting!

indy007
06-09-2008, 02:17 PM
Sure thing! This is motion & csharpusbjoystick mashed together.

It will very likely CRASH if you do not:
* Have a usb joystick plugged in (I use a Thrustmaster Cougar HOTAS setup)
* Have a phidgetts usb servo controller plugged in.
* Have PhidgetsManager running on your task bar.

Also, the 111 & 119 numbers in the updatePositions function should be changed to your servos center positions (as seen in PhidgetsManager when you double click the device), or else I could see some possible damage happening.
note: in this template, it's called updateForms() instead of the updatePositions() used in the previous post.

Be warned, there's a little bit of slop (leftover code from deleted controls used in testing), and the form labels will say "gun turret", because this control system is being built to run a drive-by-wire, ridable, paintball tank that will play in 26hour events... which should explain my burning desire for a hybrid powerplant... :veryhappy:

Adrenalynn
06-09-2008, 02:39 PM
Insanely cool, thanks!

I've been working on a large long-run battery powered 'bot's drivetrain for awhile. I can offer some advice if you're still working on your drivetrain.

LinuxGuy
06-09-2008, 02:42 PM
That is too cool db, thanks:) I love seeing these Wiimote applications in action!

Too bad I don't have a Mac:(
Why can't you do that on a PC too? Even under Windows? :veryhappy:

8-Dale

indy007
06-09-2008, 03:19 PM
Insanely cool, thanks!

I've been working on a large long-run battery powered 'bot's drivetrain for awhile. I can offer some advice if you're still working on your drivetrain.

There will be posts begging for input once I get that far in the fabrication stage. The estimated weight is 250-300 pounds ready for battle. The cruise speed (defined by game rules & insurance regs) is only 5mph... but while the terrain here is nice & flat, other places that I'll travel to can have some pretty nasty grades... so the more torque the better.

Adrenalynn
06-09-2008, 03:25 PM
Why can't you do that on a PC too? Even under Windows? :veryhappy:

8-Dale

I can.

----
Indy - my current platform tops at 15mph right now, chain driven, and weighs 280lbs. Runtimes with a 100lb payload are running about 9hrs, but my turn-over time for spare batteries is only about 6hrs.

Are you going tracked or wheeled?

Alex
06-09-2008, 03:58 PM
This should be readily adaptable to any controller that's got C# functionality, and any of the phidgets gear you have attached. I finished it all up in 1 afternoon, with only highschool level prior coding experience, and absolutely none using MS Visual Studio C# Express. With these snippets, anybody can get joystick control running in minutes. Alex is my hero.gee thanks:o I'm just glad that you found that project useful. I had a nasty time learning all of the backend DX9 stuff, so when I finally understood it well enough, I built that demo and commented the livin hell out of it:D

I'm with Adrenalynn though, do you mind uploading this project to our downloads section (http://forums.trossenrobotics.com/downloads.php)? If you're interested, I think this would also make an awesome tutorial (http://forums.trossenrobotics.com/forumdisplay.php?f=119)!

indy007
06-09-2008, 06:49 PM
Done! http://forums.trossenrobotics.com/./downloads.php?do=file&id=49

I updated this one with a bunch of comments on the MainForm.cs file, and a short one on the USBJoystick.cs file. Basically just notes from writing the quick how-to. The upload has a link back to my post with the tutorial.

Alex, you saved many of us a lot of book work and time :) I just bashed together the legos until something cool appeared. and Adrenalynn, you'll have a pm shortly! I really could use some advice.

Alex
06-10-2008, 08:36 AM
I didn't realize that your project was so in depth till I read the description! +rep:)

You did all of that with only prior high school programming experience?!? WOW!! Where on Earth did you go to high school?

Man, I'm so jealous of kids going through high school nowadays. When I went there (yes, I'm showing my age :p), they didn't even have a computer class till like my last semester as a senior:( Granted, I did at the time, live in a town surrounded by cornfields (http://maps.google.com/maps?ie=UTF-8&oe=utf-8&rls=org.mozilla:en-US:official&client=firefox-a&q=Streator,+IL,+USA&um=1&sa=X&oi=geocode_result&resnum=1&ct=title) (the town is a decent size though, bout 20k)

indy007
06-10-2008, 10:59 AM
Actually I graduated from HS in '98 :) It was Turbo Pascal in CompSci 1 on 486s and C/C++ in AP CompSci 2 on the same 486s and some pentiums. Considering I never had any of the pre-req's, I shouldn't have been allowed into any of the classes, let alone placed on the UIL competition team. Since then I did some time at an ISP tech support, then the administrator's team, but no coding there. I didn't write any code until I had to teach myself PHP & VB 2 years ago to write some tools for my auto parts brokerage business. That's all it really takes to slap together other peoples pre-written classes and make it work.

The phidgets turned out to be so easy to work with, in retrospect, I don't know why I ever thought building the control system would be a challenge. Since it's so easy, and I'm only ordering new components once a month (2 year planned build), I'm now just playing with the code to see what extra capabilities I can pull out of it. I'm working with the object detection/gesture detection sample now. http://code.google.com/p/aforge/

LinuxGuy
06-10-2008, 04:40 PM
I didn't realize that your project was so in depth till I read the description! +rep:)

You did all of that with only prior high school programming experience?!? WOW!! Where on Earth did you go to high school?
I've taught myself 3D CAD, several programming languages, how to build robots (with forum help, of course, when I was starting out and continuing), etc. I have no degrees and only a high school diploma, and a 1 year diploma in electronics technology. I did have the advantage of having access to computers from Junior High onwared, and electronics from high school on, but that is pretty much it. I have had a few programming courses (Pascal, APL) and learned C on the job and on my own. Just starting to dig deeper into object oriented programming, which I like a lot.

All you really have to do is apply yourself to what you want to learn and do. :veryhappy::veryhappy: I learn what I need as I need to know it to do something new. Anyone can do it if they really want to. :happy:

8-Dale

Alex
06-11-2008, 08:53 AM
Yeah, I wish I had more time to learn about all the stuff I want to. I'm surrounded by robots here at TR, but hardly have the time to mess around with any of them... It's torture!!:mad:

Little bit by little bit though. I still can't wait to take a BoeBot home!!

LinuxGuy
06-11-2008, 08:59 AM
Little bit by little bit though. I still can't wait to take a BoeBot home!!
If I were you, I'd get a Trekker Chassis (http://www.trossenrobotics.com/store/p/3169-Trekker-Chassis.aspx) or Stinger Robot Kit w/ Serializer (http://www.trossenrobotics.com/stinger-robot-kit.aspx) . I really want to get a Stinger Robot Kit w/ Serializer (http://www.trossenrobotics.com/stinger-robot-kit.aspx) eventually, even though I already have a two wheeled robot. I looked at the BoeBot when I was starting out, and decided it just didn't have enough space for me to add stuff. I got a kit from another company called the Octabot II and added my choice of electronics, which is what W.A.L.T.E.R. started out as.

8-Dale

Alex
06-11-2008, 10:46 AM
It's not about space for me, it's about learning about electronics. I don't have any projects at all in mind, nor do I intend to have any. I simply just want to learn more about the lower level electronics side of things. That's why I want to try out the BoeBot.

I already know about the Serializer practically in and out. I still have a little bit to learn about it, but I have enough knowledge about it to move onto something else. Besides, the Serializer deals with .NET, which is a language that I already know way too well. Don't get me wrong, the Serializer is in my top 5 favorite boards (http://forums.trossenrobotics.com/showthread.php?t=1743&page=10&highlight=top). I just want to learn more about the lower level electronics and programming.

BTW, I'm not exactly "starting out" :p

LinuxGuy
06-11-2008, 11:06 AM
Besides, the Serializer deals with .NET, which is a language that I already know way too well. Don't get me wrong, the Serializer is in my top 5 favorite boards (http://forums.trossenrobotics.com/showthread.php?t=1743&page=10&highlight=top). I just want to learn more about the lower level electronics and programming.
The Serializer does not have to be used with .NET though - it accepts ASCII commands over whatever interface is plugged into it OR TTL serial. You know all that already though, but others might not. :veryhappy::veryhappy: Make it work from Linux then.. :veryhappy::veryhappy: No .NET/C# to fall back on there. :veryhappy::veryhappy:


BTW, I'm not exactly "starting out" :p
I know. Stretch your mind and what you know. :veryhappy:

8-Dale

darkback2
06-11-2008, 11:41 AM
Its funny how hard that is. You can't think of things you haven't thought of because you didn't think of them. Its a wonder anyone ever figures anything out at all.

DB

Alex
06-11-2008, 12:00 PM
Make it work from Linux then..

yeah... right!!:D

Seriously though, I'd love to learn Linux one day, but that is waay down the line. My best friend is always hounding me about learning Linux too, haha!

I want to learn more about the stuff I mentioned earlier before I start learning an entirely new OS though. The next OS on my list to learn is Mac OSX and learning MAX/MSP. DB totally turned me on to that!! I've been wanting a Mac for a loong time now anyway.

CRAP! I did it again... sorry for hijacking the thread. If anyone wants to discuss what I was talking about further, just PM me.

Back to topic please:D

indy007
06-12-2008, 10:26 AM
Hey Alex, I'm thinking about a possible issue here with the code and a workaround.

Lets pretend we're working on the WaterHobo for a minute. We want the firing action on the trigger button of the joystick.

Right now, when the timer() cycles, it'll read the button down and execute some kind of shooting() function. Now, if the button is down for 1 full second, it would try to execute that command 10 times. That's bad when you're operating something that has to have a timing cycle (like a multi-valve pneumatic device). Instead of firing once, it would fire 10 times.

The way to fix this, and please bear with me, I haven't had a chance to test it... would be to create a boolean "FiringInUse" variable. Once the button is depressed, switch FiringInUse to "yes" and an if statement to check it. If FiringInUse == yes, skip the firing() function. If FiringInUse = no, then use the function. Simply add FiringInUse = no to the end of your Firing() function, and it should be good to go.

I'll try to get it in coded later, but it's been something nagging at me since I first started playing with it. I'm sure there's DirectInput event commands that would substitute just fine, but I haven't read into those yet.

Alex
06-12-2008, 11:19 PM
The way to fix this, and please bear with me, I haven't had a chance to test it... would be to create a boolean "FiringInUse" variable. Once the button is depressed, switch FiringInUse to "yes" and an if statement to check it. If FiringInUse == yes, skip the firing() function. If FiringInUse = no, then use the function. Simply add FiringInUse = no to the end of your Firing() function, and it should be good to go.

Yep, that's exactly how I did it when I built my pan and tilt firing board application. I'm out at RoboGames now, but I can shoot it your way when I get back. It's pretty easy though and I think you pretty much nailed it, so you'll probably be done with it b4 I get back.

I believe there is something in DirectInput that will allow you to control this as well, but I can't remember off hand what that was. I never was able to get it to work though, so I just used the method above.

indy007
06-13-2008, 07:19 PM
Yep, that's exactly how I did it when I built my pan and tilt firing board application. I'm out at RoboGames now, but I can shoot it your way when I get back. It's pretty easy though and I think you pretty much nailed it, so you'll probably be done with it b4 I get back.

I believe there is something in DirectInput that will allow you to control this as well, but I can't remember off hand what that was. I never was able to get it to work though, so I just used the method above.

TY! I was just curious. I haven't gotten to the trigger function yet. I have to write in something like 9 different fire modes to meet the various scenario paintball rule-sets. I won't get started on it until I'm happy with the auto-targeting first though.

On that note, anybody happen to know of either an ip camera or usb camera that can get exceptionally high framerates? I don't think it's possible without a heavy expenditure and probably extra equipment, but I'd like to track a very bright 2" object moving around 250fps. If anybody happened to know of one that'd fit the bill and is reasonably priced, I'd love to hear about it.

Adrenalynn
06-14-2008, 02:25 AM
Highspeed cameras are notoriously finiky and very expensive.

Is there really a good reason for needing those framerates? If you want to track people, skipping the camera and using an IR diode specifically tuned to human body heat is the route I would (and am on a project) go

indy007
06-14-2008, 10:07 AM
Highspeed cameras are notoriously finiky and very expensive.

Is there really a good reason for needing those framerates? If you want to track people, skipping the camera and using an IR diode specifically tuned to human body heat is the route I would (and am on a project) go

In the paintball events I'm building gadgets for, to take out buildings, bridges, tanks, etc, they have basically 2" co2 powered air cannons. It's a 12 gram co2 cartridge behind a quarter turn ball valve. You muzzleload a nerf vortex football (2" diamater, bright red, blue tail). They leave the barrel at around 250fps. It's a softcore potato gun.

I want to visually track and engage an incoming nerf football with my pan-tilt turret like a miniature Phalanx system. The catch would be that it'd probably need stereoscopic vision with a ferociously high frame rate and fancy math to plot the firing solution. LIDAR would work.. the Israelis adapted it for RPG/AT Missile defense (and the closure rights are massively higher), but I've never found one that's USB & affordable :(

Adrenalynn
06-14-2008, 10:14 AM
Building something similar to LIDAR from a simple perspective in a "hobby mode" isn't all the tough. Did you see my work in "OMG LASERS! Pew Pew!" or some-such thread?

I don't know that increasing the framerate of the video is going to buy you that much. You can't fire your paintball gun 250 times a second without chopping a lot of paint [;)]. A Phalanx "walks" in on a target by tracking the target and its own stream of rounds - using a RADAR system.

The math involved isn't really that tough. If you look at the laser ranging stuff I was working on, you'll see a good start at it. I could probably hit a football with just two frames from the video, definitely with three. Think about basic geometry and trig. Once you have a couple points on an arc you can start predicting that arc pretty close. If I waited to fire until after it reached the peak of the curve, gravity dictates the rest is falling-off-a-log simple/reproducible. :)