Control 2 motors with Pololu SMC G2

Hi all! I am trying to drive two engines with two Pololu SMC G2 controllers, what I have basically done is modify the example program in c # and then adapt it to the voice control program that I am building. The Example works fine, it does not give errors and it detects the two SMC G2 controllers, but when I use the buttons of the example program, I do not get a response, I pass the program here to see if someone can help me. Thanks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using Pololu.SimpleMotorControllerG2;
using Pololu.UsbWrapper;

namespace Pololu.SimpleMotorControllerG2.SmcG2Example1
{
    public partial class MainWindow : Form
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// This function runs when the user clicks the Forward button.
        /// </summary>
        void forwardButton_Click(object sender, EventArgs e)
        {
            try
            {
                using (Smc device = connectToDevice("52FF-6D06-8365-5650-1248-2467"))  // Find a device and temporarily connect.
                {
                    device.resume();         // Clear as many errors as possible.
                    device.setSpeed(3200);   // Set the speed to full forward (+100%).
                }
                using (Smc device = connectToDevice("52FF-6E06-8365-5650-4655-2467"))  // Find a device and temporarily connect.
                {
                    device.resume();         // Clear as many errors as possible.
                    device.setSpeed(3200);   // Set the speed to full forward (+100%).
                }


            }
            catch (Exception exception)  // Handle exceptions by displaying them to the user.
            {
                displayException(exception);
            }

        }

        /// <summary>
        /// This function runs when the user clicks the Reverse button.
        /// </summary>
        void reverseButton_Click(object sender, EventArgs e)
        {
            try
            {
                using (Smc device = connectToDevice("52FF-6D06-8365-5650-1248-2467"))  // Find a device and temporarily connect.
                {
                    device.resume();         // Clear as many errors as possible.
                    device.setSpeed(-3200);   // Set the speed to full forward (+100%).
                }
                using (Smc device = connectToDevice("52FF-6E06-8365-5650-4655-2467"))  // Find a device and temporarily connect.
                {
                    device.resume();          // Clear as many errors as possible.
                    device.setSpeed(-3200);   // Set the speed to full reverse (-100%).
                }
            }
            catch (Exception exception)  // Handle exceptions by displaying them to the user.
            {
                displayException(exception);
            }
        }

        /// <summary>
        /// This function runs when the user clicks the Stop button.
        /// </summary>
        void stopButton_Click(object sender, EventArgs e)
        {
            try
            {
                using (Smc device = connectToDevice("52FF-6E06-8365-5650-4655-2467"))  // Find a device and temporarily connect.
                {
                    device.stop();  // Activate the USB kill switch


                }

                using (Smc device = connectToDevice("52FF-6D06-8365-5650-1248-2467"))  // Find a device and temporarily connect.
                {
                    device.stop();  // Activate the USB kill switch


                }
            }
            catch (Exception exception)  // Handle exceptions by displaying them to the user.
            {
                displayException(exception);
            }
        }

        /// <summary>
        /// Connects to a Simple Motor Controller using native USB and returns the
        /// Smc object representing that connection.  When you are done with the 
        /// connection, you should close it using the Dispose() method so that other
        /// processes or functions can connect to the device later.  The "using"
        /// statement can do this automatically for you.
        /// </summary>
        Smc connectToDevice(String serialNumber)
        {
            // Get a list of all connected devices of this type.
            List<DeviceListItem> connectedDevices = Smc.getConnectedDevices();

            foreach(DeviceListItem dli in connectedDevices)
            {
                // If you have multiple devices connected and want to select a particular
                // device by serial number, you could simply add a line like this:
                if (dli.serialNumber != "52FF-6E06-8365-5650-4655-2467")
                {
                    continue;
                }

                else if (dli.serialNumber == "52FF-6D06-8365-5650-1248-2467") 
                { 
                    continue; 
                }
                
                Smc device = new Smc(dli); // Connect to the device.
                return device;             // Return the 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).");
        }

        /// <summary>
        /// Displays an exception to the user by popping up a message box.
        /// </summary>
        public 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);
        }

        private void MainWindow_Load(object sender, EventArgs e)
        {

        }
    }
}

me. Thanks

Hello.

Are you getting no response from both your controllers?

It looks like the logic in your connectToDevice() function is wrong. With the current code, the function will only connect to the SMC G2 controller with the serial number “52FF-6E06-8365-5650-4655-2467”. You should replace

if (dli.serialNumber != "52FF-6E06-8365-5650-4655-2467")
{
    continue;
}

else if (dli.serialNumber == "52FF-6D06-8365-5650-1248-2467") 
{
    continue; 
}

with

if (dli.serialNumber != serialNumber)
{
    continue;
}

The continue statement skips over an iteration, meaning the code moves on to the next item (or device in this case) in the list. Instead of hard-coding the serial numbers, you should use the serialNumber argument you pass to the connectToDevice() function.

- Amanda

It Works!

2 Likes