Page 1 of 1

Sending bits for digital control

Posted: Wed Jan 22, 2020 3:01 pm
by sajuukaran
Good afternoon everybody,

I am willing to use a RPi for digital control.
The signal must be a wave-type current. To send a 1, I shall send a wave with a 116 microsecond period. To send a 0, it must be a 200 microsecond period wave.
When I'm not sending packets, the signal must be a continuous following of 0.

I thought about using PWM to have a signal with a 200 us period. I could pause this signal to send bits, then resume the zeros.


Here is the problem : Sending bits.
Is it possible to send a single wave with PWM ?

Is there an other way than :
set high,
sleep,
set low,
sleep,
begin again.

My specs : RPi 4, current dev in java.

Thank you all for your help !

Re: Sending bits for digital control

Posted: Wed Jan 22, 2020 3:30 pm
by joan
pigpio waves or wave chains?

http://abyz.me.uk/rpi/pigpio/index.html

Re: Sending bits for digital control

Posted: Wed Jan 22, 2020 3:47 pm
by sajuukaran
Here is the type of signal I am willing to create :
DCCsig.png
DCCsig.png (6.93 KiB) Viewed 620 times
I corrected the periods in my first message : 116 us period for a 1, 200 us for a 0.
To have a proper AC signal, I'll use a spare part that will turn a 0/+3.3V signal into -3.3V/+3.3V.

For now, my problem is the shape of the signal, to send zeros and ones :)

Re: Sending bits for digital control

Posted: Wed Jan 22, 2020 4:29 pm
by joan
Is zero 58 on then 58 off?
Is one 116 on then 116 off?

Re: Sending bits for digital control

Posted: Wed Jan 22, 2020 5:19 pm
by sajuukaran
joan wrote:
Wed Jan 22, 2020 4:29 pm
Is zero 58 on then 58 off?
Is one 116 on then 116 off?
Not exactly :

1 : 58us high then 58us low (116us period) ;
0 : 100us high then 100us low (200 us period).

Re: Sending bits for digital control

Posted: Wed Jan 22, 2020 5:50 pm
by joan
Lots of ways of doing this. Several different ways with pigpio.

The simplest is probably using waves and wave chains with pigs.

Code: Select all

pi3bp:~ $ pigs m 4 w # set GPIO 4 as an output
pi3bp:~ $ pigs wvag 16 0 58 0 16 58 # 58 on/off on GPIO 4 (16 is 1<<4).
2
pi3bp:~ $ pigs wvcre # create wave (happens to be wave number 0)
0
pi3bp:~ $ pigs wvag 16 0 100 0 16 100 # 100 on/off on GPIO 4
2
pi3bp:~ $ pigs wvcre # create wave (happens to be wave number 1)
1
pi3bp:~ $ pigs wvcha 0 1 0 1 0 0 1 1 # transmit waves using wave chain function
pi3bp:~ $ pigs wvcha 1 1 1 0 1 1 0 0 0 1
pi3bp:~ $ 
01010011.png
01010011.png (51.2 KiB) Viewed 581 times
1110110001.png
1110110001.png (50.79 KiB) Viewed 581 times

Re: Sending bits for digital control

Posted: Wed Jan 22, 2020 7:04 pm
by sajuukaran
Thanks a lot !
What I am looking for actually exists !
At least, if I don't have any other way, I can still learn python to use your solution.

Additional problem : I only know java coding ...

I found methods that look similar to your proposition :
Here is a javadoc of pi4j

I understand that "pulse" method could create a wave as well ?

Re: Sending bits for digital control

Posted: Wed Jan 22, 2020 7:24 pm
by joan
pigpio will do the pulses accurately because they are timed by hardware. Other solutions will likely use software timing which will have timing jitter. Jitter may be irrelevant to your application.

Re: Sending bits for digital control

Posted: Wed Jan 22, 2020 8:35 pm
by sajuukaran
Right, definitetly software, definitely irrelevant ...

Have you ever heard about hardware based waves, generated by java methods ?

Re: Sending bits for digital control

Posted: Thu Jan 23, 2020 4:08 pm
by sajuukaran
I think I found a java wrapper of piGpio :

https://github.com/nkolban/jpigpio/blob ... ocket.java

In this library, waves piGpio methods are implemented.
I'll try as soon as I understand how to use it :)

Re: Sending bits for digital control

Posted: Sat Jan 25, 2020 4:05 pm
by sajuukaran
Good afternoon,

I looked into jpigpio, it should do the job for java with very few differences than the original gpio C library.

Now, I'm trying to understand further the mechanism to generate waves.
Here is what I understand from your example :

1 - You create a wave form (is wvag = wave add generic ?)
2 - You use wave create to give this form an ID. The first is supposed to be ID 0.
3 - You do the same for a second wave form, then give it ID 1
4 - You send several waves, calling them with their ID.

I don't really understand the arguments you use in wvag.
I found this piece of code in GPIO documentation :

Code: Select all

G1=4
G2=24

pi.set_mode(G1, pigpio.OUTPUT)
pi.set_mode(G2, pigpio.OUTPUT)

flash_500=[] # flash every 500 ms
flash_100=[] # flash every 100 ms

#                              ON     OFF  DELAY

flash_500.append(pigpio.pulse(1<<G1, 1<<G2, 500000))
flash_500.append(pigpio.pulse(1<<G2, 1<<G1, 500000))

flash_100.append(pigpio.pulse(1<<G1, 1<<G2, 100000))
flash_100.append(pigpio.pulse(1<<G2, 1<<G1, 100000))

pi.wave_clear() # clear any existing waveforms

pi.wave_add_generic(flash_500) # 500 ms flashes
f500 = pi.wave_create() # create and save id

pi.wave_add_generic(flash_100) # 100 ms flashes
f100 = pi.wave_create() # create and save id
In the example you gave above, in wvag, you set 16 0 58, then 0 16 58. It looks similar, but I don't understand how to choose the parameters ...
I only understood that the last one was delay, and therefore the width of the wave :lol:

Thank you for your help !

Re: Sending bits for digital control

Posted: Sat Jan 25, 2020 5:53 pm
by joan
You define a waveform with a series of triplets (three values).

The first value is the set of GPIO to switch on (high).
The second value is the set of GPIO to switch off (low).
The third value is how long this triplet should persist in microseconds (before the next triplet starts ).

The GPIO to switch on and off are defined by a bit mask. To select GPIO x bit (1<<x) is set.

So to select GPIO 5 and 9 the needed value would be (1<<5) | (1<<9) which is 32+512 or 544 in decimal.

Triplet 16 0 58 means switch on GPIO 4, switch off no GPIO, delay 58 microseconds.
Triplet 0 16 58 means switch on no GPIO, switch off GPIO 4, delay 58 microseconds.

Re: Sending bits for digital control

Posted: Sun Jan 26, 2020 10:32 am
by sajuukaran
Brilliant.

I tried several configurations with your indications and it runs perfectly.
I don't even need a spare part to generate AC current anymore, just need a second PWM pin.

Again, thanks a lot ! :D