read pulses and store
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!
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
http://forums.parallax.com/showthread.php?p=581618
for example code on counting pulses.
regards peter
You can also see my post about writing to ab sd card here: http://forums.parallax.com/showthread.php?p=626090
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
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?
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
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!
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
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
}
}
}
A) the pulses may not overlap
To play back
A) pulse out ch1
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