Friday, April 04, 2014

C# Programming for control Humanoid Kondo Robot

1.    Creating new window form application by Microsoft visual studio 2010 ( C#)
2.    Add Rcb4.dll and Extensions.dll  (down file here RCBdll)
And using rcb4 and extensions.Collections in main program (see Fig1)
3. Create the interface for the program
.
(Test with control 1 motor servo)
Chose ICS No, Frame, Position, and click Command -> Send.
4.    Wite the Command function
-          When Command button is pressed, the command for kondo is generated  
-          And shown in the Text box Cmd
-          Send button to send the command to rcb4
5.    Setup a serialPort1
BaudRate: 1250000
Parity: Even
Insert the SerialPort1 from the Toolbox, and depend on the Com number port ( when install the KondoSerial ) ( down file here KO Driver 2013 )
6. Simple code for control one motor. (download file C#)

public partial class Form1 : Form
{
    //Manage the number of data receiving
    int recv_count = 0;
    public Form1()
    {
        InitializeComponent();
      
    }
    // opem a COM port
    private void Form1_Load(object sender, EventArgs e)
    {
        if (!serialPort1.IsOpen)
        {
               serialPort1.Open();           
        }
    }
   
    // close the serial port when close the form
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (serialPort1.IsOpen)
        {
            serialPort1.Close(); // Close the serial port
        }
    }
    #region single servo
    // when the command button is pressed, the command is generated and showning
    // in the cmd textbox.
    private void ssbtnCommand_Click(object sender, EventArgs e)
    {
        ByteList cmd = new ByteList(); // creat the new cmd
        cmd.Bytes = Command.RunSingleServo((byte)cmdssICSMotor.SelectedIndex, (byte)nmssFrame.Value, (int)nmssPosition.Value);
        // shown the generation command in the cmd textbox
        txtCmd.Text = cmd.CommaText;
        // required when you actually send the command, to keep the number of received data
        //recv_count
        recv_count = 4;
    }
    #endregion single servo
    #region SendCommand
    // send the geniration command to rcb4
    private void btnSend_Click(object sender, EventArgs e)
    {
        // check the Com port
        if (!serialPort1.IsOpen)
        {
            MessageBox.Show("Com port is not connected"); // Return if Comport is not connected
            return;
        }
        // check the cmd box
        if (txtCmd.Text == string.Empty)
        {
            MessageBox.Show("Command has not been generated"); // Return if cmd box is empty
            return;
        }
       //send generation command
        ByteList cmd = new ByteList();
        cmd.CommaText = txtCmd.Text; // get text from the Cmd box
        // received Array is reply
        byte[] recv = new byte[recv_count];
        bool result = Command.Synchronize(serialPort1, cmd.Bytes, ref recv);
        // check the result after click send button
        if (result == true)
        {
            txtError.BackColor = SystemColors.Window;
        }
        else
        {
            txtError.BackColor = Color.Red;
        }
        // get the reply ByteList
        cmd.Bytes = recv;
        // Write the answer in the error textBox
        txtError.Text = cmd.CommaText;
    }
    #endregion SendCommand
}

No comments: