controling servo with c#

hi,
with the help of some of you i have built this form :
whith it you can drive servo
by increment,
by typing the degree value,
by cliking on the seromoteur button (make the servo do one rotation)
by scrolling the track bar
And it's for the track bar that i have a problem (the servo don't follow the orders) and it leads to (i think) a problem signal frequency
is someone have an idea...
4.JPG
5.JPG
 

Rick100

Senior Member
Hello,
I notice you have a thread.sleep(300) in one function but not the other. You also have a 'pause 1000' and several other pause commmands in your Picaxe program. The serrxd command is not buffered, so you could be missing bytes. I would try removing the pauses from the Picaxe program and use the PC program to set the time between bytes. You might try using a Timer control in your C# program set at 20 Hz. You also might add a textbox or label to display the value your sending out the serial port, for debuuging purpose. I've just recently started learning C# and it's a very interesting language.

Good luck,
Rick
 

Technical

Technical Support
Staff member
We would use ValueChanged rather than Scroll event, or try the event's e.NewValue rather than trackbar1.Value
And how have you opened port, values > 128 may not work depending on which codepage you are in.
 

grim_reaper

Senior Member
I thought I replied to this thread this afternoon, but apparently it didn't get posted :|

I suggested changing the line 'f = true;' to 'f = checkBox1.Checked;', so that f isn't always True, but that's insignificant.
As Rick mentioned, I think you need to add a Sleep() in the trackBar1 routine as well - otherwise scrolling will be hammering the serial port with data.
Technical's comments would also help this.
The .NET serial port object is notorious for being unreliable in certain situations. I had very annoying intermittent problems with it last year, and ended up adding all sorts of hacks with the help of StackOverflow and Google. Opening and closing the port quickly (especially when you debug/stop/debug/stop/debug/stop your code a lot) can lock the port up.

And I've just realised, you're adding 150 to your trackBar1.Value [which you didn't tell us the min/max/range of!] so if the result is over 255 converting to a Byte will produce a possibly undesired result.
 
hi
I have tested all of your suggestions, the better routine is :

for c#
private void trackBar1_ValueChanged(object sender, EventArgs e)
{
int a = (int)trackBar1.Value;
byte[] buf = new byte[1];
buf[0] = (byte)a;
serialPort1.Write(buf, 0, 1);

}

for pic:
main :
serxxd b0
servo 0, b0
goto main

however if the trackbar is moved too fast the servo can't follow the command signal
 

inglewoodpete

Senior Member
however if the trackbar is moved too fast the servo can't follow the command signal
I suspect that PC is sending out updated position data too fast for the PICAXE to keep up with. There are a couple of options you could try: 1. Put a delay of a couple of milliseconds between data bytes transmitted via the serial port of the PC or 2. Use background receive on the PICAXE.

Which PICAXE model are you using?
 

grim_reaper

Senior Member
As Rick mentioned, I think you need to add a Sleep() in the trackBar1 routine as well - otherwise scrolling will be hammering the serial port with data.
Also, please read back to Technical's suggestions regarding which event to handle and using the parameter supplied.
 
Top