Thread Us

hello, i have a problem with thread.

in the beginning of my code i do : US.Resume(); (i had already did US.Start():wink:
than at the end, US.Suspend();
but my Ultrasonic Sensor will not restart.

help plz

how can i pause US detecting and resume it after ?.

@ Tirmit -

We need more information and show us your code please


public static int usVal;
        public static void usRead() //TREAD DU US SENSOR
        {
            
            bool infini = true;
            while (infini==true)
            {
                usVal = USdetect(_pin);
                
            }
        }

public static Thread US = new Thread(usRead);
 public static TristatePort _pin;

...Main()...{

_pin = new TristatePort((Cpu.Pin)FEZ_Pin.Digital.Di52, false, false, Port.ResistorMode.Disabled);
US.Start();
while(true){

while (ScanAvant > 15 || ScanAvant < 10)
                {
                    Thread.Sleep(100);
                    Debug.Print(ScanAvant.ToString());
                    ScanAvant = usVal;

                }
......
US.suspend();
.....
//it is a long code
US.resume();

}}

i use parallax ping sensor :http://www.parallax.com/tabid/768/productid/92/default.aspx
and i use Thread because the sensor doesn’t work when i use Servo.
But it doesn’t work

@ Tirmit Have you seen this driver on codeshare for the PING))) sensor?

http://www.ghielectronics.com/community/codeshare/entry/123

yes, i used this code to create :


public static int USdetect(TristatePort _pin)
        {

            const int TicksPerMicrosecond = (int)TimeSpan.TicksPerMillisecond / 1000;


            _pin.Active = true;
            _pin.Write(true);
            Thread.Sleep(5);
            _pin.Write(false);

            _pin.Active = false; 
            bool high = false;

            do
            {
                high = _pin.Read();
            }
            while (!high);

            long startTime = System.DateTime.Now.Ticks;

            while (high)
            {
                high = _pin.Read();
            }

          
            int ticks = (int)(System.DateTime.Now.Ticks - startTime);

            int microSeconds = (ticks / TicksPerMicrosecond);


            return (microSeconds / 29 / 2);
        }





If you want to control the thread more accurately I would use an auto reset or manual reset event instead of relying on suspend/resume. You can easily “miss” a pulse with suspend/resume and the reading thread will be in a deadlock or something like that.

yes, i want to control my thread, but when i do resume. The us sensor wouldn’t work
is it a problem with servo ?
because, when i do US.start…US.suspend…US.resume and i don’t use servo, it work
but if i use servo…it doesn’t work

There is no guarantee at what instruction the thread will be suspended. For example it can be suspended at the following line:

 while (!high);

So while thread is suspended the sensor itself will send the high pulse back.

So when you resume the thread the pin is already low and the code will be stuck in the infinite loop until you restart the board.

As I said before use AutoResetEvent or ManualResetEvent to control the thread.

1 Like

ok, i ma going to test that

the sensor works again ! But…
i have a question : i need to place Thread.Abort Method after each US.Suspend or US.Resume ?

example :

if i don’t place US.Abort(), the sensor doesn’t work in this code =>


for (int i = 85; i >= 0; i = i-5)
                    {
                        US.Abort();
                        ServoMove(i, servoBase);
                        reset.Set();
                        Thread.Sleep(100);
                        US.Resume();
                        Thread.Sleep(100);
                        int ScanDroite_lent1 = usVal;
                        US.Suspend();
                        Thread.Sleep(100);
                        ServoMove((i-5), servoBase);
                        reset.Set();
                        Thread.Sleep(100);
                        US.Resume();
                        Thread.Sleep(100);
                        int ScanDroite_lent2 = usVal;
                        US.Suspend();
                        reset.Set();
                        Thread.Sleep(100);
                      ...
                            } 
                        }
                    }


but, here :
i don’t place US.Abort(), and the sensor works


ServoMove(0, servoBase);
                reset.Set();
                Thread.Sleep(10);
                US.Resume();
                Thread.Sleep(100);
                ScanDoite = usVal;
...
        

i found the solution, thx for the help.

it works just if i write :


                        US.Suspend();
                        ServoMove(90-i, servoBase); //MOVE THE SERVO
                        reset.Set();                                //AUTORESETEVENT
                        US.Resume();
                        Thread.Sleep(100);
                        ScanDroite_lent2 = usVal; // receive us values