Gyro stops taking measurements

Hello,

I wrote a program which should provide gyro data via a tcp server. The client could access it via an echo protocol. The problem is that the gyro stops to fire the gyro_MeasurementComplete event after a short time. The gyro.IsTakingMeasurements property is still true.

This is the gyro initialization code:

int ms = 10;
gyro.Calibrate();
gyro.MeasurementInterval = TimeSpan.FromTicks(TimeSpan.TicksPerMillisecond * ms);
gyro.MeasurementComplete += gyro_MeasurementComplete;

This is the server class:

public class Server
    {
        public delegate void DataReceivedEventHandler(object sender, OnDataReceivedEventArgs e);
        public event DataReceivedEventHandler OnDataReceived;

        private Socket ServerSocket;
        private int Port;

        public Server(int _port)
        {
            Port = _port;

            ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Start();
        }

        public void Start()
        {
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, Port);
            ServerSocket.Bind(localEndPoint);
            ServerSocket.Listen(Int32.MaxValue);

            Debug.Print("Server started");
            NM.SetStatusLED(NetworkManager.LEDStates.ServerRunning, true);
            new Thread(StartServerInternal).Start();
        }

        private void StartServerInternal()
        {
            while (true)
            {
                Socket clientSocket = ServerSocket.Accept();
                ProcessClientRequest request = new ProcessClientRequest(this, clientSocket);
                request.Process();
            }
        }

        private class ProcessClientRequest
        {
            private Socket ClientSocket;
            private Server ServerSocket;

            OnDataReceivedEventArgs args;

            public ProcessClientRequest(Server socket, Socket clientSocket)
            {
                Debug.Print("Client connected");
                ServerSocket = socket;
                ClientSocket = clientSocket;
            }

            public void Process()
            {
                new Thread(ProcessRequest).Start();
            }

            private void ProcessRequest()
            {
                using (ClientSocket)
                {
                    while (true)
                    {
                        try
                        {
                            if (ClientSocket.Poll(5000000, SelectMode.SelectRead))
                            {
                                if (ClientSocket.Available == 0)                                  
                                    break;
                            
                                byte[] buffer = new byte[ClientSocket.Available];
                                int bytesRead = ClientSocket.Receive(buffer, ClientSocket.Available, SocketFlags.None);
                            
                                byte[] data = new byte[bytesRead];
                                buffer.CopyTo(data, 0);
                      
                                if (data[0] != 0)//command
                                {
                                    args = new OnDataReceivedEventArgs(ClientSocket.LocalEndPoint, ClientSocket.RemoteEndPoint, data);
                                    ServerSocket.OnDataReceived(this, args);

                                    if (args.Close)
                                        break;

                                    ClientSocket.Send(args.ResponseData);//return infos
                                }
                            }
                        }
                        catch (Exception)
                        {
                            break;
                        }
                    }
                    Debug.Print("Client disconnected");
                }
            }
        }
    }

I have 3 classes: Program, Server and SensorManager. In Program i initialize Server and SensorManager. The Gyro get’s initialized in the SensorManager class.

Why the infinite loop in StartServerInternal()?

In order to enable that multiple clients can connect

I’m looking at this on my tiny phone while listening at a conference… But it looks to me like your spooling up infinite threads until you run out of memory.

No, it just creates a new thread if a new client connects

you say the gyro events stop happening but you show the code for accepting incoming client. Maybe the problem is in the gyro code?

The code in the gyro event is pretty complicated, but I tested it by itself, it worked as it should.

@ 91378246 - I suggest you develop and post a simple and complete standalone program that displays your problem. That will be most efficient way to get assistance.

Ok, I deleted every unnecessary code(updated²):

Following modules are connected: Gyro, WifiRS21, ucBattery4xAA (Fez Raptor and NETMF SDK 2015 R1-Pre2)

Program.cs:

using Microsoft.SPOT;

using GT = Gadgeteer;
using Gadgeteer.Modules.GHIElectronics;
using Gadgeteer.SocketInterfaces;
using Microsoft.SPOT.Hardware;
using Quadrocopter;


namespace GadgeteerApp2
{
    public partial class Program
    {
        void ProgramStarted()
        {
            Debug.Print("\n-----Program Started-----");

            Control control = new Control(gyro);
            control.StartMeasurements();

            NetworkManager nm = new NetworkManager(wifiRS21);
            nm.ConncectToWifi("ssid", "password");
        }
    }
}

NetworkManager.cs:

using System;
using Microsoft.SPOT;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using GT = Gadgeteer;

