Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

[SOLVED] Mini Maestro - Using C# and setTarget()


#1

I am attempting to set up a small demo program in C# to move a servo 0 on a 24-channel Mini Maestro servo controller. However, when I try to compile in Visual Studio 2013, I receive the following errors:

[quote]Error 3 The type or namespace name ‘Sequence’ could not be found (are you missing a using directive or an assembly reference?)
Error 1 The type or namespace name ‘Sequencer’ does not exist in the namespace ‘Pololu.Usc’ (are you missing an assembly reference?)
Error 2 The type or namespace name ‘Sequencer’ does not exist in the namespace ‘Pololu.Usc’ (are you missing an assembly reference?)[/quote]

I assume that I must add additional C# Pololu class files to the project, but which ones? I can compile the C# demo programs in the USB SDK, but am unclear on how to set up a new project from scratch. I have been referring to the following forum thread: https://forum.pololu.com/t/micro-maestro-info-for-sentry-gun-makers/1673/1

I am new to C#, so perhaps I have overlooked something obvious. My code is below:

[code]//A simple test program to set servo position on the Pololu Mini Maestro using C#
//
//This program was created from a blank VisualStudio console program template.
//
//See: Micro Maestro info for sentry-gun makers
//
//Note: Must add the following files to the Solution Explorer:
// Usc.cs, Usc_protocol.cs, UscSettings.cs, IUscSettingsHolder.cs, ConfigurationFile.cs
//
//Note: To remove error message “Unsafe code may only appear if compiling with /unsafe,” go to:
// Solution Explorer> Properties > Build > [tick box]: Allow unsafe code
// See: http://stackoverflow.com/questions/2026410/why-do-i-get-the-error-unsafe-code-may-only-appear-if-compiling-with-unsafe

//default using statements from VisualStudio template
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//added using statements
using System.Reflection; //Used by the Pololu UscCmd demo program. Not sure if this is required here.
using Pololu.UsbWrapper; //The Bytecode and UsbWrapper DLLs from the Pololu SDK folder must be manually added to the references section of the Solution Explorer, see: https://msdn.microsoft.com/en-us/library/6taasyc6(v=vs.80).aspx
using Pololu.Usc.Bytecode;

namespace MaestroServoTest
{
class Program
{
static void Main(string[] args)
{

        int servoNumber = 0;
        int servoTarget = (1500 * 4); //specify in units of quarter-milliseconds

        usc.setTarget(servoNumber, servoTarget);

    }
}

}
[/code]


#2

Hello.

It looks like you already figured out how to add a reference to Bytecode. You just need to do the same thing for Sequencer. We provide it both as a C# project and a DLL in the Maestro folder of the Pololu USB SDK.

–David


#3

Thanks David!

Using that bit of info and adapting some code from the UscCmd program, I was finally able to get my program to run.

I have included my complete code listing below in the hopes that other beginners will find it useful. Cheers!

EDIT - units of setTarget() are quarter microsecond (10^-6 s), not quarter millisecond (10^-3 s) as first posted.

[code]//A simple test program to set servo position on the Pololu Mini Maestro using C#
//
//This program was created from a blank Visual Studio Express 2013 console program template.
//
//See: Micro Maestro info for sentry-gun makers
//
//NOTE: Must add the following files from the Pololu USB SDK to the Solution Explorer:
// Usc.cs, Usc_protocol.cs, UscSettings.cs, IUscSettingsHolder.cs, ConfigurationFile.cs
//
//NOTE: To remove error message “Unsafe code may only appear if compiling with /unsafe,” go to:
// Solution Explorer> Properties > Build > [tick box]: Allow unsafe code
// See: http://stackoverflow.com/questions/2026410/why-do-i-get-the-error-unsafe-code-may-only-appear-if-compiling-with-unsafe
//
//NOTE: The Bytecode, Sequencer, and UsbWrapper DLLs from the Pololu USB SDK folder must be manually added to the “References”
// section of the Solution Explorer, see: https://msdn.microsoft.com/en-us/library/6taasyc6(v=vs.80).aspx
// The respective file paths are:
// pololu-usb-sdk\Maestro\Bytecode
// pololu-usb-sdk\Maestro\Sequencer\precompiled_obj
// pololu-usb-sdk\UsbWrapper_Windows
//

//default using statements from Visual Studio template:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//additional using statements:
//using System.Reflection; //Used by the Pololu UscCmd demo program. Does not seem to be required here.
using Pololu.UsbWrapper;
using Pololu.Usc.Bytecode;
//using Pololu.Usc.Sequencer; //Does not seem to be required here
using Pololu.Usc;

namespace MaestroServoTest
{
class Program
{
static void Main(string[] args)
{
//this section adapted from Pololu UscCmd program
List list = Usc.getConnectedDevices();

        if (list.Count == 0)
        {
            Console.WriteLine("No " + Usc.englishName + " devices found.");
            Console.WriteLine("Press ENTER to exit.");
            Console.ReadLine();  //causes console window to stay open for user to read output
            return;
        }

        DeviceListItem item = null;

        item = list[0];

        Usc usc = new Usc(item);
        //endsection

        byte servoNumber = 0;             //connect a servo to channel specified here
        ushort servoTarget = (2000 * 4);  //enter desired servo pusle width here.  Multiplying by 4 converts to the required
                                          //    units of quarter-microsecond.

        usc.setTarget(servoNumber, servoTarget);

        //print data to console for debugging purposes
        Console.WriteLine("servoNumber is " + servoNumber);
        Console.WriteLine("servoTarget is " + servoTarget + " (" + (servoTarget/4) + " ms)");
        Console.WriteLine("Press ENTER to exit.");
        Console.ReadLine();   //causes console window to stay open for user to read output
    }
}

}
[/code]