コード例 #1
0
 def on_enter_s2(self):
     lightLED(self.commLedPins, Color.magenta)
     print("Entering on s2")
     # It starts the polling phase and stores lists of the nodes that have replied to it both yes or no
     self.stateFSM["reply_yes"], self.stateFSM["reply_no"] = poll_and_send(
         self.stateFSM["addr"], self.stateFSM["data"], self.radio,
         self.pipe_master_tx, self.pipe_master_rx)
     # We update the variable pointing that we have tried to poll once more
     self.stateFSM["retransmission"] = self.stateFSM["retransmission"] + 1
     # Once the polling is done, it goes to state S3
     return self.to_s3()
コード例 #2
0
 def on_enter_s9(self):
     # If and only if the node has not been the first node in the transmission chain, it will wait for a USB to
     # be connected and once it is, it will save the data on it in a file
     print("Entering on S9")
     if not self.stateFSM["first_tx"]:
         lightLED(self.commLedPins, Color.cyan)
         wait_til_usb_connected()
         write_file_usb(NETWORK_FILENAME_OUTPUT, NETWORK_FILENAME_OUTPUT)
     # THIS IS THE END OF THE NETWORK MODE
     lightLED(self.commLedPins, Color.green)
     print("Finish f*****g NetMode")
     return
コード例 #3
0
 def on_enter_s10(self):
     lightLED(self.commLedPins, Color.red)
     # If the node is not the first transmitter, it adopts a passive role and waits for a hello packet in order
     # it can receive the file
     # TODO: ABRIR LAS 2 PIPES DE SLAVE
     self.radio.stopListening()
     self.radio.openWritingPipe(self.pipe_slave_tx)
     self.radio.openReadingPipe(1, self.pipe_slave_rx)
     print("entering on S10")
     self.stateFSM["polling_addr"] = wait_hello(self.stateFSM["addr"],
                                                self.radio,
                                                self.pipe_slave_rx)
     print("Hello recieved")
     # Once it receives a hello packet, it goes to state S11
     return self.to_s11()
コード例 #4
0
ファイル: main.py プロジェクト: julio-burgos/Rx_TX_RF24
power = RF24_PA_HIGH
commLedPins = [COMMLED_PIN_0, COMMLED_PIN_1, COMMLED_PIN_2]
netwLedPins = [0, 0]  # TODO: To be defined

is_on = False
# is_on = GPIO.input(SWITCH_PIN_ON_OFF)

while True:
    # is_on=False
    try:
        is_on = GPIO.input(SWITCH_PIN_ON_OFF)

        if GPIO.input(SWITCH_PIN_SRI_NW) == True:
            print("Starting SRI mode transmission")
            if GPIO.input(SWITCH_PIN_TX_RX) == True:
                lightLED(commLedPins, Color.red)
                wait_til_usb_connected()
                data = read_file_usb(filenamesri)
            lightLED(commLedPins, Color.cyan)
            while not is_on:
                #print("Waiting for beginning communication")
                is_on = GPIO.input(SWITCH_PIN_ON_OFF)
                time.sleep(0.2)
            #is_on = True

            if GPIO.input(
                    SWITCH_PIN_TX_RX) == True:  # Physically read the pin now
                print("Starting sender")

                lightLED(commLedPins, Color.blue)
                send(pipe, channel, power, rate, BLINKLED_PIN, data)
コード例 #5
0
import RPi.GPIO as GPIO
from colors import Color
from colors import lightLED
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.IN)
GPIO.setup(19, GPIO.IN)

GPIO.setup(20, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)

commLedPins = [16, 20]

# while True:
#     time.sleep(0.1)
#     sw1= GPIO.input(26) #on/off
#     sw2= GPIO.input(19) #tx/rx
#     GPIO.output(16,sw1)
#     GPIO.output(20,sw2)
#     print('SW1:' sw1)
#     print('SW2:' sw2)

while True:
    lightLED(commLedPins, Color.cyan)
    time.sleep(0.5)
    lightLED(commLedPins, Color.green)
    time.sleep(0.5)
