Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Passing Servo's to another thread.


  • Please log in to reply
8 replies to this topic

#1 rchelicopter

rchelicopter

    Member

  • Members
  • PipPip
  • 18 posts
  • LocationSan Diego, Ca

Posted 23 July 2012 - 06:47 PM

Hello all, I wonder if someone could help me with some code to pass a servo (Servo Servo1 = new Servo(Pins.GPIO_PIN_D5); from the Main method to another thread (ProcessClientRequest), here is the code that I'm playing with at the moment. using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using Microsoft.SPOT; using SecretLabs.NETMF.Hardware.NetduinoPlus; using Servo_API; namespace Threaded_Project { public static class Program { public static void Main() { Servo Servo1 = new Servo(Pins.GPIO_PIN_D5); Servo Servo2 = new Servo(Pins.GPIO_PIN_D6); Socket Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Server.Bind(new IPEndPoint(IPAddress.Any, 10100)); Server.Listen(1); while (true) { Socket clientSocket = Server.Accept(); new ProcessClientRequest(clientSocket); } } } internal sealed class ProcessClientRequest { private Socket NetduinoSocket; public ProcessClientRequest(Socket clientSocket) { NetduinoSocket = clientSocket; new Thread(ProcessRequest).Start(); //new Thread(ReadTester).Start(); } private void ProcessRequest() { using (NetduinoSocket) { byte[] buffer = Encoding.UTF8.GetBytes("Connected"); NetduinoSocket.Send(buffer, 0, buffer.Length, 0); while (true) { if (NetduinoSocket.Poll(1, SelectMode.SelectRead)) { byte[] bytes = new byte[NetduinoSocket.Available]; int count = NetduinoSocket.Receive(bytes); string request = new String(Encoding.UTF8.GetChars(bytes)); Debug.Print(request); //Servo1.Degree = 100; //Servo2.Degree = 100; } } } } } }

#2 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 23 July 2012 - 08:55 PM

Hi! Interesting project. If you mean passing parameters to a thread, this thread ;-) discusses just that: http://forums.netdui...able-in-the-mf/

#3 rchelicopter

rchelicopter

    Member

  • Members
  • PipPip
  • 18 posts
  • LocationSan Diego, Ca

Posted 23 July 2012 - 09:39 PM

Hi!

Interesting project. If you mean passing parameters to a thread, this thread ;-) discusses just that:

http://forums.netdui...able-in-the-mf/



Thanks for the reply hanzibal, I'm not quite sure thats what I'm looking for as I'm trying to use the initialized PWM port as opposed to using just a parameter.

#4 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 23 July 2012 - 10:18 PM

Do you want to pass an initialized servo interface object as a parameter to another thread?

#5 Nobby

Nobby

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts

Posted 24 July 2012 - 12:03 AM

There's two ways you can do this. -As linked by Hanzibal, you can use lambda expressions to parse the Servo objects to a function as a parameter when creating the thread. -Create a class which encapsulates the Servo objects as members. You can create regular threads for each servo and address the servo objects as this.servo1 and this.servo2. Your example code executes within a static function so it's not possible to use servo objects initialised in that function unless they are parsed to another static function or class method as parameters. Using option two makes it easier to manage your objects and use them across threads.

#6 rchelicopter

rchelicopter

    Member

  • Members
  • PipPip
  • 18 posts
  • LocationSan Diego, Ca

Posted 24 July 2012 - 10:34 PM

Thanks for you help on this, I'm currently at work and dont have access to my Netduino Plus hardware and as a result the Servo_API will not run in the emulator. Can you please look at the following and see if this aproach will work. // create a new thread with the integer value 1 as parameter Thread t = new Thread(new param(Throttle, Steering).worker); // start it t.Start(); class param { private Servo Throttle; private Servo Steering; public param(Servo Throttle, Servo Steering) { // store the param this.Throttle = Throttle; this.Steering = Steering; } public void worker() { Throttle.Degree = 100; Steering.Degree = 100; } }

#7 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 25 July 2012 - 12:40 AM

You're starting one thread to which you pass two servos. The thread then uses these servos in order to adjust the angles by a 100 degrees respectively. Then the thread terminates. Is this what you want do do?

#8 rchelicopter

rchelicopter

    Member

  • Members
  • PipPip
  • 18 posts
  • LocationSan Diego, Ca

Posted 25 July 2012 - 02:13 PM

You're starting one thread to which you pass two servos. The thread then uses these servos in order to adjust the angles by a 100 degrees respectively. Then the thread terminates.

Is this what you want do do?


Hello and thanks again,

It's not exactly what I want to do but its a good start. I just wanted to know if thats the correct way to pass the servos to a thread and it sounds like it will work. I'm going to play with it a bit and i really appreciate your help.

Thanks.

#9 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 25 July 2012 - 02:22 PM

Ok, good luck!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.