def run_usb_vcp_test(): serial_port = USB_VCP() nmea_line = None #serial_port.setinterrupt(-1) while True: # retrieving data if serial_port.any(): nmea_line = str(serial_port.readline(), 'utf-8') if nmea_line: delay(1) nmea_line = None delay(50)
buf = array( 'H', 2048 + int(2047 * math.sin(2 * math.pi * i / 128)) for i in range(128)) dac = DAC(1, bits=12) # Wired to X5 dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR) # Instatiate the USB serial object u = USB_VCP() u.write('Hello world!!') # Enter into an infinite loop while True: # Get a line from the serial connection line = u.readline() # If there is nothing, line will be None if not line is None and line: # What you got is a byte object, convert from bytes (binary data) to python string line = line.decode('ascii') # Split the string at the delimiter, creating a 3-element list els = line.strip().split(',') # Get the scope parameters that are passed (0-based indexing!) pin = els[0] interval_us = int(els[1]) count = int(els[2]) # First extract the pin that we actually want to use as the enumerated value
class Controller(object): def __init__(self): self.vs = USB_VCP() self.direction = 0 self.speed = 0 self.select_dir = 0 self.select_speed = 0 #self.vs.setinterrupt(-1) def dir_menu(self): print('please input your instructions') print('1. set the direction ') print('2. set the speed ') print('3. send the instruction ') def get_instrcution(self): print('please input your instructions ') print('1. set the direction ') print('2. set the speed ') print('3. send the instruction ') while True: if self.vs.any(): pyb.delay(2000) self.instrcution = self.vs.readline() if (self.select_dir == 1): if (self.instrcution == b'1'): self.select_dir = 0 print('forward') self.dir_menu() elif (self.instrcution == b'2'): self.select_dir = 0 print('back') self.dir_menu() elif (self.instrcution == b'3'): self.select_dir = 0 print('leftforward') self.dir_menu() elif (self.instrcution == b'4'): self.select_dir = 0 print('leftback') self.dir_menu() elif (self.instrcution == b'5'): self.select_dir = 0 print('rightforward') self.dir_menu() elif (self.instrcution == b'6'): self.select_dir = 0 print('rightback') self.dir_menu() elif (self.instrcution == b'7'): self.select_dir = 0 print('stop') self.dir_menu() elif (self.instrcution == b'8'): self.select_dir = 0 print('MotorA-F') self.dir_menu() elif (self.instrcution == b'9'): self.select_dir = 0 print('MotorA-R') self.dir_menu() elif (self.instrcution == b'10'): self.select_dir = 0 print('MotorB-F') self.dir_menu() elif (self.instrcution == b'11'): self.select_dir = 0 print('MotorB-R') self.dir_menu() elif (self.instrcution == b'12'): self.select_dir = 0 print('RUN') self.dir_menu() else: print('please set the diretion') elif (self.select_speed == 1): self.select_speed == 0 print('speed is ', int(self.instrcution)) print('please input your instructions') print('1. set the direction ') print('2. set the speed ') print('3. send the instruction ') elif (self.instrcution == b'1'): self.select_dir = 1 print('please set the diretion ') print('1. forward') print('2. back') print('3. leftforward') print('4. leftback') print('5. rightforward') print('6. rightback') print('7. stop') print('8. MotorA-F') print('9. MotorA-R') print('10.MotorB-F') print('11.MotorB-R') print('12.RUN') elif (self.instrcution == b'2'): self.select_speed = 1 print('set the speed ') elif (self.instrcution == b'3'): print('send the instruction ') else: print('test', self.instrcution) print('please input your instructions') print('1. set the direction ') print('2. set the speed ') print('3. send the instruction ')
import time from pyb import (USB_VCP, Servo) SLEEP = 0.1 UP = -90 DOWN = 0 SPEED = 1000 servo = Servo(1) servo.angle(30) p = USB_VCP() while True: if p.any(): command = p.readline().strip() if command == b'up': servo.angle(UP, SPEED) p.write(b'up-ok\n') elif command == b'down': servo.angle(DOWN, SPEED) p.write(b'down-ok\n') else: p.write(command + b'\n') p.write(b'err\n') time.sleep(SLEEP)
def car_control(ret): if ret == 'w': car.Right_move(1) car.Left_move(1) elif ret == 's': car.Right_move(2) car.Left_move(2) elif ret == 'a': car.Right_move(1) car.Left_move(2) elif ret == 'd': car.Right_move(2) car.Left_move(1) elif ret == 'x': car.Right_move(3) car.Left_move(3) while True: led1.toggle() getter.add_string(wifi.receive_char()) getter.add_string(usb_uart.readline()) ret = getter.get_char() if ret is not None: print(ret) car_control(ret) pyb.delay(100)
def run_uav_test(i2c_bus=2): global SIGNALS global SIGNAL_USR SIGNALS[0] = 0 serial_port = USB_VCP() nmea_line = None # serial_port.setinterrupt(-1) disp = display.create_spi_display(SSD1322, 256, 64) i2c = I2C(i2c_bus, freq=400000) devices = i2c.scan() lsm303 = LSM303D(i2c) switch = Switch() speed_pid = PID(target=500, kp=.4, ki=.2, kd=.1) uav['pid'] = speed_pid timestamp = None w = 0 screen_renderers = [ render_gps_screen, render_hud_screen, render_imu_screen ] renderer_idx = 0 render_screen = screen_renderers[renderer_idx] switch.callback(switch_cb) while True: # retrieving data accel, mag = lsm303.read() if serial_port.any(): nmea_line = str(serial_port.readline(), 'utf-8') # processing data x, y, z = accel x, z, y = mag uav['imu']['north'] = atan2(y, x) * 180.0 / pi if nmea_line: update_gps_data(nmea_line) nmea_line = None # sending orders if renderer_idx % 2: if timestamp: pid_value = speed_pid.update( uav['speed'], elapsed_millis(timestamp) / 1000.0) adjust_throttle(serial_port, pid_value) timestamp = millis() if SIGNALS[0] & SIGNAL_USR: renderer_idx = renderer_idx + 1 render_screen = screen_renderers[renderer_idx % len(screen_renderers)] SIGNALS[0] = SIGNALS[0] & ~SIGNAL_USR render_screen(disp.framebuf, uav) disp.send_buffer() delay(50)