コード例 #6
0
    def on_enter_s7(self):
        lightLED(self.commLedPins, Color.blue)
        # In this state, it waits for a packet and, depending on the received packet, it acts accordingly
        print("Entering on S7")
        src, dest1, is_ack, SN, dest2, packet_type, pl = wait_packet(
            self.stateFSM["addr"], self.radio, self.pipe_slave_rx)
        print("A packet has been received")
        print("Packet Type: " + str(packet_type))

        # Maybe, even though we have correctly received the file, the transmitter has not received well the ACK that
        # we have sent. Therefore, we send the ACK again and do nothing more
        if packet_type == NETWORK_PACKET_TYPE_DATA or packet_type == NETWORK_PACKET_TYPE_EOF:
            send_ack(src, self.stateFSM["addr"], self.radio,
                     self.pipe_slave_tx)
            return self.to_s7()
        elif packet_type == NETWORK_PACKET_TYPE_PASSTOKEN:
            print("Node has received a token")
            # Since it receives a token packet, it acknowledges it
            send_ack(src, self.stateFSM["addr"], self.radio,
                     self.pipe_slave_tx)
            # In order to make sure that the sending node has been correctly notified about the good reception of the token, we wait
            # TIMEOUT_TOKEN in order to wait for any repeated token packet that it sends
            time_first = time.time()
            while True:
                # After receiving a token, we wait for some time to receive more packets in case that the sending node
                # has not been correctly notified that the token has been correctly received
                _, _, _, _, _, packet_type, _, timeout_reached = wait_packet_timeout(
                    self.stateFSM["addr"], self.radio, self.pipe_slave_rx,
                    TIMEOUT_TOKEN)
                # If the timeout has been reached at the radio level, we exit the function
                time_actual = time.time()
                # If it has finally reached a the timeout in the lower layers without receiving any pckets, it can be said
                # that the sending node is aware of the good reception and that is in passive state
                if timeout_reached:
                    break
                # If we have received another tocket on time, we acknowledge it again
                elif packet_type == NETWORK_PACKET_TYPE_PASSTOKEN:
                    send_ack(src, self.stateFSM["addr"], self.radio,
                             self.pipe_slave_tx)
                # If the timeout is reached, again it can be said that the sending node is aware of the good reception of the token
                elif time_actual >= time_first + TIMEOUT_TOKEN:
                    break

            # TODO: PARA ENVIAR EL TOKEN, O PARA SER ACTIVOS SI EL TOKEN ES PARA NOSOTROS, DEBEMOS SER ACTIVOS. CERRAMOS LA PIPE DE SLAVE Y ABRIMOS 2 DE MASTER
            self.radio.stopListening()
            self.radio.closeReadingPipe(1)
            self.radio.openWritingPipe(self.pipe_master_tx)
            self.radio.openReadingPipe(1, self.pipe_master_rx)

            print("ACK has sended to acknowledge the token")
            # If it receives a token packet and it is intended to it, it obtains the payload, stores the address
            # of the node that sent the token to it and goes to state S2
            if dest2 == self.stateFSM["addr"]:
                print("Token is for me")
                self.stateFSM["src_token"] = src
                self.stateFSM["token_pl"] = pl
                return self.to_s2()
            # If it receives a token packet and it is not intended to it, it forwards the token to another node
            else:
                # If possible, send the token directly to one of the reachable nodes (that are the ones listed
                # in the reply_yes and reply_no variables)
                if dest2 in self.stateFSM[
                        "reply_yes"] or dest2 in self.stateFSM["reply_no"]:
                    print("Token is one of my neigbours")
                    dest1 = dest2
                # If not, forward the token through the node that sent the token to it
                else:
                    print("Forward the token to the source")
                    dest1 = self.stateFSM["src_token"]
                token_ack_received = False
                time_first = time.time()
                print("Dest1 ", dest1)
                print("Dest2 ", dest2)
                while True:
                    send_network_packet(self.stateFSM["addr"], dest1,
                                        PACKET_FIELD_ISACK_NO_ACK,
                                        PACKET_FIELD_SN_FIRST, dest2,
                                        NETWORK_PACKET_TYPE_PASSTOKEN, pl,
                                        self.radio, self.pipe_master_tx)
                    token_ack_received = wait_token_ack(
                        self.stateFSM["addr"], self.radio, self.pipe_master_rx)
                    time_actual = time.time()
                    if token_ack_received or time_actual >= time_first + TIMEOUT_TOKEN:
                        break

                if not token_ack_received:
                    return self.to_s2()
                else:
                    # If the received token is not directed to it, it stays in the same state S7 after forwarding the token correctly
                    # TODO; SI LO HEMOS ENVIADO BIEN, CERRAMOS LAS PIPES DE MASTER Y ABRIMOS DE SLAVE DE NUEVO
                    self.radio.stopListening()
                    self.radio.closeReadingPipe(1)
                    self.radio.openWritingPipe(self.pipe_slave_tx)
                    self.radio.openReadingPipe(1, self.pipe_slave_rx)
                    return self.to_s7()
        # If it receives a Hello packet, it stores the address of the polling node and goes to the state S13
        elif packet_type == NETWORK_PACKET_TYPE_CONTROL and pl[
                -1] == NETWORK_PACKET_CONTROL_HELLO_PAYLOAD:
            print("Hello packet received")
            self.stateFSM["polling_addr"] = src
            return self.to_s13()
        # If it receives an EOT packet, it goesd to the state S8
        elif packet_type == NETWORK_PACKET_TYPE_CONTROL and pl[
                -1] == NETWORK_PACKET_CONTROL_EOT_PAYLOAD:
            print("EOT packet received")
            # TODO: CERRAR LAS PIPES DE SLAVE Y ABRIR UNA WRITING DE MASTER PARA ENVIAR EL EOT
            self.radio.stopListening()
            self.radio.closeReadingPipe(1)
            self.radio.openWritingPipe(self.pipe_slave_tx)
            return self.to_s8()