0

Does anyone know how to make delay on raspberry pi until 58 us and 100 us? I am using raspberry pi 4B with python language. I want to make code for sending packet data (I will include my code). I'm currently using time.sleep() to determine where is bit 1 and bit 0, but unfortunately, this can't handle period until 58us and 100us. I want to make bit 1 with length 58us high and 58us low (116us/period) and bit 0 with length 100us high and 100us low (200us/period) Please help me Thank you

import pigpio
import time

PIN_12 = 12
PIN_13 = 13
BIT_TIMINGS = [100, 58]

class Packet:
    def __init__(self):
        self.address = [0, 0, 0, 0, 0, 0, 1, 1]
        self.command = [0, 1, 1, 0, 1, 1, 1, 1]
        self.EOR = [0, 0, 0, 0, 0, 0, 0, 0]

def setup():
    global pi
    pi = pigpio.pi()  # Connect to the local Pi

    pi.set_mode(PIN_12, pigpio.OUTPUT)
    pi.set_mode(PIN_13, pigpio.OUTPUT)
    
    pi.write(PIN_12, 1)  # Set initial state
    pi.write(PIN_13, 0)  # Set initial state

    EOR(train)
    idle_packet(100)

def loop():
    go_straight(train)
    idle_packet(2)

def EOR(train):
    train.EOR = [a ^ b for a, b in zip(train.address, train.command)]

def output_bit(bit_val, pin):
    pi.write(pin, 1)
    time.sleep(BIT_TIMINGS[bit_val] / 1000000.0)
    pi.write(pin, 0)
    time.sleep(BIT_TIMINGS[bit_val] / 1000000.0)

def go_straight(train):
    for _ in range(12):
        output_bit(1, PIN_13)
    output_bit(0, PIN_13)

    for bit in train.address:
        output_bit(bit, PIN_12)
    output_bit(0, PIN_12)

    for bit in train.command:
        output_bit(bit, PIN_12)
    output_bit(0, PIN_12)

    for bit in train.EOR:
        output_bit(bit, PIN_12)
    output_bit(1, PIN_12)

def idle_packet(how_many):
    for _ in range(how_many):
        for _ in range(12):
            output_bit(1, PIN_13)
        output_bit(0, PIN_13)

        for _ in range(8):
            output_bit(1, PIN_12)
        output_bit(0, PIN_12)

        for _ in range(8):
            output_bit(0, PIN_12)
        output_bit(0, PIN_12)

        for _ in range(8):
            output_bit(1, PIN_12)
        output_bit(1, PIN_12)

try:
    train = Packet()
    setup()

    while True:
        loop()

except KeyboardInterrupt:
    pi.stop()  # Cleanup pigpio resources

1 Answer 1

0

You would need to get time in high precision e.g. using time.monotonic_ns() and calculate delay between successive calls.

This is unlikely to give precise results on a multiprocessing OS and would have significant jitter.

You may have better results using waves which use hardware timing.

4
  • Perhaps add a link to abyz.me.uk/rpi/pigpio/examples.html#Python_code/create_wave.py which will generate a script for a wave.
    – joan
    Jan 6 at 8:07
  • is there any library must be included in the code? Jan 8 at 3:23
  • If you are asking about pigpio waves you should ask a new question and you could try joan's suggestion. If you want to do this you need to work out what pattern you want to send which is unclear (at least to me).
    – Milliways
    Jan 8 at 3:49
  • Okay, I'm sorry, but all of my packet were clear but the length pulse is not meet the required standard of DCC packet digital. So I need help is any another way to make length pulse in python can reach that Jan 8 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.