def run(self): try: serial_file = open_serial_port(baudrate=1000000, timeout=None) except Exception as e: raise e is_connected = False # Initialize communication with Arduino while not is_connected: print("Waiting for arduino...") write_order(serial_file, Order.HELLO) bytes_array = bytearray(serial_file.read(1)) if not bytes_array: time.sleep(2) continue byte = bytes_array[0] if byte in [Order.HELLO.value, Order.ALREADY_CONNECTED.value]: is_connected = True print("Connected to Arduino") motor_speed = -56 #start_time = millis(); while True: start_time = millis() led_state = 0 if (start_time % 2000) > 1000: led_state = 4095 # Equivalent to write_i8(serial_file, Order.MOTOR.value) for i in range(64): write_order(serial_file, Order.SERVO) write_i16(serial_file, led_state) # write_i16(serial_file, random.randint(0, 4095)) #order = read_order(serial_file) delayMicroseconds(300) #print("Ordered received: {:?}", order) #time.sleep(1) #for _ in range(1): #order = read_order(serial_file) #if(order == Order.RECEIVED): # index += 1 # print("RECEIVED: ", index) #print("Ordered received: {:?}", order) write_order(serial_file, Order.MOTOR) write_i8(serial_file, 100) #order = read_order(serial_file) elapsed_time = millis() - start_time print("TOTAL TIME: ", elapsed_time)
def __init__(self, port): self.port = port self.is_connected = False # Create serial communication object try: self.slave_device = open_serial_port( serial_port=self.port, baudrate=9600, timeout=1 ) except Exception as e: raise e threading.Thread.__init__(self) threading.Thread.setDaemon(self, daemonic=True)
def init_raspberry(): serial_file = None try: # ouvre la liaison série avec l'arduino serial_file = open_serial_port(baudrate=BAUDRATE) print(serial_file) except Exception as e: print('exception') raise e is_connected = False # Initialize communication with Arduino while not is_connected: write_order(serial_file, Order.HELLO) bytes_array = bytearray(serial_file.read(1)) print(bytes_array) if not bytes_array: time.sleep(2) continue byte = bytes_array[0] if byte in [Order.HELLO.value, Order.ALREADY_CONNECTED.value]: is_connected = True return serial_file
def forceStop(): """ Stop The car """ command_queue.clear() n_received_semaphore.release() n_received_semaphore.release() command_queue.put((Order.MOTOR, 0)) command_queue.put((Order.SERVO, int((THETA_MIN + THETA_MAX) / 2))) if __name__ == '__main__': serial_file = None try: # Open serial port (for communication with Arduino) serial_file = open_serial_port(baudrate=BAUDRATE) except Exception as e: raise e is_connected = False # Initialize communication with Arduino while not is_connected: print("Waiting for arduino...") write_order(serial_file, Order.HELLO) bytes_array = bytearray(serial_file.read(1)) if not bytes_array: time.sleep(2) continue byte = bytes_array[0] if byte in [Order.HELLO.value, Order.ALREADY_CONNECTED.value]: is_connected = True
from robust_serial import write_order, Order from robust_serial.threads import CommandThread, ListenerThread from robust_serial.utils import open_serial_port, CustomQueue def reset_command_queue(): """ Clear the command queue """ command_queue.clear() if __name__ == '__main__': try: serial_file = open_serial_port(baudrate=115200) except Exception as e: raise e is_connected = False # Initialize communication with Arduino while not is_connected: print("Waiting for arduino...") write_order(serial_file, Order.HELLO) bytes_array = bytearray(serial_file.read(1)) if not bytes_array: time.sleep(2) continue byte = bytes_array[0] if byte in [Order.HELLO.value, Order.ALREADY_CONNECTED.value]: is_connected = True
from __future__ import print_function, division, absolute_import import time from robust_serial import write_order, Order, write_i8, write_i16, read_i8, read_order from robust_serial.utils import open_serial_port if __name__ == '__main__': try: serial_file = open_serial_port(baudrate=115200, timeout=None) except Exception as e: raise e is_connected = False # Initialize communication with Arduino while not is_connected: print("Waiting for arduino...") write_order(serial_file, Order.HELLO) bytes_array = bytearray(serial_file.read(1)) if not bytes_array: time.sleep(2) continue byte = bytes_array[0] if byte in [Order.HELLO.value, Order.ALREADY_CONNECTED.value]: is_connected = True print("Connected to Arduino") motor_speed = -56 # Equivalent to write_i8(serial_file, Order.MOTOR.value)
from robust_serial import write_order, Order from robust_serial.threads import CommandThread, ListenerThread from robust_serial.utils import open_serial_port, CustomQueue ''' def reset_command_queue(): """ Clear the command queue LOL just realizized new handshake: python starts by sending, arduino starts by listening... eh actully nah """ command_queue.clear() ''' if __name__ == '__main__': try: serial_file = open_serial_port(serial_port="/dev/cu.usbmodem1411", baudrate=115200, timeout=None) except Exception as e: raise e is_connected = False #print("Order.HEllo evaluates to: ",Order.HELLO) while not is_connected: write_order( serial_file, Order.HELLO ) #basically sends one byte representing the numbe that the broadcast order corresponds to (Hello --> 00000000) bytes_array = bytearray(serial_file.read(1)) if bytes_array[0] == Order.ALREADY_CONNECTED: is_connected = True print("connected!!")
def main(): for arduino_port in arduinoPorts: try: ser = open_serial_port(serial_port=arduino_port.device ,baudrate=baudRate, timeout=1) serial_file.append(ser) except (OSError, serial.SerialException): pass # Wait 3 seconds until Arduino is ready because Arduino resets after serial USB connection time.sleep(3) # create a list of all no compatible Arduinos and no Arduinos serDel = [] IDS = [] # Send a command to verify if it is a compatible Arduino for ser in serial_file: write_order(ser, Order.HELLO) try: dataIn = read_i8(ser) if dataIn>0 and dataIn<10: # It is a compatible Arduino print("Compatible Arduino ID: "+str(dataIn)+" -> "+str(ser.port)) IDS.append(str(dataIn)) else: # No return a valid Data so it is not a compatible arduino ser.close() serDel.append(ser) print("No compatible Arduino -> "+str(ser.port)) except (struct.error, OverflowError): #There is no new data from slave, so it is not an Arduino ser.close() serDel.append(ser) #print("No Arduino -> "+str(ser.port)) pass # Delete all no compatibe Arduinos and no Arduinos from serial_file for delete in serDel: serial_file.remove(delete) if not len(serial_file)>0: print ("There are not compatible Arduinos connected !!") else: while True: print("===============================================================") print("Arduinos Available: ", IDS) user_input = input("Choose Arduino ID: ") try: index_ids=IDS.index(str(user_input)) break except ValueError: print("Invalid ID.") continuos=False try: while True: print("===============================================================") print(" SPEED TEST") print("Caution: If you have already calibrated ESC and want another calibration, first desconnect ESC from power source") print("0: Normal Operation, if you calibrated before") print("1: Calibration") print("2: Quit") print("===============================================================") user_input = input("Enter mode number and then enter: ") try: mode= int(user_input) if mode == 2: break elif mode == 0: write_order(serial_file[index_ids],Order.START) createCSV() time.sleep(5) continuos=True break elif mode == 1: write_order(serial_file[index_ids],Order.CALIBRATION) createCSV() print("Now connect the ESC to power and wait some seconds") time.sleep(12) continuos=True break else: print("No valid mode") continue except ValueError: print("Invalid Value.") pass except KeyboardInterrupt: pass try: print("Arduino data are been collected at "+inputCSV) print("Press KeyboardInterrupt Ctl+c for quit") count=0 line1 = [] while continuos==True: write_order(serial_file[index_ids], Order.DATA) speed = read_i16(serial_file[index_ids]) setpoint = read_i16(serial_file[index_ids]) control = read_i16(serial_file[index_ids]) data = {csvHeaderIndex: count, csvHeaderTime: datetime.datetime.now(), csvHeaderRPM: speed, csvHeaderSetpoint: setpoint, csvHeaderControl: control } line1 = appeandNewRow(data, line1) count=count+1 pass except KeyboardInterrupt: pass write_order(serial_file[index_ids],Order.STOP) serial_file[index_ids].close()