Main Site Documentation

Where is the CanXtra Robot Arm demo code?


#1

Last night I stumbled across a youtube video of Gus demoing a robot arm controlled by a joystick and a CanXtra, but now I can’t find the link! I also can’t find any mention of the project in the Wiki or elsewhere. Anybody know where it might be hiding? archived maybe?


#2

This is from long years a go! It uses a Serial servo controller. Here is the code but note it is too old and outdated probably.

/*
 * Robot Arm reference: http://www.lynxmotion.com/images/howto/ssc-168.pdf
 */

// This is if LCD is connected (used in CANxtra example)
// For USBizi, you should remove this define and also remove the graphics assemblies (Microsoft.SPOT.Graphics, Microsoft.SPOT.TinyCore)
//#define USE_DISPLAY   


using System;
using System.Collections;
using System.Threading;
using System.IO.Ports;
using Microsoft.SPOT;
//using Microsoft.SPOT.Input;
using Microsoft.SPOT.Hardware;
//using Microsoft.SPOT.Presentation;
//using Microsoft.SPOT.Presentation.Media;
///using Microsoft.SPOT.Presentation.Controls;
//using Microsoft.SPOT.Presentation.Shapes;

using System.Text;
using System.IO;
//using GHIElectronics.System;
//using GHIElectronics.System.SystemDevices;
//using GHIElectronics.System.IO;
//using GHIElectronics.Hardware;

using GHIElectronics.NETMF.USBHost;


namespace LynxmotionRobotArm
{
    public class Program
    {
        public static USBH_Joystick joy;
 #if USE_DISPLAY
        static Bitmap screen = new Bitmap(SystemMetrics.ScreenWidth, SystemMetrics.ScreenHeight);
        static Font smallFont = Resources.GetFont(Resources.FontResources.small);
 #else
        static class screen
        {
            public static void Clear() { }
            public static void Flush() { }
        }
 #endif
        static SerialPort port;

        class Servo
        {
            public int id;
            public int position = 1500;
            public int state = 0;      // 0: none
            // 1: increasing
            // -1: decreasing

            public Servo(int id)
            {
                this.id = id;
            }

            public void Increase() { state = 1; }
            public void Decrease() { state = -1; }
            public void Stop() { state = 0; }

            public void Process()
            {
                switch (state)
                {
                    case 1: // increasing
                        if (position < 1740)
                            position += 10;

                        //SetArmPosition(id, position);
                        break;

                    case -1: // decreasing
                        if (position > 1115)
                            position -= 10;

                        //SetArmPosition(id, position);
                        break;
                }

                SetArmPosition(id, position);
            }
        }

        const int X_SERVO = 0;
        const int Y_SERVO = 1;
        const int ARM_UP_DOWN_SERVO = 2;
        const int MOUTH_UP_DOWN_SERVO = 3;
        const int MOUTH_CATCH_SERVO = 4;
        const int MOUTH_ROTATE_SERVO = 5;


        static Servo[] servos = new Servo[6]{
            new Servo(X_SERVO),
            new Servo(Y_SERVO),
            new Servo(ARM_UP_DOWN_SERVO),
            new Servo(MOUTH_UP_DOWN_SERVO),
            new Servo(MOUTH_CATCH_SERVO),
            new Servo(MOUTH_ROTATE_SERVO),
                    };

        public static void Main()
        {
            try
            {
                screen.Clear();
                print("Starting...", 0, 0);
                screen.Flush();

                port = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One);
                port.ReadTimeout = 5000;
                port.WriteTimeout = 5000;
                port.Open();

                SetArmPosition(0, 1500);
                SetArmPosition(1, 1500);
                SetArmPosition(2, 1500);
                SetArmPosition(3, 1500);
                SetArmPosition(4, 1500);
                SetArmPosition(5, 1500);

                USBHostController.DeviceConnectedEvent += new USBH_DeviceConnectionEventHandler(USBHostController_DeviceConnectedEvent);

                while (true)
                {
                    foreach (Servo s in servos)
                        s.Process();

                    /*if (joy != null)
                    {
                        screen.Clear();
                        print("(x,y) = (" + joy.Cursor.X.ToString() + "," + joy.Cursor.Y.ToString() + ")", 0, 0);
                        print("Elbow Pos: " + servos[ARM_UP_DOWN_SERVO].position.ToString(), 0, 10);
                        print("Wrist Pos: " + servos[MOUTH_UP_DOWN_SERVO].position.ToString(), 0, 20);
                        print("Wrist Rot: " + servos[MOUTH_ROTATE_SERVO].position.ToString(), 0, 30);
                        print("Gripper: " + servos[MOUTH_CATCH_SERVO].position.ToString(), 0, 40);
                        screen.Flush();
                    }

                    */
                    Thread.Sleep(10);
                }
            }
            catch (Exception e)
            {
                screen.Clear();
                print("EXP: " + e.Message, 0, 0);
                screen.Flush();
            }
        }

        static void USBHostController_DeviceConnectedEvent(USBH_Device device)
        {
            screen.Clear();
            print("Joystick connected", 0, 0);
            screen.Flush();

            joy = new USBH_Joystick(device);
            //joy.Disconnected += Disconnected;
            joy.SetCursorBounds(1000, 2000, 1000, 2000);
            joy.JoystickXYMove += Moved;
            joy.JoystickButtonDown += ButtonDown;
            joy.JoystickButtonUp += ButtonUp;
        }

        /*static void Disconnected(Joystick joy, JoystickEventArgs args)
        {
            screen.Clear();
            print("Joystick Disconnected", 0, 0);
            screen.Flush();
        }*/

        static void Moved(USBH_Joystick joy, USBH_JoystickEventArgs args)
        {
            //SetArmPosition(0, joy.Cursor.X);
            //SetArmPosition(1, joy.Cursor.Y);

            if (joy.Cursor.X > 1600)
                servos[X_SERVO].Increase();
            else if (joy.Cursor.X < 1400)
                servos[X_SERVO].Decrease();
            else
                servos[X_SERVO].Stop();


            if (joy.Cursor.Y > 1600)
                servos[Y_SERVO].Increase();
            else if (joy.Cursor.Y < 1400)
                servos[Y_SERVO].Decrease();
            else
                servos[Y_SERVO].Stop();
        }

        static void SetArmPosition(int servo, int pos)
        {
            byte[] cmd = Encoding.UTF8.GetBytes("#" + servo.ToString() + "P" + pos.ToString() + "T150\r");
            port.Write(cmd, 0, cmd.Length);
        }

        static void ButtonDown(USBH_Joystick joy, USBH_JoystickEventArgs args)
        {
            switch (args.ChangedButton)
            {
                case 5:     // arm down
                    servos[ARM_UP_DOWN_SERVO].Increase();
                    break;

                case 6:     // arm up
                    servos[ARM_UP_DOWN_SERVO].Decrease();
                    break;

                case 3:     // mouthRotate CCW
                    servos[MOUTH_ROTATE_SERVO].Increase();
                    break;

                case 4:     // mouthRotate CW
                    servos[MOUTH_ROTATE_SERVO].Decrease();
                    break;

                case 2:     // mouth up
                    servos[MOUTH_UP_DOWN_SERVO].Increase();
                    break;

                case 1:     // mouth down
                    servos[MOUTH_UP_DOWN_SERVO].Decrease();
                    break;

                case 0:
                    servos[MOUTH_CATCH_SERVO].Decrease();
                    break;

            }
        }

        static void ButtonUp(USBH_Joystick joy, USBH_JoystickEventArgs args)
        {
            switch (args.ChangedButton)
            {
                case 5:     // arm down
                    servos[ARM_UP_DOWN_SERVO].Stop();
                    break;

                case 6:     // arm up
                    servos[ARM_UP_DOWN_SERVO].Stop();
                    break;

                case 3:     // mouthRotate CCW
                    servos[MOUTH_ROTATE_SERVO].Stop();
                    break;

                case 4:     // mouthRotate CW
                    servos[MOUTH_ROTATE_SERVO].Stop();
                    break;

                case 2:     // mouth up
                    servos[MOUTH_UP_DOWN_SERVO].Stop();
                    break;

                case 1:     // mouth down
                    servos[MOUTH_UP_DOWN_SERVO].Stop();
                    break;

                case 0:
                    servos[MOUTH_CATCH_SERVO].Increase();
                    break;
            }
        }

        // LCD Print
        public static void print(string str, int x, int y)
        {
 #if USE_DISPLAY
            screen.DrawText(str, smallFont, Color.White, x, y);
 #else
            Debug.Print(str);
 #endif
        }
    }
}


#3

too old? Never!

Thx


#4

Ransom…

Any progress on Spider Web?


#5

No. I asked for 2 quotes and neither has responded. I’ve even reminded both… not sure why I’m getting the silent treatment.


#6

That servo example works fine.
I used it for my hexapod (servo code).