FEZmini Robot problem: PWM and CompareOutput simultaneously

When programming the FEZ Mini Robot I noticed that the motors Servo’s influence. The engines are controlled with PWM and the Servos with the OutputCompare function GHI.
To be able to easily demonstrate this, I made ​​a small test program. The servo is controlled by pushbuttons and Di2 DI4 and engine with the LDR button.
In the diagram of the control board, I can’t find anything about a possible cause of this problem.
Who can advise me or is it a bug in the library.

The source of the test program:

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;

namespace FEZ_Mini_Application1
{
    public class Program
    {
        public static void Main()
        {
            OutputCompare _srv = new OutputCompare((Cpu.Pin)FEZ_Pin.Digital.Di7, true, 5);
            OutputPort _dir1 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di3, false);
            PWM _pwm1 = new PWM((PWM.Pin)FEZ_Pin.PWM.Di5);
            OutputPort _dir2 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di9, false);
            PWM _pwm2 = new PWM((PWM.Pin)FEZ_Pin.PWM.Di6);
            InputPort _but1 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di2, false, Port.ResistorMode.PullUp); 
            InputPort _but2 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di4, false, Port.ResistorMode.PullUp);
            InputPort _ldr = new InputPort((Cpu.Pin)FEZ_Pin.Digital.LDR, false, Port.ResistorMode.PullUp);
            OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, true);

            uint[] timings = new uint[5]; 
            byte pos = 0;
            int count = 0;
            _dir1.Write(false);
            _dir2.Write(false);
            _pwm1.Set(1000, 0);
            _pwm2.Set(1000, 0);

            while (true)
            {
                if (!_ldr.Read())
                {
                    _pwm1.Set(1000, 50);
                    _pwm2.Set(1000, 50);
                }
                else
                {
                    _pwm1.Set(1000, 0);
                    _pwm2.Set(1000, 0);
                }

                if (!_but1.Read()) 
                {
                    if (pos < 180)
                        pos += 5;
                    if (pos == 0)
                        pos = 1;
                    timings[0] = (uint)(((float)((2500 - 400) / 180) * (pos)) + 400); 
                    timings[1] = 50000;
                    _srv.Set(true, timings, 0, 2, true);
                }

                if (!_but2.Read())
                {
                    if (pos > 5)
                        pos -= 5;
                    if (pos == 0)
                        pos = 1;
                    timings[0] = (uint)(((float)((2500 - 400) / 180) * (pos)) + 400); 
                    timings[1] = 50000;
                    _srv.Set(true, timings, 0, 2, true);

                }
                if (count++ > 20)
                {
                    count = 0;
                    led.Write(!led.Read());
                }
                Thread.Sleep(10);
            }
        }
    }
}

Hello !

Could it be linked to the fact that with Usbizi, as per the documentation (GHI API), “All PWM pins share the same timer. Changing one PWM frequency will affect the others.” ? However your code looks fine 8)

On my robot application, I had to pick a frequency that works fine for the servo to vary speed of my motors ; I am only changing the duty cycle of the 4 PWM I am using (2 for servos, 2 for motors). It is working fine, with the known limitation of the PWM bug described in this thread: http://tinyclr.com/forum/1/2830/

I didn’t implement yet the solution suggested by Wouter, for which is provided some great code here :
http://code.tinyclr.com/project/348/pwm-helper-class-with-clean-transitions/

Hope that helps !
Have fun ! :wink:

How can I turn the motors and servos the same time?
For my robot, the servo arm for a gripping arm and moving a distance sensor.
The simultaneous use of the motors and servos for the application required.

@ Nicolas3, I understand your comment about PWM, but the servo is not connected to a PWM port. For this I use the output compare function for the servo.

You move the servos and the motors speed only by changing the duty cycle - not the frequency. Would you have a scope or a logic analyser to see exactly what is happening on the digital pins ?

Also, did you try to put your 2 servos on different power supplies, jus to make sure the problem does not relate to power better than digital output data ?

To the shaping of a fluctuating power supply to eliminate the plugs of the engine disconnected from the motor control boardje.
The problem was not resolved but remained present even when not connected motors. Generating a PWM signal is enough for the servos with the OutputCompare function to disrupt. Once you set the PWM signal duty cycle of 0 indicates it does work as expected.

This test seems to me clear a firmware problem. If this problem is not resolved, the availability of FEZ Mini Robot for robotic missions games really limited!

GHI would look into this issue?

If I understand this correctly, this problem is a combination of noise in the robot circuit design and in the servos. I have seen it before where the servos get jittery when the wheels spin. The problem disappears if the servos are powered separately from the robot power. Then tested it with different servo and it worked fine. Do you have a different servo of a different brand that you can try?

Hi Hinnie… Thanks for your code… still learning :wink: