Monday, April 07, 2014

Control multiple servo motor with C# -rcb4.dll

Create the list view to selectect multiple servo to control.
in the list view, add two column (ics no and position)
Create the list servo in rcb4.dll by manual


//initial setting
       private void Form1_Shown(object sender, EventArgs e)
       {
           // make a list of servo in listView
           lisVICSmotor.Items.Clear();
           //search all secvor
           for (int i = 0; i < Rcb4.Rcb4.MaxDeviceNumner; i++)
           {
               ListViewItem item = new ListViewItem(new string[] {i.ToString ("00"),"7500" });
               lisVICSmotor.Items.Add(item);
           }
       }



for check list view ( when selected index changed and position value changed)
//when the servo motor selection column is clicked


       private void lisVICSmotor_SelectedIndexChanged(object sender, EventArgs e)
       {
           // when nothing is slelected


           if (lisVICSmotor.SelectedIndices.Count ==0)
           {
               return;
               
           }
           // get the number of the selected list
           int sindex = lisVICSmotor.SelectedIndices[0];
           //should chose 1 time, gets the curently position
           nmmsPosition.Value = Convert.ToInt32(lisVICSmotor.Items[sindex].SubItems[1].Text);
       }


       // when the value of nmmsPosition has changed, list data is updated
       private void nmmsPosition_ValueChanged(object sender, EventArgs e)
       {
           //exit when no coonection
           if (lisVICSmotor.SelectedIndices.Count ==0)
           {
               return;
           }


           //get the list number that is selected


           int sindex = lisVICSmotor.SelectedIndices[0]; // chose 1
           //set the position into listview[position]
           lisVICSmotor.Items[sindex].SubItems[1].Text = nmmsPosition.Value.ToString();
       }



for creating command to control multiple servo motor
//generated the command to control multiple servo motor
       // when click command button
       private void btnmsCommand_Click(object sender, EventArgs e)
       {
           //make the command
           System.Collections.Generic.Dictionary<int, int> icsList = new Dictionary<int, int>();


           //register the number of frame, here set -1 to frame value
           //note allways have
           icsList.Add(-1, (int)nmmsFrame.Value);


           //remove the data of id that is checked from the list view to the listing
           foreach (ListViewItem item in lisVICSmotor.Items)
           {
               if (item.Checked)
               {
                   try
               {
                       int icsNo = int.Parse(item.SubItems[0].Text);
                       int pos = int.Parse (item.SubItems[1].Text);


                       icsList.Add(icsNo,pos);
               }
               
                   catch
                   {
                       //do not anythinh
                       continue;
                   }


                }
           }


           //generate a command
           ByteList cmd = new ByteList();


           //System.Collections.Generic.Dictionary<int, int> icsList
           cmd.Bytes = Command.RunConstFrameServo(icsList);


           //show in the cmd textbox
           txtCmd.Text = cmd.CommaText;
           recv_count = 4;
          }



Send the generation command
//generated the command to control multiple servo motor
       // when click command button
       private void btnmsCommand_Click(object sender, EventArgs e)
       {
           //make the command
           System.Collections.Generic.Dictionary<int, int> icsList = new Dictionary<int, int>();


           //register the number of frame, here set -1 to frame value
           //note allways have
           icsList.Add(-1, (int)nmmsFrame.Value);


           //remove the data of id that is checked from the list view to the listing
           foreach (ListViewItem item in lisVICSmotor.Items)
           {
               if (item.Checked)
               {
                   try
               {
                       int icsNo = int.Parse(item.SubItems[0].Text);
                       int pos = int.Parse (item.SubItems[1].Text);


                       icsList.Add(icsNo,pos);
               }
               
                   catch
                   {
                       //do not anythinh
                       continue;
                   }


                }
           }


           //generate a command
           ByteList cmd = new ByteList();


           //System.Collections.Generic.Dictionary<int, int> icsList
           cmd.Bytes = Command.RunConstFrameServo(icsList);


           //show in the cmd textbox
           txtCmd.Text = cmd.CommaText;
           recv_count = 4;
          }



1 comment:

Unknown said...

i have some errors like 00 00 00 00 and the motor don't move to any grade only rotate, and i am not used to C#