Exemple #1
0
        private void ConnectDevice(string motorNO)
        {
            // unload any currently connected device if not of the desired type
            if (KDC101 != null)
            {
                if (KDC101.DeviceID == motorNO)
                {
                    return;
                }
                DisconnectDevice();
            }

            // create the new device
            KDC101 = KCubeDCServo.CreateKCubeDCServo(motorNO);

            try
            {
                KDC101.Connect(motorNO);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return;
            }

            // wait for settings to be initialized (on the channel)
            KDC101.WaitForSettingsInitialized(5000);

            // create user interface (WPF view) via the DeviceManager
            _contentControl.Content = KCubeDCServoUI.CreateLargeView(KDC101, DeviceConfiguration.DeviceSettingsUseOptionType.UseConfiguredSettings);
        }
Exemple #2
0
 private void CreateDevice()
 {
     // create the device
     this.currentMotor = KCubeDCServo.CreateKCubeDCServo(this.serialNo);
     if (this.currentMotor == null)
     {
         // an error occured
         Console.WriteLine("{0} is not a KCubeDCServo", this.serialNo);
         Console.ReadKey();
         return;
     }
 }
Exemple #3
0
        private void initializeFiber(string serial)
        {
            try
            {
                // Instructs the DeviceManager to build and maintain the list of devices connected.
                DeviceManagerCLI.BuildDeviceList();
            }
            catch (Exception)
            {
                this.fiberLabel.Text = "Not connected";
                return;
            }

            try
            {
                _kCubeDCServoMotor = KCubeDCServo.CreateKCubeDCServo(serial);
                // Establish a connection with the device.
                _kCubeDCServoMotor.Connect(serial);
            }
            catch (Exception)
            {
                this.fiberLabel.Text = "Unable to connect";
                return;
            }

            try
            {
                // Wait for the device settings to initialize. We ask the device to
                // throw an exception if this takes more than 5000ms (5s) to complete.
                _kCubeDCServoMotor.WaitForSettingsInitialized(5000);
                // Initialize the DeviceUnitConverter object required for real world unit parameters.
                _kCubeDCServoMotor.LoadMotorConfiguration(_kCubeDCServoMotor.DeviceID, DeviceConfiguration.DeviceSettingsUseOptionType.UseFileSettings);
                // This starts polling the device at intervals of 250ms (0.25s).
                _kCubeDCServoMotor.StartPolling(250);
                // We are now able to enable the device for commands.
                _kCubeDCServoMotor.EnableDevice();
                // Enable buttons and contros
                ToggleFiberButtons(true);
                this.fiberLabel.Text = "Connected";
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to initialize fiber controller\n" + ex);
                return;
            }
        }
        private void buttonConnect_Click(object sender, EventArgs e)
        {
            if (_kCubeDCServoMotor != null)
            {
                MessageBox.Show("Device already connected");
                return;
            }

            const string serialNumber = "27250312";

            // All of this operation has been placed inside a single "catch-all"
            // exception handler. This is to reduce the size of the example code.
            // Normally you would have a try...catch per API call and catch the
            // specific exceptions that could be thrown (details of which can be
            // found in the Kinesis .NET API document).
            try
            {
                // Instructs the DeviceManager to build and maintain the list of
                // devices connected.
                DeviceManagerCLI.BuildDeviceList();

                _kCubeDCServoMotor = KCubeDCServo.CreateKCubeDCServo(serialNumber);

                // Establish a connection with the device.
                _kCubeDCServoMotor.Connect(serialNumber);

                // Wait for the device settings to initialize. We ask the device to
                // throw an exception if this takes more than 5000ms (5s) to complete.
                _kCubeDCServoMotor.WaitForSettingsInitialized(5000);

                // Initialize the DeviceUnitConverter object required for real world
                // unit parameters.
                _kCubeDCServoMotor.LoadMotorConfiguration(_kCubeDCServoMotor.DeviceID, DeviceConfiguration.DeviceSettingsUseOptionType.UseFileSettings);

                // This starts polling the device at intervals of 250ms (0.25s).
                _kCubeDCServoMotor.StartPolling(250);

                // We are now able to enable the device for commands.
                _kCubeDCServoMotor.EnableDevice();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to connect to device\n" + ex);
            }
        }
        private void buttonDisconnect_Click(object sender, EventArgs e)
        {
            if (_kCubeDCServoMotor == null)
            {
                MessageBox.Show("Not connected to device");
                return;
            }

            // All of this operation has been placed inside a "catch-all" exception
            // handler. Normally you would catch the more specific exceptions that
            // the API call might throw (details of which can be found in the
            // Kinesis .NET API document).
            try
            {
                // Shuts down this device and frees any resources it is using.
                _kCubeDCServoMotor.ShutDown();

                _kCubeDCServoMotor = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to disconnect from device\n" + ex);
            }
        }
        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            // This instructs the DeviceManager to build and maintain the list of
            // devices connected. We then print a list of device name strings called
            // “devices” which contain the prefix “27”
            DeviceManagerCLI.BuildDeviceList();
            List <string> devices = DeviceManagerCLI.GetDeviceList(27);

            // IF statement – if the number of devices connected is zero, the Window
            // will display “No Devices”.
            if (devices.Count == 0)
            {
                MessageBox.Show("No Devices");
                return;
            }
            // Selects the first device serial number from “devices” list.
            string serialNo = devices[0];
            // Creates the device. We assign an instance of the device to _kCubeDCServo
            KCubeDCServo _kCubeDCServo = KCubeDCServo.CreateKCubeDCServo(serialNo);

            // Connect to the device & wait for initialisation. This is contained in a
            // Try/Catch Error Handling Statement.
            try
            {
                _kCubeDCServo.Connect(serialNo);
                // wait for settings to be initialized
                _kCubeDCServo.WaitForSettingsInitialized(5000);
            }
            catch (DeviceException ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            // Create the Kinesis Panel View for KDC101
            _contentControl.Content = KCubeDCServoUI.CreateLargeView(_kCubeDCServo);
        }
        static void Main(string[] args)
        {
            TCubeDCServo.RegisterDevice();
            KCubeDCServo.RegisterDevice();

            // get parameters from command line
            int argc = args.Count();

            if (argc < 1)
            {
                Console.WriteLine("Usage = DC_Console_net_managed [serial_no] [position: optional (0 - 50)] [velocity: optional (0 - 5)]");
                Console.ReadKey();
                return;
            }

            decimal position = 0m;

            if (argc > 1)
            {
                position = decimal.Parse(args[1]);
            }

            decimal velocity = 0m;

            if (argc > 2)
            {
                velocity = decimal.Parse(args[2]);
            }

            string serialNo = args[0];

            try
            {
                // build device list
                DeviceManagerCLI.BuildDeviceList();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception raised by BuildDeviceList {0}", ex);
                Console.ReadKey();
                return;
            }

            // get available KCube DC Servos and check our serial number is correct
            List <string> serialNumbers = DeviceManagerCLI.GetDeviceList(new List <int> {
                KCubeDCServo.DevicePrefix, TCubeDCServo.DevicePrefix
            });

            if (!serialNumbers.Contains(serialNo))
            {
                if (serialNumbers.Count > 0)
                {
                    serialNo = serialNumbers[0];
                    Console.WriteLine("using serial number {0}", serialNo);
                }
                else
                {
                    Console.WriteLine("{0} is not a valid serial number", serialNo);
                    Console.ReadKey();
                    return;
                }
            }

            // create the device
            IGenericCoreDeviceCLI device = DeviceFactory.CreateDevice(serialNo);
            IGenericAdvancedMotor motor  = device as IGenericAdvancedMotor;

            if (motor == null)
            {
                Console.WriteLine("{0} is not a DCServo", serialNo);
                Console.ReadKey();
                return;
            }

            // connect device
            try
            {
                Console.WriteLine("Opening device {0}", serialNo);
                device.Connect(serialNo);

                if (!motor.IsSettingsInitialized())
                {
                    motor.WaitForSettingsInitialized(5000);
                }

                // display info about device
                DeviceInfo di = device.GetDeviceInfo();
                Console.WriteLine("Device {0} = {1}", di.SerialNumber, di.Name);

                // start the device polling
                motor.StartPolling(250);
            }
            catch (DeviceException ex)
            {
                Console.WriteLine("Failed to open device {0} - {1}", ex.DeviceID, ex.Message);
                Console.ReadKey();
                return;
            }

            DeviceUnitConverter deviceUnitConverter;

            try
            {
                // call GetMotorConfiguration on the device to initialize the DeviceUnitConverter object required for real unit parameters
                MotorConfiguration motorSettings = motor.GetMotorConfiguration(serialNo);
                motorSettings.DeviceSettingsName = "PRM1-Z8";
                motorSettings.UpdateCurrentConfiguration();

                MotorDeviceSettings motorDeviceSettings = motor.MotorDeviceSettings;
                motor.SetSettings(motorDeviceSettings, true, false);

                // test code to test get / sert of parameters using real world units
                TestVelocityParameters(motor);
                TestJogParameters(motor);
                TestHomingParameters(motor);
                TestLimitParameters(motor);
                if (device is TCubeDCServo)
                {
                    TestPotentiometerParameters(device as TCubeDCServo);                     // TDC Only
                }

                motorSettings.UpdateCurrentConfiguration();
                deviceUnitConverter = motor.UnitConverter;
            }
            catch (DeviceException ex)
            {
                Console.WriteLine("Failed prepare settings {0} - {1}", ex.DeviceID, ex.Message);
                Console.ReadKey();
                return;
            }

            try
            {
                if (!Home_1(motor))
                {
                    Console.WriteLine("Failed to home device");
                    Console.ReadKey();
                    return;
                }
            }
            catch (DeviceException ex)
            {
                Console.WriteLine("Failed to Home device settings {0} - {1}", ex.DeviceID, ex.Message);
                Console.ReadKey();
                return;
            }

            try
            {
                // if position is set
                if (position != 0)
                {
                    // update velocity if required using real world methods
                    if (velocity != 0)
                    {
                        VelocityParameters velPars = motor.GetVelocityParams();
                        velPars.MaxVelocity = velocity;
                        motor.SetVelocityParams(velPars);
                    }

                    if (!MoveTo_1(motor, position, deviceUnitConverter))
                    {
                        Console.WriteLine("Failed to set position");
                        Console.ReadKey();
                    }
                }
                else
                {
                    char c = '\0';
                    do
                    {
                        do
                        {
                            Console.WriteLine("Press a key");
                            Console.WriteLine("0 to exit");
                            Console.WriteLine("1 to test StopImmediate()");
                            Console.WriteLine("2 to test Stop(5000)");
                            Console.WriteLine("3 to test Stop(WaitEvent)");
                            c = Console.ReadKey().KeyChar;
                        } while (c < '0' || c > '3');

                        if (c != '0')
                        {
                            motor.MoveContinuous(MotorDirection.Forward);
                            Console.WriteLine("Press any key to stop");
                            Console.ReadKey();
                            StatusBase status;
                            if (c == '1')
                            {
                                motor.Stop(5000);
                            }
                            if (c == '2')
                            {
                                motor.StopImmediate();
                            }
                            if (c == '3')
                            {
                                ManualResetEvent waitEvent = new ManualResetEvent(false);
                                waitEvent.Reset();
                                motor.Stop(p =>
                                {
                                    Console.WriteLine("Message Id {0}", p);
                                    waitEvent.Set();
                                });
                                if (!waitEvent.WaitOne(5000))
                                {
                                    Console.WriteLine("Failed to Stop");
                                }
                            }
                            do
                            {
                                status = motor.Status;
                                Console.WriteLine("Status says {0} ({1:X})", status.IsInMotion ? "Moving" : "Stopped", status.Status);
                                Thread.Sleep(50);
                            } while (status.IsInMotion);
                        }
                    } while (c != '0');
                }
            }
            catch (DeviceException ex)
            {
                Console.WriteLine("Failed to Move device settings {0} - {1}", ex.DeviceID, ex.Message);
                Console.ReadKey();
                return;
            }

            try
            {
                device.Disconnect(true);
            }
            catch (DeviceException ex)
            {
                Console.WriteLine("Failed to Disconnect {0} - {1}", ex.DeviceID, ex.Message);
            }

            Console.ReadKey();
        }
