Search code examples
c#serial-portusbuisteppermotordriver

Stellaris Stepper motor RDK USB Serial Interface with PC Application- Packet Format


I'm working with Stellaris Stepper motor RDK from TI- Luminary Micro. Move me to right Forum if required. I'm using USB Serial Interface for interfacing the Device with my Application developed using C#. Currently I'm stuck with sending the control commands from my Application. I can able to start & stop the Stepper motor by sending the following commands. But I'd like to set the Target Steps/sec. I cant understand how to form a command control Packet with required Target Steps. So any Help regarding this is appreciated.

code:

//To startMotor:

byte[] StartMotorRequest = new byte[] {0xff,0x07,0x13,0x14,0x00,0xcf,0xff,
0x04,0x30,0xcd,0xff,0x09,0x13,0x08,0x00,0xd0,0x30,0x0e,0xcf };
 _serialPort.Write(StartMotorRequest, 0, StartMotorRequest.Length);

//To StopMotor:

byte[] StopMotorRequest = new byte[] { 0xff,0x04,0x31,0xcc};
_serialPort.Write(StopMotorRequest, 0, StopMotorRequest.Length);

Thank you,

Balaji.R


Solution

  • Stellaris - Stepper Motor Controller RDK- USB Serial Interface with Computer- Usage Details

    Here is a simple Tutorial to Interface your Stepper motor with your Application using USB Serial Interface.

    Commands are sent to the motor drive with the following format:

    {tag} {length} {command} {optional command data byte(s)} {checksum}

    • The {tag} byte is 0xff.
    • The {length} byte contains the overall length of the command packet, starting with the {tag} and ending with the {checksum}. The maximum packet length is 255 bytes.
    • The {command} byte is the command being sent. Based on the command, there may be optional command data bytes that follow.
    • The {checksum} byte is the value such that the sum of all bytes in the command packet (including the checksum) will be zero. This is used to validate a command packet and allow the target to synchronize with the command stream being sent by the host.
    • The Value of Checksum bytes can be calculated by subtracting all the remaining values from zero. i.e. if the Command is 0x01. Checksum byte can be calculated as [0-(ff+04+01)]= 0xfc.

    So the 0x01 command with no data bytes would be sent as follows:

    0xff 0x04 0x01 0xfc

    To Start the Motor with Predefined Target Steps/Seconds

    CMD_RUN = 0x30 TAG_CMD = 0xff

    Description: Starts the motor running based on the current parameter set, if it is not already running.

    Command:

    TAG_CMD 0x04 CMD_RUN {checksum}

    CMD_SET_PARAM_VALUE

    Description: Sets the value of a parameter. For parameters that have values larger than a single byte, not all bytes of the parameter value need to be supplied; value bytes that are not supplied (that is, the more significant bytes) are treated as if a zero was transmitted. If more bytes than required for the parameter value are supplied, the extra bytes are ignored.

    Command:

    TAG_CMD {length} CMD_SET_PARAM_VALUE {param} {value} [{value} ...] {checksum}

    PARAM_TARGET_POS 0x08

    Description: Specifies the target position of the motor.

    PARAM_USE_ONBOARD_UI 0x1e

    Description: Specifies whether the on-board user interface should be active or inactive.


    Sample code in C# to Start & Stop the Motor Pre-Loaded with Target Steps/Sec

    private void InitializeSerialPort()
            {
                try
                {
                    String ComPortName = ComPortCB.Text;
                    ComPortName = ComPortName.Trim();
                    _serialPort = new SerialPort(ComPortName, 115200, Parity.None, 8, StopBits.One);
                    _serialPort.Handshake = Handshake.None;
                    _serialPort.WriteTimeout = 500;
                    _serialPort.ReadTimeout = 500;
                    _serialPort.Open();
                    _serialPort.Close();
                    _SerialPortInitialization = true;
                }
                catch (NullReferenceException excpt)
                {
                    MessageBox.Show(excpt.Message);
                }
            }
    
    public static void StartStepperMotor()
            {
                try
                {
                    if (!_serialPort.IsOpen)
                        _serialPort.Open();
    
                    byte[] StartMotorRequest = new byte[] { 0xff, 0x04, 0x30, 0xcd };
                    byte[] SetParamTargetPosition = new byte[] { 0xFF, 0x09, 0x13, 0x08, 0x00, 0x50, 0xC3, 0x00, 0xCA };
    
                    byte[] GetDataItems;
                    GetDataItems = new byte[] { 0xff, 0x06, 0x13, 0x1e, 0x00, 0xca };
                    _serialPort.Write(GetDataItems, 0, GetDataItems.Length);
    
                    _serialPort.Write(StartMotorRequest, 0, StartMotorRequest.Length);
                    _serialPort.Write(SetParamTargetPosition, 0, SetParamTargetPosition.Length);
                    //_serialPort.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
                }
            }
    
    public static void StopStepperMotor()
            {
                try
                {
                    if (!_serialPort.IsOpen)
                        _serialPort.Open();
    
                    byte[] StopMotorRequest = new byte[] { 0xff, 0x04, 0x31, 0xcc };
                    _serialPort.Write(StopMotorRequest, 0, StopMotorRequest.Length);
                    //_serialPort.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
                }
            }
    
    private void RotateStepperMotor(int RequiredAngle)
        {
            try
            {
                if (!_serialPort.IsOpen)
                    _serialPort.Open();
    
                ushort Steps = RequiredAngle;
                byte TargetPositionLSB = (byte)(Steps & 0xFFu);
                byte TargetPositionMSB = (byte)((Steps >> 8) & 0xFFu);
    
                byte CheckSumByte;
                CheckSumByte = (byte)(0 - (0xFF + 0x09 + 0x13 + 0x08 + TargetPositionLSB + TargetPositionMSB));
    
    
                byte[] GetDataItems;
                GetDataItems = new byte[] { 0xff, 0x06, 0x13, 0x1e, 0x00, 0xca };
                _serialPort.Write(GetDataItems, 0, GetDataItems.Length);
    
                byte[] StartMotorRequest = new byte[] { 0xff, 0x04, 0x30, 0xcd };
                byte[] SetParamTargetPosition = new byte[] { 0xFF, 0x09, 0x13, 0x08, 0x00, TargetPositionLSB, TargetPositionMSB, 0x00, CheckSumByte };
    
                _serialPort.Write(StartMotorRequest, 0, StartMotorRequest.Length);
                _serialPort.Write(SetParamTargetPosition, 0, SetParamTargetPosition.Length);
    
                _serialPort.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
            }
    
        }