namespace GadgeteerApp2
{
    public class NetworkManager
    {
        private Gadgeteer.Modules.GHIElectronics.WiFiRS21 wifiRS21;

        public NetworkManager(Gadgeteer.Modules.GHIElectronics.WiFiRS21 _wifiRS21)
        {
            wifiRS21 = _wifiRS21;
        }

        public void ConncectToWifi(string ssid, string pw)
        {
            if (!wifiRS21.NetworkInterface.Opened)
                wifiRS21.NetworkInterface.Open();
            wifiRS21.NetworkInterface.EnableDhcp();
            wifiRS21.NetworkInterface.EnableDynamicDns();

            try
            {
                wifiRS21.NetworkInterface.Join(ssid, pw);
            }
            catch
            {
                Debug.Print("=====Network not found=====");
                return;
            }

            GT.Timer waitForWifiTimer = new GT.Timer(100);
            waitForWifiTimer.Tick += waitForWifiTimer_Tick;
            waitForWifiTimer.Start();
        }

        private void waitForWifiTimer_Tick(GT.Timer timer)
        {
            if (wifiRS21.NetworkInterface.IPAddress == "0.0.0.0")
                return;

            timer.Stop();
            Debug.Print("Connected to wifi");
            Debug.Print("IP: " + wifiRS21.NetworkInterface.IPAddress);
            wifiRS21.UseThisNetworkInterface();
        }
    }
}

Control.cs:

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Gadgeteer.Modules.GHIElectronics;

namespace Quadrocopter
{
    public class Control
    {
        public Control(Gyro _gyro)
        {
            gyro = _gyro;
            Debug.Print("Initializing Gyro...");
            gyro.LowPassFilter = Gyro.Bandwidth.Hertz256;
            gyro.SampleRateDivider = 39;

            gyro.Calibrate();
            gyro.MeasurementInterval = TimeSpan.FromTicks(TimeSpan.TicksPerMillisecond * ms);
            gyro.MeasurementComplete += gyro_MeasurementComplete;
        }

        Gyro gyro;

        int ms = 10;
        int i = 0;

        private void gyro_MeasurementComplete(Gyro sender, Gyro.MeasurementCompleteEventArgs e)
        {
            i++;
            Debug.Print(i + " times gyro_MeasurementComplete(x:" + e.X + "y:" + e.Y + "z:" + e.Z + ")");
        }

        public void StartMeasurements()
        {
            Debug.Print("Starting to Measure every " + ms + "ms...");
            gyro.StartTakingMeasurements();
        }
    }
}

@ 91378246 - Even that code is a bit too complicated. If you post a very small example using only the gyro module, we can take a look at it on our end.

I updated my last post. I can’t go any smaller than this, because I can’t run a server without an wifi connection…

@ 91378246 - You can take the whole server code out. Essentially just print the gyro measurements to the debug window.

I’m sry, I updated the code again (the error occurs when I run nm.ConncectToWifi()).

You set the sampling at 10ms and in the complete you print this out each time. That is 100 per second and may just too many to keep up.



Try commenting this time line out or change the sampling time to something a little slower to start with.

I changed the sampling time to 100ms; same outcome:

