Shop OBEX P1 Docs P2 Docs Learn Events
read pulses and store — Parallax Forums

read pulses and store

claym53claym53 Posts: 36
edited 2007-02-21 20:18 in General Discussion
I have a 2 channel am reciever which recieves from a remote controller. The am reciever creates pulses that go to a motor controller, which then moves motors accordingly. Basically an rc car setup.

What I want to be able to do is put a javelin chip between the am reciver and the motor controller that records the am reciever pulses, and store them in a variable·so it can send the pulses·to the motor controller at a later time.

If anyone could write some example code so I could get started, it would be very much appreciated!

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-01-22 20:32
    Check out this thread
    http://forums.parallax.com/showthread.php?p=581618
    for example code on counting pulses.

    regards peter
  • claym53claym53 Posts: 36
    edited 2007-01-25 19:14
    Ok, I looked at the thread on counting pulses, but I still don't understand exactly how to get it to do what I want. I need to be able to record the pulses coming in and put them in a string or something so I can save it to a file on an SD card and reproduce the pulses at a later time.

    You can also see my post about writing to ab sd card here: http://forums.parallax.com/showthread.php?p=626090
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-01-25 19:37
    You want to record a pulsetrain and play it back some time later.
    You can use CPU.pulseIn() as showed in the other thread.
    You need to record the high pulse width and·low period width

    int[noparse]/noparse recbufHP = new int[noparse][[/noparse]128]; //high periods
    int[noparse]/noparse recbufLP = new int[noparse][[/noparse]128]; //low periods

    //record
    for (int i=0; i<128; i++) { //record 128 pulses
    · recbufHP[noparse][[/noparse]i] = CPU.pulseIn(32676,CPU.pin0,true); //record high period
    · recbufLP[noparse][[/noparse]i] = CPU.pulseIn(32767,CPU.pin0,false); //record low period
    }

    CPU.delay(10000); //wait some time

    //playback (on another pin)
    for (int i=0; i<128; i++) {
    · CPU.writePin(CPU.pin1,true); //start high period
    · CPU.delay(recbufHP[noparse][[/noparse]i]);
    · CPU.writePin(CPU.pin1,false); //start low period
    · CPU.delay(recbufLP[noparse][[/noparse]i]);
    }

    Note that the above shows only the primary actions that are required.
    Due to JVM the output pulsetrain will differ in timing from the input pulsetrain
    so it will not be an exact replica in terms of timing.
    If pulses appear on a regular interval (say one pulse every 20 msec) then
    it would be possible to recreate a more accurate output pulsetrain.

    regards peter
  • claym53claym53 Posts: 36
    edited 2007-01-26 19:34
    Ok, here is what I have:

    import stamp.core.*;
    import stamp.math.*;

    public cl(expletive) test{

    static void main() {
    int[noparse]/noparse recbufHP = new int[noparse][[/noparse]128]; //high periods
    int[noparse]/noparse recbufLP = new int[noparse][[/noparse]128]; //low periods
    int[noparse]/noparse recbufHPb = new int[noparse][[/noparse]128];
    int[noparse]/noparse recbufLPb = new int[noparse][[/noparse]128];

    //record
    for (int i=0; i<128; i++) { //record 128 pulses
    recbufHP = CPU.pulseIn(32676,CPU.pin0,true); //record high period
    recbufLP = CPU.pulseIn(32767,CPU.pin0,false); //record low period
    recbufHPb = CPU.pulseIn(32676,CPU.pin1,true);
    recbufLPb = CPU.pulseIn(32767,CPU.pin1,false);

    System.out.println(recbufHP+" - "+recbufLP+" : "+recbufHPb+" - "+recbufLPb);
    }

    CPU.delay(10000); //wait some time

    //playback (on another pin)
    for (int i=0; i<128; i++) {
    CPU.writePin(CPU.pin2,true); //start high period
    CPU.delay(recbufHP);
    CPU.writePin(CPU.pin2,false); //start low period
    CPU.delay(recbufLP);

    CPU.writePin(CPU.pin3,true);
    CPU.delay(recbufHPb);
    CPU.writePin(CPU.pin3,false);
    CPU.delay(recbufLPb);
    }

    }
    }

    It records the pulsetrain on pins 0 and 1 and then outputs them on pins 2 and 3. It seems to be outputting the pulses, but they arent the same as what are put in. Any ideas?
  • claym53claym53 Posts: 36
    edited 2007-01-26 19:39
    It should output one pulse every 20 msec.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-01-27 12:07
    For servo pulses that arrive every 20msec things are simpler
    because you only need to record the high period width (which is always 1-2 msec)

    int[noparse]/noparse recbuf = new int[noparse][[/noparse]128]; //high periods

    CPU.writePin(CPU.pin1,false); //idle state output pin

    //record
    for (int i=0; i<128; i++) { //record 128 pulses
    · recbuf[noparse][[/noparse]i] = CPU.pulseIn(2350,CPU.pin0,true); //record high period, timeout 2350 equals 20.398msec
    }

    CPU.delay(10000); //wait some time

    //playback (on another pin)
    for (int i=0; i<128; i++) {
    · CPU.pulseOut(recbuf[noparse][[/noparse]i],CPU.pin1); //high pulse
    · CPU.delay(2304-recbuf[noparse][[/noparse]i]); //wait until 20msec passed·(2304*8.68usec = 20msec)
    }

    Due to decoding time of javabytes you may need to lower the above 2304 value
    so each pulse starts at 20msec interval (use scope to verify)

    To record 2 channels make sure the pulses are interleaved, that is pulse from ch2 comes
    10msec after pulse for ch1, otherwise it is impossible to record 2 channels.
    I would first try to record and playback one channel.

    regards peter
  • claym53claym53 Posts: 36
    edited 2007-02-20 19:13
    The script lasted posted by peter doesn't seem to be working so I modified it a little and ended up coming up with something like this that seemed to work:

    import stamp.core.*;

    public class RecordPlay{

    static void main() {
    int[noparse]/noparse recbuf = new int[noparse][[/noparse]128]; //high periods

    CPU.writePin(CPU.pin2,false); //idle state output pin

    //record
    for (int i=0; i<128; i++) { //record 128 pulses
    recbuf = CPU.pulseIn(2350,CPU.pin0,true); //record high period, timeout 2350 equals 20.398msec
    System.out.print("record: "+recbuf);
    CPU.delay(2000);
    }

    CPU.delay(10000); //wait some time

    //playback (on another pin)
    for (int i=0; i<128; i++) {
    CPU.pulseOut(recbuf,CPU.pin2); //high pulse
    System.out.print("play: "+recbuf);
    CPU.delay(2000); //wait 20msec
    }

    }
    }

    This code though doesn't work either... it seems to record and output the pulses on the other pin, but they aren't the same pulses.

    Any ideas please!
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-02-20 20:30
    import stamp.core.*;

    public class RecordPlay{

    static void main() {
    int[noparse]/noparse recbuf = new int[noparse][[/noparse]128]; //high periods

    CPU.writePin(CPU.pin2,false); //idle state output pin

    //record
    for (int i=0; i<128; i++) { //record 128 pulses
    recbuf[noparse][[/noparse]i] = CPU.pulseIn(2350,CPU.pin0,true); //record high period, timeout 2350 equals 20.398msec
    System.out.print("record: ");
    System.out.println(recbuf[noparse][[/noparse]i]);
    CPU.delay(100); //wait 10msec
    }

    CPU.delay(10000); //wait some time

    //playback (on another pin)
    for (int i=0; i<128; i++) {
    CPU.pulseOut(recbuf[noparse][[/noparse]i],CPU.pin2); //high pulse
    System.out.print("play: ");
    System.out.println(recbuf);
    CPU.delay(200); //wait 20msec
    }

    }
    }


    regards peter


    Post Edited (Peter Verkaik) : 2/20/2007 8:38:18 PM GMT
  • claym53claym53 Posts: 36
    edited 2007-02-21 16:13
    That works great on one channel, but here is what I tried to do for 2 channels and it didn't seems to work:

    import stamp.core.*;

    public class RecordPlay{

    static void main() {
    int[noparse]/noparse recbuf = new int[noparse][[/noparse]128]; //high periods
    int[noparse]/noparse recbufA = new int[noparse][[/noparse]128];

    CPU.writePin(CPU.pin2,false); //idle state output pin
    CPU.writePin(CPU.pin3,false);

    //record
    for (int i=0; i<128; i++) { //record 128 pulses
    recbuf = CPU.pulseIn(2350,CPU.pin0,true); //record high period, timeout 2350 equals 20.398msec
    recbufA = CPU.pulseIn(2350,CPU.pin1,true);
    System.out.print("record: "+recbuf+" - "+recbufA+"\n");
    CPU.delay(100); //wait 10msec
    }

    CPU.delay(10000); //wait some time

    //playback (on another pin)
    for (int i=0; i<128; i++) {
    CPU.pulseOut(recbuf,CPU.pin2); //high pulse
    CPU.pulseOut(recbufA,CPU.pin3);
    System.out.print("play: "+recbuf+" - "+recbufA+"\n");
    CPU.delay(200); //wait 20msec
    }

    }
    }
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-02-21 20:18
    To record 2 channels
    A) the pulses may not overlap
    B) you then also need to record the interleave time between end of pulse ch1 and start of pulse ch2

    To play back
    A) pulse out ch1
    B) delay interleave [url=mailto:time@record]time[/url]
    C) pulse out ch2
    D) delay 20msec - time pulse1 - interleave time - time pulse2

    If the pulses overlap or the interleave time is too short, you cannot record 2 channels with the javelin.

    regards peter
Sign In or Register to comment.