コード例 #1
0
class Car:
    def __init__(self):
        self.drive_msg = DriveMessage()
        self.check_msg = CheckMessage()
        self._bus = can.interface.Bus(bustype='socketcan', channel='can0')
        self.transmitter = Transmitter(self._bus)
        self.receiver = Receiver(self._bus, self.update)
        self.data = CarData()
        self.last_update = time.time()
        self.notifier = can.Notifier(self._bus, [self.receiver])
        self.controller = PIController(proportional=0.05, integral=0.1)

        gpsd.connect()

        self.send_messages()

    def update(self, data: CarData):
        self.last_update = time.time()
        self.data = data
        if self.tx_check(data.rx_check):
            self.periodic_update()
        else:
            self.send_messages()

    def can_error(self):
        self.check_msg.invalid_message_received()
        self.send_messages()

    @property
    def is_outdated(self):
        return time.time() - self.last_update > 1.0

    @property
    def is_ok(self):
        return self.data.has_control and not self.is_outdated

    def periodic_update(self):
        self.increment_msg_counts()
        self.update_from_controller()
        self.send_messages()
        self.clear_errors()

    def increment_msg_counts(self) -> None:
        self.drive_msg.increment_msg_count()
        self.check_msg.increment_msg_count()

    def update_from_controller(self) -> None:
        self.drive_msg.velocity = Driving.to_can(
            self.controller.update(self.velocity))

    def clear_errors(self) -> None:
        self.check_msg.clear_errors()

    def send_messages(self):
        if self.drive_msg is not None:
            self.transmitter.transmit(self.drive_msg)
        if self.check_msg is not None:
            self.transmitter.transmit(self.check_msg)

    def drive(self, v: float, s: float) -> None:
        print(f'Setting velocity {Driving.to_can(int(v))}')
        self.drive_msg.velocity = Driving.to_can(int(v))
        print(f'Setting steering {Steering.to_can(int(s))}')
        self.drive_msg.steering = Steering.to_can(int(s))

    def release_control(self) -> None:
        self.drive_msg.ctrl = 0

    def shutdown(self) -> None:
        self.release_control()
        self.periodic_update()

        time.sleep(0.2)
        self.transmitter.shutdown()
        time.sleep(0.2)
        self._bus.shutdown()

    def tx_check(self, rx_check: int) -> bool:
        if rx_check > self.check_msg.tx_check or self.check_msg.tx_check == 255:
            self.check_msg.set_tx_check(rx_check)
            return True
        return False

    @property
    def velocity(self) -> float:
        return self.data.velocity

    @velocity.setter
    def velocity(self, velocity: Union[int, float]) -> None:
        self.controller.target = velocity
        # self.drive_msg.velocity = Driving.to_can(int(velocity))

    @property
    def steering_angle(self) -> float:
        return self.data.steering_angle

    @steering_angle.setter
    def steering_angle(self, steering: Union[int, float]) -> None:
        self.drive_msg.steering = Steering.to_can(int(steering))

    @property
    def ebrake(self) -> bool:
        return self.check_msg.stop

    @ebrake.setter
    def ebrake(self, enabled: bool) -> None:
        self.check_msg.stop = enabled

    @property
    def gps_position(self):
        return gpsd.get_current().position()
コード例 #2
0
    # print(airborne)
    # print("Concentration Units (standard)")
    # print("---------------------------------------")
    # print("PM 1.0: %d\tPM2.5: %d\tPM10: %d" %
    #       (pm10_standard, pm25_standard, pm100_standard))
    # print("Concentration Units (environmental)")
    # print("---------------------------------------")
    # print("PM 1.0: %d\tPM2.5: %d\tPM10: %d" % (pm10_env, pm25_env, pm100_env))
    # print("---------------------------------------")
    # print("Particles > 0.3um / 0.1L air:", particles_03um)
    # print("Particles > 0.5um / 0.1L air:", particles_05um)
    # print("Particles > 1.0um / 0.1L air:", particles_10um)
    # print("Particles > 2.5um / 0.1L air:", particles_25um)
    # print("Particles > 5.0um / 0.1L air:", particles_50um)
    # print("Particles > 10 um / 0.1L air:", particles_100um)
    # print("---------------------------------------")

    buffer = buffer[32:]
    # print("Buffer ", buffer)

    if (airborne.particles_03um < 200):
        led.green()
    elif (airborne.particles_03um < 350):
        led.yellow()
    else:
        led.red()

    if airborne_data_list.__len__() > 0 and i % 30 == 0:
        transmitter.transmit(airborne_data_list)
