Automatically sending servos to a default position

I’m using a micro maestro to control three servos via a computer. I’ve used the easy example as a base for the code. When none of the GUI buttons are pressed, I want the servos to automatically return to a default position, and I can’t for the life of me figure out how to do this.

Hi.

I am not sure what you are having trouble understanding. Could you please post the code you have so far and show me what trouble you are having with the code?

-Derrill

using Pololu.Usc;
using Pololu.UsbWrapper;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;

namespace Pololu.Usc.MaestroEasyExample
{
    public partial class MainWindow : Form
    {
        public MainWindow()
        {
        	InitializeComponent();
		TrySetTarget (0, 1500 * 4);			//Set position of servo on powerup
		TrySetTarget (1, 1500 * 4);
                TrySetTarget (2, 1500 * 4);
        }
		
        void Button1000_Click(object sender, EventArgs e)	//Set position of servo when button clicked
        {
            	TrySetTarget (0, 1000 * 4);  
		TrySetTarget (1, 1000 * 4);
                TrySetTarget (2, 1000 * 4);
        }

        void TrySetTarget(Byte channel, UInt16 target)
        {
            try
            {
                using (Usc device = connectToDevice())  
                {
                    device.setTarget(channel, target);
                }
            }
            catch (Exception exception)  
            {
                displayException(exception);
            }
        }

        Usc connectToDevice()
        {
            List<DeviceListItem> connectedDevices = Usc.getConnectedDevices();

            foreach (DeviceListItem dli in connectedDevices)
            {
                Usc device = new Usc(dli); 
                return device;             
            }
            throw new Exception("Could not find device.  Make sure it is plugged in to USB " +
                "and check your Device Manager (Windows) or run lsusb (Linux).");
        }

        void displayException(Exception exception)
        {
            StringBuilder stringBuilder = new StringBuilder();
            do
            {
                stringBuilder.Append(exception.Message + "  ");
                if (exception is Win32Exception)
                {
                    stringBuilder.Append("Error code 0x" + ((Win32Exception)exception).NativeErrorCode.ToString("x") + ".  ");
                }
                exception = exception.InnerException;
            }
            while (exception != null);
            MessageBox.Show(stringBuilder.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

When the device is powered on, each servo moves to position 1500, and when the button is clicked, each servo moves to position 1000. When I release the button, I want each servo to return to position 1500, however they remain at 1000. What commands do I need to achieve this?

At this point, it looks like you know how to set the servo targets, which means that what you are trying to do has essentially become an issue of generating the appropriate C# events with the appropriate GUI controls. There are many ways to do what you are trying to accomplish. One would be to use a different control with one event for a mouse press and another for a mouse release. Another would be to use a checkbox control. Yet another would be one that does one action on the first button click and a different one on the second (you could save the state with a global variable so you know which action to perform for each click). I suggest you look for a good tutorial on C#.

-Derrill

Yep, found out about MouseDown and MouseUp.

In MainWindow.cs I changed

void Button1000_Click(object sender, EventArgs e)   
        {
		positions
        }

to

void Button1000_MouseDown(object sender, MouseEventArgs e)
        {
		new positions
        }

void Button1000_MouseUp(object sender, MouseEventArgs e)
        {
		default positions
        }

And in MainWindow.Designer.cs I changed

this.Button1000.Click += new System.EventHandler(this.Button1000_Click);

to

this.Button1000.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Button1000_MouseDown);
this.Button1000.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Button1000_MouseUp);

I’ll leave this here for others, as I had a devil of a job getting the syntax right.