-----Program Started-----
Initializing Gyro…
Starting to Measure every 100ms…
1 times gyro_MeasurementComplete(x:0.20869565217391273y:-0.06956521739130439z:-0.069565217391304279)
2 times gyro_MeasurementComplete(x:0.13913043478260878y:0z:-0.069565217391304279)
3 times gyro_MeasurementComplete(x:0.13913043478260878y:0.06956521739130439z:0)
4 times gyro_MeasurementComplete(x:0.20869565217391273y:-0.27826086956521756z:-0.13913043478260867)
5 times gyro_MeasurementComplete(x:0y:0z:0.13913043478260873)
6 times gyro_MeasurementComplete(x:0y:0.06956521739130439z:-0.13913043478260867)
7 times gyro_MeasurementComplete(x:0y:-0.06956521739130439z:0)
8 times gyro_MeasurementComplete(x:0.06956521739130439y:0.13913043478260878z:0)
9 times gyro_MeasurementComplete(x:0.06956521739130439y:0.13913043478260878z:0.06956521739130439)
10 times gyro_MeasurementComplete(x:0.20869565217391273y:0.06956521739130439z:0)
11 times gyro_MeasurementComplete(x:-0.06956521739130439y:0.13913043478260878z:-0.069565217391304279)
Der Thread ‘’ (0x3) hat mit Code 0 (0x0) geendet.
12 times gyro_MeasurementComplete(x:-0.06956521739130439y:-0.20869565217391317z:-0.069565217391304279)
13 times gyro_MeasurementComplete(x:-0.06956521739130439y:0.06956521739130439z:-0.069565217391304279)
14 times gyro_MeasurementComplete(x:-0.13913043478260878y:0z:0.13913043478260873)
15 times gyro_MeasurementComplete(x:0y:0.06956521739130439z:-0.069565217391304279)
16 times gyro_MeasurementComplete(x:0.06956521739130439y:0.06956521739130439z:0)
17 times gyro_MeasurementComplete(x:-0.06956521739130439y:0.06956521739130439z:0)
18 times gyro_MeasurementComplete(x:0.20869565217391273y:-0.20869565217391317z:-0.069565217391304279)
19 times gyro_MeasurementComplete(x:0.06956521739130439y:-0.06956521739130439z:0)
20 times gyro_MeasurementComplete(x:0.13913043478260878y:0z:0.06956521739130439)
21 times gyro_MeasurementComplete(x:0.13913043478260878y:-0.13913043478260878z:-0.069565217391304279)
22 times gyro_MeasurementComplete(x:0y:0.20869565217391317z:-0.13913043478260867)
23 times gyro_MeasurementComplete(x:0.27826086956521712y:0.06956521739130439z:-0.13913043478260867)
24 times gyro_MeasurementComplete(x:0.20869565217391273y:-0.13913043478260878z:0.06956521739130439)
25 times gyro_MeasurementComplete(x:0.06956521739130439y:-0.27826086956521756z:0)
26 times gyro_MeasurementComplete(x:0y:0z:0.13913043478260873)
27 times gyro_MeasurementComplete(x:0.20869565217391273y:0.20869565217391317z:0.13913043478260873)
28 times gyro_MeasurementComplete(x:0.20869565217391273y:0.06956521739130439z:0)
29 times gyro_MeasurementComplete(x:0.34782608695652151y:0.06956521739130439z:-0.13913043478260867)
30 times gyro_MeasurementComplete(x:0y:0z:0)
31 times gyro_MeasurementComplete(x:0.13913043478260878y:0.20869565217391317z:0.13913043478260873)
32 times gyro_MeasurementComplete(x:0y:0z:-0.069565217391304279)
33 times gyro_MeasurementComplete(x:0y:-0.13913043478260878z:-0.13913043478260867)
34 times gyro_MeasurementComplete(x:-0.06956521739130439y:0.34782608695652151z:0.13913043478260873)
Connected to wifi
IP: 10.0.1.25

@ 91378246 - What happens if you initialize the gyro after connecting to wifi?

Well, then this happens (tried that already a few times, I have to unplug and re plug the module after that error to get it running again. If I don’t do this and try to rebuild it visual studio says that it’s not connected):

186 times gyro_MeasurementComplete(x:-0.27826086956521712y:-0.27826086956521756z:0.069565217391304335)
187 times gyro_MeasurementComplete(x:-0.13913043478260834y:0.34782608695652151z:-0.13913043478260867)
#### Exception System.ApplicationException - 0x00000000 (6) ####
#### Message: SoftwareI2C: Exception writing to device at address 104 - perhaps device is not responding or not plugged in.
#### Gadgeteer.SocketInterfaces.NativeI2CBus::Execute [IP: 004e] ####
#### Gadgeteer.SocketInterfaces.NativeI2CBus::WriteRead [IP: 0073] ####
#### Gadgeteer.SocketInterfaces.I2CBus::WriteRead [IP: 0022] ####
#### Gadgeteer.Modules.GHIElectronics.Gyro::Read [IP: 0016] ####
#### Gadgeteer.Modules.GHIElectronics.Gyro::OnInterrupt [IP: 0006] ####
#### Gadgeteer.SocketInterfaces.NativeInterruptInput::OnPortInterrupt [IP: 0008] ####
Eine Ausnahme (erste Chance) des Typs “System.ApplicationException” ist in Gadgeteer.dll aufgetreten.
188 times gyro_MeasurementComplete(x:-0.069565217391303946y:-0.06956521739130439z:0)
Ein Ausnahmefehler des Typs “System.ApplicationException” ist in Gadgeteer.dll aufgetreten.
Zusätzliche Informationen: SoftwareI2C: Exception writing to device at address 104 - perhaps device is not responding or not plugged in.

@ 91378246 - And if you take wifi out completely does it function properly?

No, the same error occurs after ~150 times gyro_MeasurementComplete() fired