コード例 #3
0
ファイル: main.py プロジェクト: Solanar/CMPUT313_Asn1
def start():
    get_arguments()

    # Transmitter.transmit returns the new size of a block
    new_block_size = Transmitter.transmit(parameter_dict[K],
                                          parameter_dict[F])

    # for T trials, repeat the simulation
    for i in range(parameter_dict[T]):
        # clear this trial's variables
        trials_time = 0
        trials_received_frames = 0
        trials_failed_frames = 0

        # Set the first seed for the simulation
        Simulator.set_seed(parameter_dict[TSeeds][i])

        while (trials_time <= parameter_dict[R]):
            trials_received_blocks = 0  # new frame
            trials_failed_blocks = 0    # new frame

            # set the number of blocks to be transmitted in this frame
            transmissions = parameter_dict[K]

            if (parameter_dict[K] == 0):
                transmissions = 1

            # For K blocks (or 1 if K == 0), simulate the transmission
            for j in range(transmissions):  # range starts at 0
                # frame_failure = 0 if block was transmitted successfully
                block_failure = handle_block(new_block_size,
                                             parameter_dict[E],
                                             parameter_dict[K])

                # record block success or failure
                if (block_failure > 0):
                    trials_failed_blocks += 1
                else:
                    trials_received_blocks += 1

            # set trials_time to number of bits and response overhead
            trials_time += (parameter_dict[F] +
                            (parameter_dict[K] * Transmitter.r)
                            + parameter_dict[A])
            # update number of transmitted frames
            Statistics.update(Statistics.total_frames)
            # frame failed, resend the frame
            if(trials_failed_blocks >= 1):
                trials_failed_frames += 1
            # the last frame being sent (no longer needed) see forums
            #elif(trials_time > parameter_dict[R]):
            #    pass
            # successful transmition
            else:
                Statistics.update(Statistics.correctly_received_frames)
                trials_received_frames += 1

        #a print("Trial number:", i)
        #a print("Received Frames", trials_received_frames)
        #a print("Failed Frames", trials_failed_frames)

        # Assume: Take all K*(F+r) trials_time units into account
        # even if in last frame
        Statistics.append(Statistics.throughput_averages,
                          ((parameter_dict[F] * trials_received_frames)
                           / trials_time))

        if(trials_received_frames != 0):
            # Assume: Take all frames into account, even last frame
            Statistics.append(Statistics.frame_averages,
                              (trials_received_frames + trials_failed_frames) /
                              trials_received_frames)
        else:
            Statistics.append(Statistics.frame_averages, 0)

        # Add to total time
        Statistics.statistics_dict[Statistics.total_time] += trials_time

    # Call Print Statements
    #a print()
    #a print("----------------------------------------------")
    print_input(sys.argv)
    Statistics.set_final_values(parameter_dict[F], parameter_dict[R])
    Statistics.print_frame_ci()
    Statistics.print_throughput_ci()
コード例 #4
0
def start():
    get_arguments()

    # Transmitter.transmit returns the new size of a block
    new_block_size = Transmitter.transmit(parameter_dict[K], parameter_dict[F])

    # for T trials, repeat the simulation
    for i in range(parameter_dict[T]):
        # clear this trial's variables
        trials_time = 0
        trials_received_frames = 0
        trials_failed_frames = 0

        # Set the first seed for the simulation
        Simulator.set_seed(parameter_dict[TSeeds][i])

        while (trials_time <= parameter_dict[R]):
            trials_received_blocks = 0  # new frame
            trials_failed_blocks = 0  # new frame

            # set the number of blocks to be transmitted in this frame
            transmissions = parameter_dict[K]

            if (parameter_dict[K] == 0):
                transmissions = 1

            # For K blocks (or 1 if K == 0), simulate the transmission
            for j in range(transmissions):  # range starts at 0
                # frame_failure = 0 if block was transmitted successfully
                block_failure = handle_block(new_block_size, parameter_dict[E],
                                             parameter_dict[K])

                # record block success or failure
                if (block_failure > 0):
                    trials_failed_blocks += 1
                else:
                    trials_received_blocks += 1

            # set trials_time to number of bits and response overhead
            trials_time += (parameter_dict[F] +
                            (parameter_dict[K] * Transmitter.r) +
                            parameter_dict[A])
            # update number of transmitted frames
            Statistics.update(Statistics.total_frames)
            # frame failed, resend the frame
            if (trials_failed_blocks >= 1):
                trials_failed_frames += 1
            # the last frame being sent (no longer needed) see forums
            #elif(trials_time > parameter_dict[R]):
            #    pass
            # successful transmition
            else:
                Statistics.update(Statistics.correctly_received_frames)
                trials_received_frames += 1

        #a print("Trial number:", i)
        #a print("Received Frames", trials_received_frames)
        #a print("Failed Frames", trials_failed_frames)

        # Assume: Take all K*(F+r) trials_time units into account
        # even if in last frame
        Statistics.append(
            Statistics.throughput_averages,
            ((parameter_dict[F] * trials_received_frames) / trials_time))

        if (trials_received_frames != 0):
            # Assume: Take all frames into account, even last frame
            Statistics.append(Statistics.frame_averages,
                              (trials_received_frames + trials_failed_frames) /
                              trials_received_frames)
        else:
            Statistics.append(Statistics.frame_averages, 0)

        # Add to total time
        Statistics.statistics_dict[Statistics.total_time] += trials_time

    # Call Print Statements
    #a print()
    #a print("----------------------------------------------")
    print_input(sys.argv)
    Statistics.set_final_values(parameter_dict[F], parameter_dict[R])
    Statistics.print_frame_ci()
    Statistics.print_throughput_ci()