Raptor system stuck every couple of seconds when using sensors

I’m using Raptor connected to sensors.
When capturing the events from any of them, every couple of seconds the system stucks for a second and stops pulling data from the sensors for ~1000ms (when normally events are coming every ~15ms).
I tried pulling only from Accelerometer - same issue.

Could you create a small program with as few modules as possible that reliably recreates the problem and post it here?

Welcome to forum.

Sounds like garbage collection. Do garbage collection messages appear in the output windows of Visual Studio when processing suspends?

Thanks for the replay.
The code:

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using System.Diagnostics;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using Gadgeteer.Modules.Seeed;

namespace GadgeteerApp4
{
    public partial class Program
    {
        static long startTime = 0;
        private System.IO.StreamWriter swAcc;
        int numberOfEntriesAc
```cs
c = 0;
        public GT.Timer StepTimer = new GT.Timer(15000);
        public static GT.StorageDevice _storage;
        
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            /*******************************************************************************************
            Modules added in the Program.gadgeteer designer view are used by typing 
            their name followed by a period, e.g.  button.  or  camera.
            
            Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
                button.ButtonPressed +=<tab><tab>
            
            If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
                GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
                timer.Tick +=<tab><tab>
                timer.Start();
            *******************************************************************************************/


            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");

            _storage = sdCard.GetStorageDevice();
            string pathFileAccName = "ChackAcc.csv";
            swAcc = new System.IO.StreamWriter(_storage.RootDirectory + "\\" + "SensorData" + "\\" + pathFileAccName, true);

            //Add title entries here and make sure indice is correct in the sensors data
            string title = "time[ms]," + "acceleration.X[mg0],acceleration.Y[mg0],acceleration.Z[mg0]";
            swAcc.WriteLine("sep=,");
            swAcc.WriteLine(title);
            numberOfEntriesAcc = title.Split(',').Length;

            startTime = DateTime.Now.Ticks;

            accelerometer.MeasurementComplete += new Accelerometer.MeasurementCompleteEventHandler(accelerometer_MeasurementComplete);
            accelerometer.MeasurementRange = Accelerometer.Range.EightG;
            accelerometer.ContinuousMeasurementInterval = new TimeSpan(0, 0, 0, 0, 15);
            accelerometer.StartContinuousMeasurements();

            GT.Timer timer = new GT.Timer(15000);
            timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
            timer.Start();
            led7r.Animate(200, false, true, true);
            
        }

        void timer_Tick(GT.Timer timer)
        {
            accelerometer.StopContinuousMeasurements();

            swAcc.Close();
            _storage.Volume.FlushAll();
            led7r.TurnLightOff(2);
            led7r.TurnLightOff(4);
            led7r.TurnLightOff(6);
        }

        void accelerometer_MeasurementComplete(Accelerometer sender, Accelerometer.Acceleration acceleration)
        {
            AddSample(acceleration, DateTime.Now.Ticks);
        }

        void AddSample(object data, long sampleTime)
        {
            SaveRecordToSD(data, sampleTime);
        }

        public void SaveRecordToSD(object record, long sampleTime)
        {
            System.IO.StreamWriter writer = null;
            //Add sensors data
            string newRecordString = "";
            //Add time stamp
            string timeStamp = ((sampleTime - startTime) / 10000).ToString("F1");
            newRecordString = timeStamp;

            // Acc
            if (record is Accelerometer.Acceleration)
            {
                writer = swAcc;
                Accelerometer.Acceleration acc = (Accelerometer.Acceleration)record;
                newRecordString += CreateCsvLine(1, numberOfEntriesAcc, (acc.X * 1000).ToString(), (acc.Y * 1000).ToString(), (acc.Z * 1000).ToString());
            }
            writer.WriteLine(newRecordString);
        }

        // Creates a csv line with the sensors data
        string CreateCsvLine(int startIndex, int numberOfEntries, params string[] data)
        {
            string csvLine = "";
            for (int i = 1; i <= numberOfEntries; i++)
            {
                if (i < numberOfEntries)
                    csvLine += ",";

                if ((i >= startIndex) && (i < startIndex + data.Length))
                {
                    csvLine += data[i - startIndex];
                }
            }
            return csvLine;
        }
    }
}

looks like lots of string concatenation is being done. this will result in garbage collection.

1 Like

I’m used the same code with Hydra and there were no problem.
Also, I tried it with no string concatenation, and with no memory allocation (just writing straight to the SD) and it still a problem.
I understand that there is no way to disable the GC.

Maybe something is wrong with the Raptor?
Is there any firmware update?

Have you looked at the debug.gc method to see its options? One of the things you can do is turn back on printing GC messages which will tell you when GC is running.I think you need to start debugging this by telling us when the delay occurs, not just that you think it occurs -gee, even telling us how you came to this conclusion would help.

As for firmware updates, what GHI SDK did you install? The 2014 r1 SDK is the current one, and has the latest firmware.

@ yakov.morgen - I have same kind of behaviour… I have created a small test program to measure the performance of SD card write speed… when i run this test program a few times the raptor stops working, i have to reflash the raptor to bring it back to live…

If you remove writing to the SD card, does the Raptor function normally?

I’d go one further - create everything as though you would write to the SD card, but don’t actually output it. That way you can isolate the actual card output versus the string work that usually causes GC to kick in; if the behavior is still there, then it’s most likely GC doing it’s business, otherwise it’s time to look deeper at the SD card