CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2009
    Posts
    1

    Contrl servo via COM port with Slider/Trackbar

    Hi guys,

    as i am absolute novice in C# so i have to ask you for help.
    I've got a 8channel servocontroller board based on PIC16F84A. http://www.digitalnemesis.com/info/projects/picservo/
    I want to create simple program, where servo can be controlled by Track bar/slider.
    I have managed to comunicate with servo via simple program, but that program can only activate/deactivate the servo.
    I've been looking over internet to find some solution, but i don't know how to implement it to the program.

    Here is the C# code for activaion/deactivation:
    -------------------
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    using System.IO.Ports;

    namespace serial
    {
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
    public MainForm()
    {
    //
    // The InitializeComponent() call is required for Windows Forms designer support.
    //
    InitializeComponent();

    //
    // TODO: Add constructor code after the InitializeComponent() call.
    //
    }

    void Button1Click(object sender, EventArgs e)
    {
    //configuring the serial port

    serialPort1.PortName="COM1";
    serialPort1.BaudRate=2400;
    serialPort1.DataBits=8;
    serialPort1.Parity=Parity.None;
    serialPort1.StopBits= StopBits.One;

    //opening the serial port
    serialPort1.Open();

    //write data to serial port - lock servo 1
    serialPort1.Write("1");

    //close the port
    serialPort1.Close();
    }

    void Button2Click(object sender, EventArgs e)
    {
    //configuring the serial port

    serialPort1.PortName="COM1";
    serialPort1.BaudRate=2400;
    serialPort1.DataBits=8;
    serialPort1.Parity=Parity.None;
    serialPort1.StopBits= StopBits.One;

    //opening the serial port
    serialPort1.Open();

    //write data to serial port - release servo 1
    serialPort1.Write("A");

    //close the port
    serialPort1.Close();

    }
    }
    }
    ----------------------------------------

    According to example commands from website from link above, if i sent to PIC for example command "2", it will activate servo on channel 2.
    if i send command "B", ot will deactivate servo 2.
    ----------------------------------------
    Hex | Decimal | Data Byte Description
    00 | 0 | Yes = Reset the controller. All outputs off, all servos disabled and all servos position and offset values are set to 128 (midrange). The data byte following this command MUST be zero. To ensure that a reset command it actually executed, send three consecutive zero bytes.

    10 | 16 | Yes = Set the servo output 'Position' value for the servo specified in the channel nibble. The data byte contains the new position value between 0 and 255.

    20 | 32 | Yes = Set the servo output 'Offset' value for the servo specified in the channel nibble. The data byte contains the new offset value between 0 and 255.

    30 | 48 | No = Enable Servo Output. Start generating the servo control pulse for the servo specified in the channel nibble.

    40 | 64 | No = Disable Servo Output. Stop generating the servo control pulse for the servo specified in the channel nibble.

    50 | 80 | No = Set Digital Output On (High). Set the digital output specified in the channel nibble high (5 volts).

    60 | 96 | No = Set Digital Output Off (Low). Set the digital output specified in the channel nibble low (0 volts).
    -------------------------------------------
    Example Commands
    Enable Servo 0: 48 + 0 = 48 (ASCII '0')
    Enable Servo 2: 48 + 2 = 50 (ASCII '2')
    Disable Servo 0: 64 + 0 = 64 (ASCII '@')
    Disable Servo 2: 64 + 2 = 66 (ASCII 'B')
    Set Servo 1 Position to 180: 16 + 1 = 17 then 180

    So how to do it, if i want to use slider to turn servo left/rigt???
    Thank you very much.

    Regards
    Brano

  2. #2
    Join Date
    Jun 2009
    Posts
    1

    Thumbs up Re: Contrl servo via COM port with Slider/Trackbar

    Hi, how´s it going?
    I´m newbie in robotic, but recently I bought a servo controller in this site: www.roboticasimples.com and I made a C# project to test it.

    The code it´s no god, but it´s works....rsrs.

    I made this:
    - C# windows form project;
    - put 2 buttons into the form;
    - put a SerialPort controller into the form;

    When I start my application I make the connection with the serial port like this:

    private void Form1_Load( object sender, System.EventArgs e )
    {
    serial = new SerialPort( "COM1", 2400, Parity.None, 8, StopBits.One );

    if ( !serial.IsOpen )
    {
    serial.Open();
    }

    }

    Double click in the first button..then put this:

    private void button1_Click( object sender, System.EventArgs e )
    {
    Move( 1 ); // move the servo to 0º
    }

    into button two put this:

    private void button2_Click( object sender, System.EventArgs e )
    {
    Move( 180 ); // move servo to 180º
    }

    and I created a private method to move the servo:

    private new void Move( byte posicao )
    {
    servo = 0; // first servo
    var data = new[] { servo, posicao }; // I created a byte array, but we need to use only 2 bytes
    serial.Write( data, 0, data.Length ); // write data into the COM Port
    }

    I hope this code can help you!

    If you discovery more about how to work with serial port and servos, please get in touch, my msn is nanabakid@hotmail.com

    Regards


    Luciano Lima
    MCAD | MCP
    Brasil

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured