Problem with SetPower

HomeForumsMonoBrick Communication LibraryProblem with SetPower

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #4062
    Author Image
    Mehrdad
    Participant

    Hi,

    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 Author ImageMehrdad.
    #4089
    Author Image
    Anders Søborg
    Keymaster

    Hi
    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

    #4090
    Author Image
    Mehrdad
    Participant

    Hi 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,
    Mehrdad

    #4091
    Author Image
    Anders Søborg
    Keymaster

    Hi

    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

    #4093
    Author Image
    Mehrdad
    Participant

    Hi 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

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

Posted in

Make a donation