<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">Le 14/10/2014 20:59, Martin Peach a
      écrit :<br>
    </div>
    <blockquote
cite="mid:CAN5xZ3bAZynheCUE2_GVk_UfivuX6Xr-691mtDL3WiQxpZ_G7g@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div>Ah I see...<br>
        </div>
        In your code you're calling gettimeofday() repeatedly while the
        pulse is arriving. This will freeze Pd for the duration of the
        pulse. Maybe that's what causes the segfault?<br>
      </div>
    </blockquote>
    <br>
    That looks like a good explanation.<br>
    <br>
    <blockquote
cite="mid:CAN5xZ3bAZynheCUE2_GVk_UfivuX6Xr-691mtDL3WiQxpZ_G7g@mail.gmail.com"
      type="cite">
      <div dir="ltr">You could use a clock callback in Pd to check for
        the echo, but then your resolution is down to Pd's event loop
        period.<br>
      </div>
    </blockquote>
    <br>
    I've no clue if it's enough or not, for me the only way to be
    accurate is about using wall clock instead of cpu clock, <br>
    <br>
    <blockquote
cite="mid:CAN5xZ3bAZynheCUE2_GVk_UfivuX6Xr-691mtDL3WiQxpZ_G7g@mail.gmail.com"
      type="cite">
      <div dir="ltr">Alternatively start a new thread to measure the
        pulse and use a clock to check for the thread result. That way
        you'll get the most accurate time.<br>
      </div>
    </blockquote>
    <br>
    I don't know if I can do that, but will give a try by looking at
    other externals using threads.<br>
    <br>
    thanks again<br>
    <br>
    pc<br>
    <br>
    <blockquote
cite="mid:CAN5xZ3bAZynheCUE2_GVk_UfivuX6Xr-691mtDL3WiQxpZ_G7g@mail.gmail.com"
      type="cite">
      <div dir="ltr"><br>
        Martin<br>
      </div>
      <div class="gmail_extra"><br>
        <div class="gmail_quote">On Tue, Oct 14, 2014 at 2:21 PM, Colet
          Patrice <span dir="ltr"><<a moz-do-not-send="true"
              href="mailto:colet.patrice@free.fr" target="_blank">colet.patrice@free.fr</a>></span>
          wrote:<br>
          <blockquote class="gmail_quote" style="margin:0 0 0
            .8ex;border-left:1px #ccc solid;padding-left:1ex">
            <div text="#000000" bgcolor="#FFFFFF">
              <div>Le 14/10/2014 19:51, Martin Peach a écrit :<br>
              </div>
              <span class="">
                <blockquote type="cite">
                  <div dir="ltr">Oh sorry, I thought you were using an
                    arduino.<br>
                    <br>
                  </div>
                </blockquote>
                <br>
              </span> I'm using an emulated arduino, it's called pcduino
              and it's the most powerfull miniPC we can actually find.<br>
              PureData is running so fine on it even through ssh there
              is no latency like I've encountered on raspberry,<br>
              and Multitrack USB soundcards runs greatly on it.
              <div>
                <div class="h5"><br>
                  <br>
                  <blockquote type="cite">
                    <div dir="ltr">Martin<br>
                    </div>
                    <div class="gmail_extra"><br>
                      <div class="gmail_quote">On Tue, Oct 14, 2014 at
                        1:16 PM, Colet Patrice <span dir="ltr"><<a
                            moz-do-not-send="true"
                            href="mailto:colet.patrice@free.fr"
                            target="_blank">colet.patrice@free.fr</a>></span>
                        wrote:<br>
                        <blockquote class="gmail_quote" style="margin:0
                          0 0 .8ex;border-left:1px #ccc
                          solid;padding-left:1ex">
                          <div text="#000000" bgcolor="#FFFFFF">
                            <div>Le 14/10/2014 15:06, Martin Peach a
                              écrit :<br>
                            </div>
                            <span>
                              <blockquote type="cite">
                                <div dir="ltr">
                                  <div>Not sure why it segfaults, just
                                    want to say that that method is
                                    really bad. Because of Pd's control
                                    rate as well as the serial interface
                                    latency, your timing will be really
                                    sketchy. The way to do what you want
                                    is to implement the ranging entirely
                                    on the arduino and send the values
                                    back to Pd via [comport]. So a Pd
                                    [pulseIn] object shoud simply send a
                                    request to the arduino to start a
                                    pulse and return the time. It could
                                    be implemented as an abstraction
                                    containing [comport].<br>
                                  </div>
                                  <br>
                                </div>
                              </blockquote>
                              <br>
                            </span> I'm not using serial to read gpio
                            state but fopen() (may be fopen() is not
                            fast enough), like in a raspberry (but it's
                            not on raspberry), I will try to do this
                            with python and use pdsend for retrieving
                            value into puredata.<br>
                            thank you for the advice.<br>
                            pc
                            <div>
                              <div><br>
                                <br>
                                <blockquote type="cite">
                                  <div dir="ltr">Martin<br>
                                  </div>
                                  <div class="gmail_extra"><br>
                                    <div class="gmail_quote">On Mon, Oct
                                      13, 2014 at 9:29 PM, Colet Patrice
                                      <span dir="ltr"><<a
                                          moz-do-not-send="true"
                                          href="mailto:colet.patrice@free.fr"
                                          target="_blank">colet.patrice@free.fr</a>></span>
                                      wrote:<br>
                                      <blockquote class="gmail_quote"
                                        style="margin:0 0 0
                                        .8ex;border-left:1px #ccc
                                        solid;padding-left:1ex">Hello,
                                        I'm trying to implement the
                                        pulseIn() function we can find
                                        in arduino libraries into an
                                        external.<br>
                                        <br>
                                        I couldn't make it work because
                                        some part doesn't seem to behave
                                        like I expect so I'm wondering
                                        if it's caused by puredata
                                        process,<br>
                                        so maybe someone in the list
                                        could enlight me about that.<br>
                                        <br>
                                        The purpose of the external is
                                        about using an ultrasound sensor
                                        connected on gpio that require a
                                        microsecond timer for getting
                                        distance like this:<br>
                                        <br>
                                        <br>
                                        //here is a custom timer using a
                                        struct provided by pcduino
                                        headers:<br>
                                        #include core.h<br>
                                        unsigned long pat_micros() {<br>
                                            struct timeval tv;<br>
                                            gettimeofday(&tv,NULL);<br>
                                            return
                                        tv.tv_sec*(uint64_t)1000000+tv.tv_usec;<br>
                                        }<br>
                                        <br>
                                        //here is the custom pulseIn()
                                        function:<br>
                                        unsigned long
                                        pat_pulseIn(t_pdpcduino *x, int
                                        pin)<br>
                                        {<br>
                                            unsigned long timeout =
                                        100000;<br>
                                            unsigned long start =
                                        pat_micros();<br>
                                        <br>
                                            while( pdpcduino_gpio_read2
                                        (x, pin) == 1 )<br>
                                                if( pat_micros() - start
                                        > timeout )<br>
                                                    return 0;<br>
                                        <br>
                                        // this is where I'm getting a
                                        segmentation fault:<br>
                                        <br>
                                            while ( pdpcduino_gpio_read2
                                        (x, pin) == 0 )<br>
                                                if( pat_micros() - start
                                        > timeout )<br>
                                                return 0;<br>
                                        <br>
                                        //...<br>
                                            unsigned long value =
                                        pat_micros();<br>
                                        <br>
                                            while( pdpcduino_gpio_read2
                                        (x, pin) == 1 )<br>
                                                if( pat_micros() - start
                                        > timeout )<br>
                                                    return 0;<br>
                                        <br>
                                            return pat_micros() - value;<br>
                                        <br>
                                        }<br>
                                        <br>
                                        //...<br>
                                        <br>
                                        <br>
                                        If I reduce the timeout number,
                                        segfault comes randomly after
                                        this function has been
                                        triggered, so I'm wondering if
                                        this method is really
                                        appropriate,<br>
                                        is there an exemple of external
                                        (containing such while loops) I
                                        could get inspired from to write
                                        this pulseIn() function?<br>
                                        <br>
                                        <br>
                                        <br>
                                        <br>
                                        <br>
                                        <br>
_______________________________________________<br>
                                        Pd-dev mailing list<br>
                                        <a moz-do-not-send="true"
                                          href="mailto:Pd-dev@lists.iem.at"
                                          target="_blank">Pd-dev@lists.iem.at</a><br>
                                        <a moz-do-not-send="true"
                                          href="http://lists.puredata.info/listinfo/pd-dev"
                                          target="_blank">http://lists.puredata.info/listinfo/pd-dev</a><br>
                                      </blockquote>
                                    </div>
                                    <br>
                                  </div>
                                </blockquote>
                                <br>
                              </div>
                            </div>
                          </div>
                        </blockquote>
                      </div>
                      <br>
                    </div>
                  </blockquote>
                  <br>
                </div>
              </div>
            </div>
          </blockquote>
        </div>
        <br>
      </div>
    </blockquote>
    <br>
  </body>
</html>