Home→Forums→MonoBrick Communication Library→Problem with SetPower
Tagged: SetPower method
- This topic has 4 replies, 2 voices, and was last updated 10 years, 7 months ago by Mehrdad.
-
AuthorPosts
-
March 18, 2014 at 22:59 #4062
MehrdadParticipantHi,
I need to thank you first for the incredible library you have provided us. I have a problem with SetPower method. I am trying to develop a PID controller for motor position based on input power (DC motor system identification and control). Since I am going to use it as a lab project for my students, I do not want to use the MoveTo() method or On(at speed) (that will defeat the purpose and challenge of the problem). I assume that this method should turn a motor on at a certain power, but I am not sure if I understood the method right because it does not work for me. Here is a sample code which is supposed to turn motor A on and turn it at 20% power for 2 seconds in 100 millisecond time steps (I have already connected to brick in some other subroutine, so this is just for rotation):
void TestLoop()
{
Stopwatch swouter = new Stopwatch();
Stopwatch swinner = new Stopwatch();
int steptime = 100;
swouter.Start();
while (swouter.ElapsedMilliseconds < 2000)
{
swinner.Reset();
swinner.Start();
brick.MotorA.On(0);
brick.MotorA.SetPower(20,true);while (swinner.ElapsedMilliseconds < steptime)
{}
this.Invoke(new Action(()=>txtApos.Text=brick.MotorA.GetTachoCount().ToString() ));}
brick.MotorA.Off();
}
By the way I can turn the motor with speed (motorA.on(20)) without any problem. Please let me know if it is possible to do so with monobrick or if I am doing something wrong. Thanks!
- This topic was modified 10 years, 8 months ago by Mehrdad.
March 25, 2014 at 21:37 #4089
Anders SøborgKeymasterHi
What school are you working at? And what are you teaching?Sorry for the late reply….
Using the MonoBrick communication library for a PID Controller is not recommended since there is no way to guarantee that your sample rate is constant – so this will give unexpected behaviour. You should run the PID controller directly on the brick itself. You could use the MonoBrick firmware 🙂
Anders
March 25, 2014 at 22:40 #4090
MehrdadParticipantHi Andres,
Thank you for your reply. I am a lab instructor at UofR, Canada and the course is about digital control. I can somehow keep sampling time constant (within an acceptable tolerance) with that stopwatch trick in above code and changing the priority of control thread to high. My original question left unanswered: How can I make SetPower() to work?
Anyway I was pressed with the time and moved to the other available library (LEGO MINDSTORMS EV3 API), but your answer on using SetPower() will be greatly appreciated, since I have found your library more reliable in reading sensors via USB.
Best regards,
MehrdadMarch 26, 2014 at 22:04 #4091
Anders SøborgKeymasterHi
When you use the setpower the driver is not enabled – which of cource it should… so this is a bug.
You can fix it your self. Go to motor.cs and locate:
/// <summary> /// Sets the power of the motor. /// </summary> /// <param name="power">Power to use.</param> /// <param name="reply">If set to <c>true</c> reply from brick will be send.</param> public void SetPower(byte power, bool reply){ output.SetPower(power, reply); }
And replace this with
/// <summary> /// Sets the power of the motor. /// </summary> /// <param name="power">Power to use.</param> /// <param name="reply">If set to <c>true</c> reply from brick will be send.</param> public void SetPower(byte power, bool reply){ output.SetPower(power, reply); output.Start(false); }
once this is done you should be able to use the setpower function like this
using System; using MonoBrick.EV3; using System.Threading; using System.Diagnostics; public static class Program{ static void Main(string[] args) { var ev3 = new Brick<Sensor,Sensor,Sensor,Sensor>("usb"); ev3.Connection.Open(); ev3.MotorA.SetPower(80); System.Threading.Thread.Sleep(5000); ev3.MotorA.SetPower(0); ev3.Connection.Close(); } }
Thanks
Anders
March 29, 2014 at 02:37 #4093
MehrdadParticipantHi Anders,
I haven’t tried it yet but I am sure this will fix the problem. I will let you know when I have checked it.
Again thanks
Mehrdad -
AuthorPosts
You must be logged in to reply to this topic.
Follow