Exemple #8
0
        public void init()
        {
            LinLi = KCubeDCServo.CreateKCubeDCServo(KDC101_left);
            if (LinLi == null)
            {
                MessageBox.Show("Device A is not a KDC101");
            }

            LinRe = KCubeDCServo.CreateKCubeDCServo(KDC101_right);
            if (LinRe == null)
            {
                MessageBox.Show("Device B is not a KDC101");
            }

            RotLi = KCubeBrushlessMotor.CreateKCubeBrushlessMotor(KBD101_left);
            if (RotLi == null)
            {
                MessageBox.Show("Device D is not a KBD101");
            }

            RotRe = KCubeBrushlessMotor.CreateKCubeBrushlessMotor(KBD101_right);
            if (RotRe == null)
            {
                MessageBox.Show("Device D is not a KBD101");
            }

            // Open a connection to the device.
            try
            {
                LinLi.Connect(KDC101_left);
            }
            catch (Exception)
            {
                // Connection failed
                MessageBox.Show("Failed to open device A");
            }

            try
            {
                LinRe.Connect(KDC101_right);
            }
            catch (Exception)
            {
                // Connection failed
                MessageBox.Show("Failed to open device B");
            }

            try
            {
                RotLi.Connect(KBD101_left);
            }
            catch (Exception)
            {
                // Connection failed
                MessageBox.Show("Failed to open device C");
            }

            try
            {
                RotRe.Connect(KBD101_right);
            }
            catch (Exception)
            {
                // Connection failed
                MessageBox.Show("Failed to open device D");
            }

            // Wait for the device settings to initialize - timeout 5000ms
            LinLi.WaitForSettingsInitialized(500);
            LinRe.WaitForSettingsInitialized(500);
            RotLi.WaitForSettingsInitialized(500);
            RotRe.WaitForSettingsInitialized(500);

            // Initialize the DeviceUnitConverter object required for real world
            // unit parameters.
            LinLi.LoadMotorConfiguration(KDC101_left, DeviceConfiguration.DeviceSettingsUseOptionType.UseFileSettings);
            LinRe.LoadMotorConfiguration(KDC101_right, DeviceConfiguration.DeviceSettingsUseOptionType.UseFileSettings);
            RotLi.LoadMotorConfiguration(KBD101_left, DeviceConfiguration.DeviceSettingsUseOptionType.UseFileSettings);
            RotRe.LoadMotorConfiguration(KBD101_right, DeviceConfiguration.DeviceSettingsUseOptionType.UseFileSettings);

            // Start the device polling
            // The polling loop requests regular status requests to the motor to ensure the program keeps track of the device.
            LinLi.StartPolling(50);
            LinRe.StartPolling(50);
            RotLi.StartPolling(50);
            RotRe.StartPolling(50);

            // Needs a delay so that the current enabled state can be obtained
            Thread.Sleep(50);

            // Enable the channel otherwise any move is ignored
            LinLi.EnableDevice();
            LinRe.EnableDevice();
            RotLi.EnableDevice();
            RotRe.EnableDevice();

            // Needs a delay to give time for the device to be enabled
            Thread.Sleep(50);
        }