初始化提交
This commit is contained in:
BIN
boards/default/micropython_robot/build/Feiyi-0x0-V1.19.1-lib.bin
Normal file
BIN
boards/default/micropython_robot/build/Feiyi-0x0-V1.19.1-lib.bin
Normal file
Binary file not shown.
BIN
boards/default/micropython_robot/build/Feiyi-0x0-V1.19.1.bin
Normal file
BIN
boards/default/micropython_robot/build/Feiyi-0x0-V1.19.1.bin
Normal file
Binary file not shown.
Binary file not shown.
BIN
boards/default/micropython_robot/build/MixBot-0x1000-V1.19.1.bin
Normal file
BIN
boards/default/micropython_robot/build/MixBot-0x1000-V1.19.1.bin
Normal file
Binary file not shown.
Binary file not shown.
BIN
boards/default/micropython_robot/build/RM_E1-0x1000-V1.19.1.bin
Normal file
BIN
boards/default/micropython_robot/build/RM_E1-0x1000-V1.19.1.bin
Normal file
Binary file not shown.
203
boards/default/micropython_robot/build/lib/bot51.py
Normal file
203
boards/default/micropython_robot/build/lib/bot51.py
Normal file
@@ -0,0 +1,203 @@
|
||||
"""
|
||||
BOT51
|
||||
|
||||
Micropython library for the BOT51(ADC*7, Motor*2*2, Matrix12x12, on/off)
|
||||
=======================================================
|
||||
|
||||
#Preliminary composition 20230812
|
||||
|
||||
@dahanzimin From the Mixly Team
|
||||
"""
|
||||
import uframebuf
|
||||
from micropython import const
|
||||
|
||||
_BOT51_ADDRESS = const(0x26)
|
||||
_BOT5_ID = const(0x00)
|
||||
_BOT51_VBAT = const(0x01)
|
||||
_BOT51_PS = const(0x03)
|
||||
_BOT51_ALS = const(0x07)
|
||||
_BOT51_FLAG = const(0x0B)
|
||||
_BOT51_MOTOR = const(0x0C)
|
||||
_BOT51_LEDS = const(0x10)
|
||||
|
||||
class BOT51(uframebuf.FrameBuffer_Uincode):
|
||||
def __init__(self, i2c_bus, address=_BOT51_ADDRESS, brightness=0.8, font_address=0x3A0000, width=12, height=12):
|
||||
self._i2c= i2c_bus
|
||||
self._addr = address
|
||||
if self._rreg(_BOT5_ID) != 0x26:
|
||||
raise AttributeError("Cannot find a BOT51")
|
||||
self._buffer = bytearray((width + 7) // 8 * height)
|
||||
super().__init__(self._buffer, width, height, uframebuf.MONO_HMSB)
|
||||
self.reset()
|
||||
self.font(font_address)
|
||||
self.set_brightness(brightness)
|
||||
|
||||
def _wreg(self, reg, val):
|
||||
'''Write memory address'''
|
||||
self._i2c.writeto_mem(self._addr, reg, val.to_bytes(1, 'little'))
|
||||
|
||||
def _rreg(self, reg, nbytes=1):
|
||||
'''Read memory address'''
|
||||
self._i2c.writeto(self._addr, reg.to_bytes(1, 'little'))
|
||||
return self._i2c.readfrom(self._addr, nbytes)[0] if nbytes<=1 else self._i2c.readfrom(self._addr, nbytes)[0:nbytes]
|
||||
|
||||
def reset(self):
|
||||
"""Reset all registers to default state"""
|
||||
for reg in range(_BOT51_MOTOR, _BOT51_MOTOR + 28):
|
||||
self._wreg(reg, 0x00)
|
||||
|
||||
def shutdown(self, flag=True):
|
||||
"""This function is only available on battery power"""
|
||||
if flag:
|
||||
self._wreg(_BOT51_FLAG, (self._rreg(_BOT51_FLAG)) & 0XBF)
|
||||
else:
|
||||
self._wreg(_BOT51_FLAG, (self._rreg(_BOT51_FLAG)) | 0X40)
|
||||
|
||||
def get_brightness(self):
|
||||
return self._brightness
|
||||
|
||||
def set_brightness(self, brightness):
|
||||
if not 0.0 <= brightness <= 1.0:
|
||||
raise ValueError("Brightness must be a decimal number in the range: 0.0-1.0")
|
||||
self._brightness = brightness
|
||||
self._wreg(_BOT51_FLAG, round(15 * brightness) | (self._rreg(_BOT51_FLAG)) & 0xF0)
|
||||
|
||||
def direction(self, angle=0):
|
||||
'''set display direction '''
|
||||
if not 0 <= angle <= 3:
|
||||
raise ValueError("Direction must be a number in the range: 0~3")
|
||||
self._wreg(_BOT51_FLAG, angle << 4 | (self._rreg(_BOT51_FLAG)) & 0xCF)
|
||||
|
||||
def show(self):
|
||||
"""Refresh the display and show the changes."""
|
||||
self._i2c.writeto_mem(self._addr, _BOT51_LEDS, self._buffer)
|
||||
|
||||
def pwm(self, index, duty=None):
|
||||
"""Motor*2*2 PWM duty cycle data register"""
|
||||
if not 0 <= index <= 3:
|
||||
raise ValueError("Motor port must be a number in the range: 0~3")
|
||||
if duty is None:
|
||||
return self._rreg(_BOT51_MOTOR + index)
|
||||
else:
|
||||
if not 0 <= duty <= 255:
|
||||
raise ValueError("Duty must be a number in the range: 0~255")
|
||||
self._wreg(_BOT51_MOTOR + index, duty)
|
||||
|
||||
def motor(self, index, action, speed=0, atten=0.4):
|
||||
speed = round(max(min(speed, 100), -100) * atten)
|
||||
if action=="N":
|
||||
self.pwm(index * 2, 0)
|
||||
self.pwm(index * 2 + 1, 0)
|
||||
elif action=="P":
|
||||
self.pwm(index * 2, 255)
|
||||
self.pwm(index * 2 + 1, 255)
|
||||
elif action=="CW":
|
||||
if speed >= 0:
|
||||
self.pwm(index * 2, 0)
|
||||
self.pwm(index * 2 + 1, speed * 255 // 100)
|
||||
else:
|
||||
self.pwm(index * 2, - speed * 255 // 100)
|
||||
self.pwm(index * 2 + 1, 0)
|
||||
elif action=="CCW":
|
||||
if speed >= 0:
|
||||
self.pwm(index * 2, speed * 255 // 100)
|
||||
self.pwm(index * 2 + 1, 0)
|
||||
else:
|
||||
self.pwm(index * 2, 0)
|
||||
self.pwm(index * 2 + 1, - speed * 255 // 100)
|
||||
elif action=="NC":
|
||||
return round(self.pwm(index * 2) * 100 / 255 / atten), round(self.pwm(index * 2 + 1) * 100 / 255 / atten)
|
||||
else:
|
||||
raise ValueError('Invalid input, valid are "N","P","CW","CCW"')
|
||||
|
||||
def move(self, action, speed=100, atten=0.4):
|
||||
if action=="N":
|
||||
self.motor(0, "N")
|
||||
self.motor(1, "N")
|
||||
elif action=="P":
|
||||
self.motor(0, "P")
|
||||
self.motor(1, "P")
|
||||
elif action=="F":
|
||||
self.motor(0, "CCW", speed, atten)
|
||||
self.motor(1, "CW", speed, atten)
|
||||
elif action=="B":
|
||||
self.motor(0, "CW", speed, atten)
|
||||
self.motor(1, "CCW", speed, atten)
|
||||
elif action=="L":
|
||||
self.motor(0, "CW", speed, atten)
|
||||
self.motor(1, "CW", speed, atten)
|
||||
elif action=="R":
|
||||
self.motor(0, "CCW", speed, atten)
|
||||
self.motor(1, "CCW", speed, atten)
|
||||
else:
|
||||
raise ValueError('Invalid input, valid are "N","P","F","B","L","R"')
|
||||
|
||||
def read_bat(self, ratio=5/1023):
|
||||
'''Read battery power'''
|
||||
vbat= self._rreg(_BOT51_VBAT) << 2 | self._rreg(_BOT51_VBAT + 1) >> 6
|
||||
return round(vbat * ratio, 2)
|
||||
|
||||
def read_ps(self, index, ratio=100/1023):
|
||||
'''Read proximity sensor values'''
|
||||
if not 0 <= index <= 1:
|
||||
raise ValueError("Proximity sensor port must be a number in the range: 0,1")
|
||||
vps= self._rreg(_BOT51_PS + (1 - index) * 2) << 2 | self._rreg(_BOT51_PS + (1 - index) * 2 + 1) >> 6
|
||||
return round(vps * ratio, 2)
|
||||
|
||||
def read_als(self, index, ratio=100/255):
|
||||
'''Read light sensor values'''
|
||||
if not 0 <= index <= 3:
|
||||
raise ValueError("Light sensor port must be a number in the range: 0~3")
|
||||
vals= self._rreg(_BOT51_ALS + index)
|
||||
return round(vals * ratio)
|
||||
|
||||
"""Graph module"""
|
||||
HEART_SMALL=b'\x00\x00\x00\x00\x88\x00\xdc\x01\xfe\x03\xfe\x03\xfc\x01\xf8\x00p\x00 \x00\x00\x00\x00\x00'
|
||||
HEART=b'\x00\x00\x88\x00\xdc\x01\xfe\x03\xff\x07\xff\x07\xfe\x03\xfc\x01\xf8\x00p\x00 \x00\x00\x00'
|
||||
HAPPY=b'\x00\x00\x00\x00\x01\x08\x01\x08\x01\x08\x00\x00\x00\x00\x04\x02\x08\x01\x90\x00`\x00\x00\x00'
|
||||
SAD=b'\x00\x00\x00\x00\x07\x0e\x00\x00\x00\x00\x00\x00\x00\x00`\x00\x90\x00\x08\x01\x04\x02\x00\x00'
|
||||
SMILE=b'\x00\x00\x00\x00\x01\x08\x03\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x03\x0c\x0e\x07\xf8\x01\x00\x00'
|
||||
SILLY=b'\x00\x00\x00\x00\x01\x08\x02\x04\x01\x08\x00\x00\x00\x00\xf8\x01\x08\x01\x90\x00`\x00\x00\x00'
|
||||
FABULOUS=b'\x00\x00\x01\x08\x02\x04\x03\x0c\x00\x00\x00\x00\xfe\x07\x02\x04\x02\x04\x04\x02\xf8\x01\x00\x00'
|
||||
SURPRISED=b'`\x00`\x00`\x00`\x00`\x00`\x00`\x00`\x00\x00\x00`\x00`\x00\x00\x00'
|
||||
ASLEEP=b'\x00\x00\x00\x00\x00\x00\x03\x0c\x00\x00\x00\x00\x00\x00\x00\x00\xf8\x01\x04\x02\xf8\x01\x00\x00'
|
||||
ANGRY=b'\x00\x00\x08\x01\x04\x02\x02\x04\x01\x08\x00\x00\x00\x00`\x00\x90\x00\x08\x01\x04\x02\x02\x04'
|
||||
CONFUSED=b'\xf0\x00\x98\x01\x0c\x03\x0c\x03\x00\x03\x80\x01\xc0\x00`\x00`\x00\x00\x00`\x00`\x00'
|
||||
NO=b'\x00\x00\x00\x00\x04\x02\x08\x01\x90\x00`\x00`\x00\x90\x00\x08\x01\x04\x02\x00\x00\x00\x00'
|
||||
YES=b'\x00\x00\x00\x00\x00\x0c\x00\x06\x00\x03\x80\x01\xc3\x00f\x00<\x00\x18\x00\x00\x00\x00\x00'
|
||||
LEFT_ARROW=b'\x00\x00\x00\x00\x10\x00\x08\x00\x04\x00\xfa\x07\x04\x00\x08\x00\x10\x00\x00\x00\x00\x00\x00\x00'
|
||||
RIGHT_ARROW=b'\x00\x00\x00\x00\x80\x00\x00\x01\x00\x02\xfe\x05\x00\x02\x00\x01\x80\x00\x00\x00\x00\x00\x00\x00'
|
||||
DRESS=b'\x00\x00\x00\x00\x88\x00\xfc\x01\xd8\x00\x70\x00\xa8\x00\x54\x01\xaa\x02\xfc\x01\x00\x00\x00\x00'
|
||||
TRANSFORMERS=b'\x00\x00\x40\x00\x40\x00\xb0\x01\x50\x01\x50\x01\x50\x01\xa0\x00\xa0\x00\x10\x01\x08\x02\x00\x00'
|
||||
SCISSORS=b'\x00\x00\x01\x04\x03\x06\x06\x03\x8c\x01\xd8\x00\x70\x00\x20\x00\xdc\x01\x52\x02\x52\x02\x8c\x01'
|
||||
EXIT=b'\x00\x00\xc0\x00\x40\x00\x70\x00\xe8\x00\x24\x03\x20\x00\x54\x00\x90\x00\x10\x01\x00\x02\x00\x00'
|
||||
TREE=b'\x00\x00\x20\x00\x70\x00\xf8\x00\xfc\x01\xfe\x03\xff\x07\x20\x00\x20\x00\x20\x00\x20\x00\x00\x00'
|
||||
PACMAN=b'\x00\x00\xfc\x01\x06\x02\x23\x01\x83\x00\x41\x00\x83\x00\x03\x01\x06\x02\xfc\x01\x00\x00\x00\x00'
|
||||
TARGET=b'\x00\x00\x00\x00\xf0\x01\x08\x02\x08\x02\x48\x02\x08\x02\x08\x02\xf0\x01\x00\x00\x00\x00\x00\x00'
|
||||
TSHIRT=b'\x10\x01\xe8\x02\x04\x04\x02\x08\x0c\x06\x08\x02\x08\x02\x08\x02\x08\x02\x08\x02\xf8\x03\x00\x00'
|
||||
ROLLERSKATE=b'\x00\x00\x3e\x00\x22\x00\x22\x00\xe2\x03\x02\x04\x02\x04\xfe\x07\x05\x0a\x07\x0e\x00\x00\x00\x00'
|
||||
DUCK=b'\x00\x00\x00\x00\x38\x00\x24\x00\x42\x00\x4f\x00\xc8\x0f\x08\x04\x08\x02\xf8\x01\x00\x00\x00\x00'
|
||||
HOUSE=b'\x00\x00\xfc\x03\x06\x06\x03\x0c\xf4\x02\x94\x02\x94\x02\xb4\x02\x94\x02\x94\x02\x00\x00\x00\x00'
|
||||
TORTOISE=b'\x00\x00\x60\x00\x68\x01\xf8\x01\xf8\x01\xf8\x01\xf8\x01\xf8\x01\xf8\x01\xf0\x00\x08\x01\x00\x00'
|
||||
BUTTERFLY=b'\x00\x00\x06\x03\x89\x04\x51\x04\xfe\x03\x70\x00\xa8\x00\x74\x01\x8a\x02\x06\x03\x00\x00\x00\x00'
|
||||
STICKFIGURE=b'\x20\x00\x50\x00\x50\x00\x20\x00\x20\x00\x70\x00\xa8\x00\x24\x01\x50\x00\x88\x00\x04\x01\x00\x00'
|
||||
GHOST=b'\x00\x00\xf8\x00\xac\x01\xac\x01\xac\x01\xfc\x01\xac\x01\xac\x01\x54\x01\xfc\x03\xfc\x07\x00\x00'
|
||||
PITCHFORK=b'\x00\x00\x00\x00\x00\x0f\x80\x00\x80\x00\xff\x0f\x80\x00\x80\x00\x00\x0f\x00\x00\x00\x00\x00\x00'
|
||||
MUSIC_QUAVERS=b'\x00\x00\x00\x00\x00\x00\x00\x00\x8c\x01\xde\x03\x79\x0f\x30\x06\x00\x00\x00\x00\x00\x00\x00\x00'
|
||||
MUSIC_QUAVER=b'\xc0\x00\x40\x01\x40\x01\x40\x01\x40\x01\x40\x00\x40\x00\x58\x00\x7c\x00\x7e\x00\x3e\x00\x1c\x00'
|
||||
MUSIC_CROTCHET=b'\x40\x00\x40\x00\x40\x00\x40\x00\x40\x00\x40\x00\x40\x00\x58\x00\x7c\x00\x7e\x00\x3e\x00\x1c\x00'
|
||||
COW=b'\x00\x00\x00\x00\x08\x00\x1e\x00\xfa\x03\xfe\x07\xfe\x0b\x18\x0b\x18\x03\x18\x03\x18\x03\x00\x00'
|
||||
RABBIT=b'\x50\x00\xa8\x00\xa8\x00\xa8\x00\xa8\x00\x8c\x01\x04\x01\x54\x01\x04\x01\x24\x01\x74\x01\xf8\x00'
|
||||
SQUARE_SMALL=b'\x00\x00\x00\x00\x00\x00\x00\x00\xf0\x00\x90\x00\x90\x00\xf0\x00\x00\x00\x00\x00\x00\x00\x00\x00'
|
||||
SQUARE=b'\xff\x0f\x01\x08\x01\x08\x01\x08\x01\x08\x01\x08\x01\x08\x01\x08\x01\x08\x01\x08\x01\x08\xff\x0f'
|
||||
DIAMOND_SMALL=b'\x00\x00\x00\x00\x00\x00\x00\x00\x70\x00\xa8\x00\x50\x00\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00'
|
||||
DIAMOND=b'\x00\x00\x00\x00\xfc\x01\x56\x03\xfb\x06\x76\x03\xac\x01\xd8\x00\x70\x00\x20\x00\x00\x00\x00\x00'
|
||||
CHESSBOARD=b'\x00\x00\x00\x00\xff\x07\x55\x05\xff\x07\x55\x05\xff\x07\x55\x05\xff\x07\x00\x00\x00\x00\x00\x00'
|
||||
TRIANGLE_LEFT=b'\x00\x00\x00\x00\x80\x00\xc0\x00\xe0\x00\xf0\x00\xe0\x00\xc0\x00\x80\x00\x00\x00\x00\x00\x00\x00'
|
||||
TRIANGLE=b'\x00\x00\x00\x00\x00\x00\x60\x00\xf0\x00\xf8\x01\xfc\x03\xfe\x07\xff\x0f\x00\x00\x00\x00\x00\x00'
|
||||
SNAKE=b'\x00\x00\x00\x00\x00\x00\x00\x0e\x00\x0a\x00\x0e\xf0\x03\xf8\x03\x1c\x00\x0e\x00\x00\x00\x00\x00'
|
||||
UMBRELLA=b'\x00\x00\x00\x00\x20\x00\xf8\x00\xfc\x01\xfe\x03\x20\x00\x20\x00\x20\x00\xa0\x00\x40\x00\x00\x00'
|
||||
SKULL=b'\x00\x00\x00\x00\xf8\x00\x24\x01\x22\x02\xde\x03\xfc\x01\x88\x00\x88\x00\xa8\x00\x70\x00\x00\x00'
|
||||
GIRAFFE=b'\xc0\x00\xc0\x01\x40\x00\x40\x00\x40\x00\x40\x00\x40\x00\x40\x00\x78\x00\x48\x00\x48\x00\x48\x00'
|
||||
SWORD=b'\x00\x00\x00\x00\x00\x00\x04\x00\x0c\x00\xff\x07\xff\x07\x0c\x00\x04\x00\x00\x00\x00\x00\x00\x00'
|
||||
|
||||
136
boards/default/micropython_robot/build/lib/feiyi.py
Normal file
136
boards/default/micropython_robot/build/lib/feiyi.py
Normal file
@@ -0,0 +1,136 @@
|
||||
"""
|
||||
MixGo CC -Onboard resources
|
||||
|
||||
MicroPython library for the MixGo CC -Onboard resources
|
||||
=======================================================
|
||||
|
||||
#Preliminary composition 20221010
|
||||
|
||||
dahanzimin From the Mixly Team
|
||||
"""
|
||||
|
||||
import time,gc
|
||||
from machine import Pin,SoftI2C,ADC,PWM,Timer,RTC
|
||||
|
||||
'''i2c-onboard'''
|
||||
onboard_i2c=SoftI2C(scl = Pin(6), sda = Pin(7), freq = 400000)
|
||||
|
||||
'''RTC'''
|
||||
rtc_clock=RTC()
|
||||
|
||||
'''ACC-Sensor'''
|
||||
try :
|
||||
import mxc6655xa
|
||||
onboard_acc = mxc6655xa.MXC6655XA(onboard_i2c)
|
||||
except Exception as e:
|
||||
print("Warning: Failed to communicate with MXC6655XA (ACC) or",e)
|
||||
|
||||
'''ALS_PS-Sensor'''
|
||||
try :
|
||||
import ltr553als
|
||||
onboard_als = ltr553als.LTR_553ALS(onboard_i2c)
|
||||
except Exception as e:
|
||||
print("Warning: Failed to communicate with TR_553ALS (ALS&PS) or",e)
|
||||
|
||||
#PS The previous version cancelled this feature
|
||||
# '''BPS-Sensor'''
|
||||
# try :
|
||||
# import hp203x
|
||||
# onboard_hp203x = hp203x.HP203X(onboard_i2c)
|
||||
# except Exception as e:
|
||||
# print("Warning: Failed to communicate with HP203X (BPS) or",e)
|
||||
|
||||
# '''THS-Sensor'''
|
||||
# try :
|
||||
# import ahtx0
|
||||
# onboard_ahtx0 = ahtx0.AHTx0(onboard_i2c)
|
||||
# except Exception as e:
|
||||
# print("Warning: Failed to communicate with AHTx0 (THS) or",e)
|
||||
|
||||
'''RFID-Sensor'''
|
||||
try :
|
||||
import rc522
|
||||
onboard_rfid = rc522.RC522(onboard_i2c)
|
||||
except Exception as e:
|
||||
print("Warning: Failed to communicate with RC522 (RFID) or",e)
|
||||
|
||||
'''ADC*7, Motor*2*2, Matrix12x12'''
|
||||
try :
|
||||
import bot51
|
||||
onboard_bot51 = bot51.BOT51(onboard_i2c)
|
||||
onboard_matrix = onboard_bot51
|
||||
except Exception as e:
|
||||
print("Warning: Failed to communicate with Bot51 (Matrix Motor ALS ADC..) or",e)
|
||||
|
||||
'''MGS-Sensor'''
|
||||
try :
|
||||
import mmc5603
|
||||
onboard_mgs = mmc5603.MMC5603(onboard_i2c)
|
||||
except Exception as e:
|
||||
print("Warning: Failed to communicate with MMC5603 (MGS) or",e)
|
||||
|
||||
'''2RGB_WS2812'''
|
||||
from ws2812 import NeoPixel
|
||||
onboard_rgb = NeoPixel(Pin(8), 4)
|
||||
|
||||
'''1Buzzer-Music'''
|
||||
from music import MIDI
|
||||
onboard_music =MIDI(10)
|
||||
|
||||
'''MIC_Sensor'''
|
||||
class MICSensor:
|
||||
def __init__(self,pin):
|
||||
self.adc=ADC(Pin(pin))
|
||||
self.adc.atten(ADC.ATTN_11DB)
|
||||
|
||||
def read(self):
|
||||
maxloudness = 0
|
||||
for i in range(5):
|
||||
loudness = self.sample()
|
||||
if loudness > maxloudness:
|
||||
maxloudness = loudness
|
||||
return maxloudness
|
||||
|
||||
def sample(self):
|
||||
values = []
|
||||
for i in range(50):
|
||||
val = self.adc.read_u16()
|
||||
values.append(val)
|
||||
return max(values) - min(values)
|
||||
|
||||
onboard_sound = MICSensor(4)
|
||||
|
||||
'''2Button'''
|
||||
class Button:
|
||||
def __init__(self, pin):
|
||||
self.pin = Pin(pin, Pin.IN, Pin.PULL_UP)
|
||||
self.flag = True
|
||||
|
||||
def get_presses(self, delay = 1):
|
||||
last_time,presses = time.time(), 0
|
||||
while time.time() < last_time + delay:
|
||||
time.sleep(0.05)
|
||||
if self.was_pressed():
|
||||
presses += 1
|
||||
return presses
|
||||
|
||||
def is_pressed(self):
|
||||
return not self.pin.value()
|
||||
|
||||
def was_pressed(self, flag = 0):
|
||||
if(self.pin.value() != self.flag):
|
||||
self.flag = self.pin.value()
|
||||
time.sleep(0.02)
|
||||
if self.flag:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
def irq(self, handler, trigger):
|
||||
self.pin.irq(handler = handler, trigger = trigger)
|
||||
|
||||
button_a = Button(5)
|
||||
button_b = Button(9)
|
||||
|
||||
'''Reclaim memory'''
|
||||
gc.collect()
|
||||
354
boards/default/micropython_robot/build/lib/mixbot.py
Normal file
354
boards/default/micropython_robot/build/lib/mixbot.py
Normal file
@@ -0,0 +1,354 @@
|
||||
"""
|
||||
Mixbot-Onboard resources
|
||||
|
||||
Micropython library for the Mixbot-Onboard resources
|
||||
=======================================================
|
||||
|
||||
@dahanzimin From the Mixly Team
|
||||
"""
|
||||
|
||||
import time, gc, random, uframebuf
|
||||
from micropython import const
|
||||
from machine import Pin, SoftI2C, ADC, PWM, RTC
|
||||
|
||||
'''RTC'''
|
||||
rtc_clock=RTC()
|
||||
|
||||
'''2RGB_WS2812'''
|
||||
from ws2812 import NeoPixel
|
||||
onboard_rgb = NeoPixel(Pin(12), 2, default=1)
|
||||
|
||||
'''1Buzzer-Music'''
|
||||
from music import MIDI
|
||||
onboard_music =MIDI(25)
|
||||
|
||||
spk_en = Pin(27, Pin.OUT)
|
||||
spk_en.value(0)
|
||||
|
||||
'''i2c-onboard & ext'''
|
||||
class I2C_device(SoftI2C):
|
||||
|
||||
CRC8_Table =b'\x00^\xbc\xe2a?\xdd\x83\xc2\x9c~ \xa3\xfd\x1fA\x9d\xc3!\x7f\xfc\xa2@\x1e_\x01\xe3\xbd>`\x82\xdc#}\x9f\xc1B\x1c\xfe\xa0\xe1\xbf]\x03\x80\xde<b\xbe\xe0\x02\\\xdf\x81c=|"\xc0\x9e\x1dC\xa1\xffF\x18\xfa\xa4\'y\x9b\xc5\x84\xda8f\xe5\xbbY\x07\xdb\x85g9\xba\xe4\x06X\x19G\xa5\xfbx&\xc4\x9ae;\xd9\x87\x04Z\xb8\xe6\xa7\xf9\x1bE\xc6\x98z$\xf8\xa6D\x1a\x99\xc7%{:d\x86\xd8[\x05\xe7\xb9\x8c\xd20n\xed\xb3Q\x0fN\x10\xf2\xac/q\x93\xcd\x11O\xad\xf3p.\xcc\x92\xd3\x8do1\xb2\xec\x0eP\xaf\xf1\x13M\xce\x90r,m3\xd1\x8f\x0cR\xb0\xee2l\x8e\xd0S\r\xef\xb1\xf0\xaeL\x12\x91\xcf-s\xca\x94v(\xab\xf5\x17I\x08V\xb4\xeai7\xd5\x8bW\t\xeb\xb56h\x8a\xd4\x95\xcb)w\xf4\xaaH\x16\xe9\xb7U\x0b\x88\xd64j+u\x97\xc9J\x14\xf6\xa8t*\xc8\x96\x15K\xa9\xf7\xb6\xe8\nT\xd7\x89k5'
|
||||
|
||||
def _crc8(self, buf):
|
||||
_sum = 0
|
||||
for i in range(0, len(buf)):
|
||||
_sum = self.CRC8_Table[_sum ^ buf[i]]
|
||||
return _sum
|
||||
|
||||
def read_device(self, addr, cmd, nbytes=1):
|
||||
buf = self.readfrom_mem(addr, cmd, nbytes+2)
|
||||
if self._crc8(buf[:-1]) == buf[-1]:
|
||||
return buf[1] if nbytes<=1 else buf[1:-1]
|
||||
|
||||
def write_device(self, addr, cmd, buf=0):
|
||||
buf = buf.to_bytes(1, 'little') if type(buf) is int else buf
|
||||
buf = bytearray([cmd, random.randint(0, 255)]) + buf
|
||||
crc8 = self._crc8(buf).to_bytes(1, 'little')
|
||||
self.writeto(addr, buf + crc8)
|
||||
if crc8 == self.readfrom(addr, 1):
|
||||
return True
|
||||
|
||||
onboard_i2c = I2C_device(scl=Pin(18), sda=Pin(23) , freq=400000)
|
||||
ext_i2c = I2C_device(scl=Pin(22), sda=Pin(21), freq=200000)
|
||||
|
||||
'''Version judgment'''
|
||||
if 0x68 in onboard_i2c.scan():
|
||||
version=1
|
||||
else:
|
||||
version=0
|
||||
|
||||
'''Accelerometer+Gyroscope'''
|
||||
if version:
|
||||
import icm42670
|
||||
acc_gyr = icm42670.ICM42670(onboard_i2c)
|
||||
|
||||
'''2-Button'''
|
||||
class Button:
|
||||
def __init__(self, pin, level=True):
|
||||
self._pin = Pin(pin, Pin.IN)
|
||||
self._flag = True
|
||||
self._level = level
|
||||
|
||||
def get_presses(self, delay = 1):
|
||||
last_time,presses = time.time(), 0
|
||||
while time.time() < last_time + delay:
|
||||
time.sleep(0.05)
|
||||
if self.was_pressed():
|
||||
presses += 1
|
||||
return presses
|
||||
|
||||
def is_pressed(self):
|
||||
return self._pin.value() == self._level
|
||||
|
||||
def was_pressed(self):
|
||||
if self._pin.value() != self._flag:
|
||||
time.sleep(0.01)
|
||||
self._flag = self._pin.value()
|
||||
return self._level if self._flag else not self._level
|
||||
|
||||
def irq(self, handler, trigger):
|
||||
self._pin.irq(handler = handler, trigger = trigger)
|
||||
|
||||
button_p = Button(34, True)
|
||||
button_a = Button(35, False)
|
||||
button_b = Button(39, False)
|
||||
|
||||
'''2-LED'''
|
||||
class LED:
|
||||
def __init__(self, pin):
|
||||
self._pin =PWM(Pin(pin), freq=5000, duty_u16=65535)
|
||||
self.setbrightness(0)
|
||||
|
||||
def setbrightness(self,val):
|
||||
if not 0 <= val <= 100:
|
||||
raise ValueError("Brightness must be in the range: 0-100%")
|
||||
self._brightness=val
|
||||
self._pin.duty_u16(val * 65535 // 100)
|
||||
|
||||
def getbrightness(self):
|
||||
return self._brightness
|
||||
|
||||
def setonoff(self,val):
|
||||
if(val == -1):
|
||||
self.setbrightness(100) if self._brightness < 50 else self.setbrightness(0)
|
||||
elif(val == 1):
|
||||
self.setbrightness(100)
|
||||
elif(val == 0):
|
||||
self.setbrightness(0)
|
||||
|
||||
def getonoff(self):
|
||||
return True if self._brightness > 0 else False
|
||||
|
||||
def value(self,val=None):
|
||||
if val is None:
|
||||
return self.getonoff()
|
||||
else:
|
||||
self.setbrightness(100) if val else self.setbrightness(0)
|
||||
|
||||
rled = LED(2)
|
||||
gled = LED(4)
|
||||
|
||||
'''3-ADCSensor'''
|
||||
class ADCSensor:
|
||||
def __init__(self, pin):
|
||||
self.adc=ADC(Pin(pin))
|
||||
self.adc.atten(ADC.ATTN_11DB)
|
||||
|
||||
def read(self):
|
||||
return self.adc.read_u16()
|
||||
|
||||
def voltage(self, scale=4.6):
|
||||
return round(self.adc.read_uv() * scale / 1000000, 2)
|
||||
|
||||
def loudness(self):
|
||||
values = []
|
||||
for i in range(200):
|
||||
val = self.adc.read_u16()
|
||||
values.append(val)
|
||||
return max(values) - min(values)
|
||||
|
||||
adc1 = ADCSensor(33)
|
||||
adc2 = ADCSensor(32)
|
||||
battery = ADCSensor(36)
|
||||
sound = ADCSensor(38)
|
||||
|
||||
'''4-FindLine /i2c'''
|
||||
class FindLine(object):
|
||||
|
||||
CORRECTING_WHITE = const(0x01)
|
||||
CORRECTING_BLACK = const(0x02)
|
||||
CORRECTING_RESET_TO_FAB = const(0x04)
|
||||
|
||||
def __init__(self, i2c_bus, addr=0x01):
|
||||
self._i2c = i2c_bus
|
||||
self._addr = addr
|
||||
self._data = (0,0,0,0)
|
||||
|
||||
def getdata(self):
|
||||
_buf = self._i2c.read_device(self._addr, 0x10, 4)
|
||||
if _buf:
|
||||
self._data = tuple(_buf)
|
||||
return self._data
|
||||
|
||||
def correct(self,type):
|
||||
'''type 0x01 校正白色 0x02 校正黑色 0x04 恢复出厂 '''
|
||||
if type not in [CORRECTING_WHITE, CORRECTING_BLACK, CORRECTING_RESET_TO_FAB]:
|
||||
raise ValueError('Invalid parameter')
|
||||
self._i2c.write_device(self._addr, 0xA0, type)
|
||||
|
||||
patrol = FindLine(onboard_i2c)
|
||||
|
||||
'''4-LEDmatrix /i2c'''
|
||||
class Matrix5x5(uframebuf.FrameBuffer_Ascall):
|
||||
|
||||
"""Graph module 5x5"""
|
||||
HEART = b'\n\x1f\x1f\x0e\x04'
|
||||
HEART_SMALL = b'\x00\n\x0e\x04\x00'
|
||||
HAPPY = b'\x00\n\x00\x11\x0e'
|
||||
SAD = b'\x00\n\x00\x0e\x11'
|
||||
SMILE = b'\x00\x00\x00\x11\x0e'
|
||||
SILLY = b'\x11\x00\x1f\x14\x1c'
|
||||
FABULOUS = b'\x1f\x1b\x00\n\x0e'
|
||||
SURPRISED = b'\n\x00\x04\n\x04'
|
||||
ASLEEP = b'\x00\x1b\x00\x0e\x00'
|
||||
ANGRY = b'\x11\n\x00\x1f\x15'
|
||||
CONFUSED = b'\x00\n\x00\n\x15'
|
||||
NO = b'\x11\n\x04\n\x11'
|
||||
YES = b'\x00\x10\x08\x05\x02'
|
||||
LEFT_ARROW = b'\x04\x02\x1f\x02\x04'
|
||||
RIGHT_ARROW = b'\x04\x08\x1f\x08\x04'
|
||||
DRESS = b'\n\x1b\x0e\x0e\x1f'
|
||||
TRANSFORMERS = b'\x04\x0e\x15\n\x11'
|
||||
SCISSORS = b'\x13\x0b\x04\x0b\x13'
|
||||
EXIT = b'\x04\x0e\x15\x0b\x10'
|
||||
TREE = b'\x04\x0e\x1f\x04\x04'
|
||||
PACMAN = b'\x1e\x0b\x07\x0f\x1e'
|
||||
TARGET = b'\x04\x0e\x1b\x0e\x04'
|
||||
TSHIRT = b'\x1b\x1f\x0e\x0e\x0e'
|
||||
ROLLERSKATE = b'\x18\x18\x1f\x1f\n'
|
||||
DUCK = b'\x06\x07\x1e\x0e\x00'
|
||||
HOUSE = b'\x04\x0e\x1f\x0e\n'
|
||||
TORTOISE = b'\x00\x0e\x1f\n\x00'
|
||||
BUTTERFLY = b'\x1b\x1f\x04\x1f\x1b'
|
||||
STICKFIGURE = b'\x04\x1f\x04\n\x11'
|
||||
GHOST = b'\x1f\x15\x1f\x1f\x15'
|
||||
PITCHFORK = b'\x15\x15\x1f\x04\x04'
|
||||
MUSIC_QUAVERS = b'\x1e\x12\x12\x1b\x1b'
|
||||
MUSIC_QUAVER = b'\x04\x0c\x14\x07\x07'
|
||||
MUSIC_CROTCHET = b'\x04\x04\x04\x07\x07'
|
||||
COW = b'\x11\x11\x1f\x0e\x04'
|
||||
RABBIT = b'\x05\x05\x0f\x0b\x0f'
|
||||
SQUARE_SMALL = b'\x00\x0e\n\x0e\x00'
|
||||
SQUARE = b'\x1f\x11\x11\x11\x1f'
|
||||
DIAMOND_SMALL = b'\x00\x04\n\x04\x00'
|
||||
DIAMOND = b'\x04\n\x11\n\x04'
|
||||
CHESSBOARD = b'\n\x15\n\x15\n'
|
||||
TRIANGLE_LEFT = b'\x01\x03\x05\t\x1f'
|
||||
TRIANGLE = b'\x00\x04\n\x1f\x00'
|
||||
SNAKE = b'\x03\x1b\n\x0e\x00'
|
||||
UMBRELLA = b'\x0e\x1f\x04\x05\x06'
|
||||
SKULL = b'\x0e\x15\x1f\x0e\x0e'
|
||||
GIRAFFE = b'\x03\x02\x02\x0e\n'
|
||||
SWORD = b'\x04\x04\x04\x0e\x04'
|
||||
|
||||
|
||||
def __init__(self, i2c_bus, addr=0x03, brightness=0.5):
|
||||
self._i2c = i2c_bus
|
||||
self._addr = addr
|
||||
self._brightness= brightness
|
||||
self._buffer = bytearray(5)
|
||||
super().__init__(self._buffer, 5, 5, uframebuf.MONO_HMSB)
|
||||
self.font("5x5")
|
||||
self.screenbright(self._brightness)
|
||||
self.clear()
|
||||
|
||||
def screenbright(self, brightness=None, background=0):
|
||||
if brightness is None :
|
||||
return self._brightness
|
||||
else:
|
||||
if not 0.0 <= brightness <= 1.0:
|
||||
raise ValueError("Brightness must be a decimal number in the range: 0.0-1.0")
|
||||
self._brightness = brightness
|
||||
self._i2c.write_device(self._addr, 0xA5, bytes([round(255 * brightness), round(255 * background)]))
|
||||
|
||||
def ambientbright(self):
|
||||
bright = self._i2c.read_device(self._addr, 0x10)
|
||||
if bright:
|
||||
return bright
|
||||
|
||||
def direction(self,mode = 0):
|
||||
'''set display direction '''
|
||||
self._i2c.write_device(self._addr, 0xA7, mode)
|
||||
|
||||
def show(self):
|
||||
'''Refresh the display and show the changes'''
|
||||
buf = bytearray(4)
|
||||
buf[0] = (self._buffer[4] & 0xF0) >> 4
|
||||
buf[1] = (self._buffer[3] & 0x1E) >> 1 | (self._buffer[4] & 0x0F) << 4
|
||||
buf[2] = (self._buffer[1] & 0x18) >> 3 | (self._buffer[2] & 0x1F) << 2 | (self._buffer[3] & 0x01) << 7
|
||||
buf[3] = (self._buffer[0] & 0x1F) | (self._buffer[1] & 0x07) << 5
|
||||
self._i2c.write_device(self._addr, 0xA1, buf)
|
||||
|
||||
def clear(self):
|
||||
''' clear display'''
|
||||
self._i2c.write_device(self._addr, 0xA6)
|
||||
|
||||
onboard_matrix = Matrix5x5(onboard_i2c)
|
||||
|
||||
'''2 Motor /i2c'''
|
||||
class Motor(object):
|
||||
|
||||
STOP_MODE = const(0x00)
|
||||
BRAKE_MODE = const(0x01)
|
||||
PWR_MODE = const(0x02)
|
||||
SPEED_MODE = const(0x03)
|
||||
TURNS_MODE = const(0x04)
|
||||
|
||||
def __init__(self, i2c_bus, addr=0x02, scale=90 * 4):
|
||||
self._i2c = i2c_bus
|
||||
self._addr = addr
|
||||
self._scale = scale
|
||||
self._signala = PWM(Pin(13), freq=500, duty_u16=49150)
|
||||
self._signalb = PWM(Pin(14), freq=500, duty_u16=49150)
|
||||
self._status = ((0,0,0,0), (0,0,0,0))
|
||||
self._motor = ([0,0], [0,0])
|
||||
|
||||
def _u2s(self, value, n=8):
|
||||
return value if value < (1 << (n-1)) else value - (1 << n)
|
||||
|
||||
def status(self):
|
||||
_buf = self._i2c.read_device(self._addr, 0x10, 9)
|
||||
if _buf:
|
||||
self._status = ((_buf[0] >> 4, -self._u2s(_buf[1]), -self._u2s(_buf[3]), abs(self._u2s(_buf[6] << 8 | _buf[5], 16))),
|
||||
(_buf[0] & 0x0F, self._u2s(_buf[2]), self._u2s(_buf[4]), abs(self._u2s(_buf[8] << 8 | _buf[7], 16))))
|
||||
return self._status
|
||||
|
||||
def run(self, idx, mode, value):
|
||||
if idx == 1 or idx == 2:
|
||||
self._motor[idx-1][0], self._motor[idx-1][1] = mode, value
|
||||
else:
|
||||
self._motor = ([mode, value], [mode, value])
|
||||
|
||||
buf = bytearray(5)
|
||||
m1_pwr_speed, m2_pwr_speed = 0, 0
|
||||
buf[0] = (self._motor[0][0] << 4) | self._motor[1][0]
|
||||
if self._motor[0][0] == self.TURNS_MODE:
|
||||
_turns = round(self._motor[0][1] * self._scale)
|
||||
buf[1] = (- _turns) & 0xFF
|
||||
buf[2] = ((- _turns) >> 8) & 0xFF
|
||||
else:
|
||||
m1_pwr_speed = - max(min(self._motor[0][1], 100), -100)
|
||||
if self._motor[1][0] == self.TURNS_MODE:
|
||||
_turns = round(self._motor[1][1] * self._scale)
|
||||
buf[3] = _turns & 0xFF
|
||||
buf[4] = (_turns >> 8) & 0xFF
|
||||
else:
|
||||
m2_pwr_speed = max(min(self._motor[1][1], 100), -100)
|
||||
|
||||
self._i2c.write_device(self._addr, 0xA0, buf)
|
||||
self._signala.duty_u16(33422 + 31457 * (m1_pwr_speed + 100) // 200)
|
||||
self._signalb.duty_u16(33422 + 31457 * (m2_pwr_speed + 100) // 200)
|
||||
|
||||
def move(self, action, mode, value=100):
|
||||
if action=="N":
|
||||
self.run(0, self.STOP_MODE, 0)
|
||||
elif action=="P":
|
||||
self.run(0, self.BRAKE_MODE, 0)
|
||||
elif action=="F":
|
||||
self.run(0, mode, value)
|
||||
elif action=="B":
|
||||
self.run(0, mode, -value)
|
||||
elif action=="L":
|
||||
self.run(1, mode, -value)
|
||||
self.run(2, mode, value)
|
||||
elif action=="R":
|
||||
self.run(1, mode, value)
|
||||
self.run(2, mode, -value)
|
||||
else:
|
||||
raise ValueError('Invalid input, valid are "N","P","F","B","L","R"')
|
||||
|
||||
motor = Motor(onboard_i2c)
|
||||
|
||||
'''Reclaim memory'''
|
||||
gc.collect()
|
||||
44
boards/default/micropython_robot/build/lib/mixbot_ext.py
Normal file
44
boards/default/micropython_robot/build/lib/mixbot_ext.py
Normal file
@@ -0,0 +1,44 @@
|
||||
"""
|
||||
Mixbot-EXT
|
||||
|
||||
Micropython library for the Mixbot-External integrated I2C equipment
|
||||
=======================================================
|
||||
|
||||
@dahanzimin From the Mixly Team
|
||||
"""
|
||||
import i2cdevice
|
||||
from mixbot import ext_i2c
|
||||
|
||||
'''Motor'''
|
||||
ext_motor = i2cdevice.Motor(ext_i2c)
|
||||
|
||||
'''Traffic light'''
|
||||
ext_traffic = i2cdevice.Traffic_LED(ext_i2c)
|
||||
|
||||
'''LED /RGBYW'''
|
||||
R_LED = i2cdevice.R_LED(ext_i2c)
|
||||
G_LED = i2cdevice.G_LED(ext_i2c)
|
||||
B_LED = i2cdevice.B_LED(ext_i2c)
|
||||
Y_LED = i2cdevice.Y_LED(ext_i2c)
|
||||
W_LED = i2cdevice.W_LED(ext_i2c)
|
||||
|
||||
'''button*5'''
|
||||
ext_button = i2cdevice.Buttonx5(ext_i2c)
|
||||
|
||||
'''collision sensor'''
|
||||
ext_collision = i2cdevice.Button(ext_i2c)
|
||||
|
||||
'''Infrared sensor'''
|
||||
ext_infrared = i2cdevice.Infrared(ext_i2c)
|
||||
|
||||
'''Potentiometer'''
|
||||
ext_potentiometer = i2cdevice.Dimmer(ext_i2c)
|
||||
|
||||
'''Color sensor'''
|
||||
ext_color = i2cdevice.Color_ID(ext_i2c)
|
||||
|
||||
'''Servo Motor'''
|
||||
ext_servo = i2cdevice.Motor_servo(ext_i2c)
|
||||
|
||||
'''Sonar'''
|
||||
ext_sonar = i2cdevice.Sonar(ext_i2c)
|
||||
174
boards/default/micropython_robot/build/lib/rm_e1.py
Normal file
174
boards/default/micropython_robot/build/lib/rm_e1.py
Normal file
@@ -0,0 +1,174 @@
|
||||
"""
|
||||
RM E1 -Onboard resources
|
||||
|
||||
MicroPython library for the RM E1 -Onboard resources
|
||||
=======================================================
|
||||
|
||||
#Preliminary composition 20220703
|
||||
|
||||
dahanzimin From the Mixly Team
|
||||
"""
|
||||
|
||||
import time,gc
|
||||
#import ble_handle
|
||||
from machine import Pin,SoftI2C,ADC,PWM,RTC
|
||||
|
||||
'''Bluetooth-handle'''
|
||||
#handle=ble_handle.Handle()
|
||||
|
||||
'''i2c-onboard'''
|
||||
onboard_i2c=SoftI2C(scl = Pin(22), sda = Pin(21), freq = 400000)
|
||||
|
||||
'''RTC'''
|
||||
rtc_clock=RTC()
|
||||
|
||||
'''ACC-Sensor'''
|
||||
class ACC:
|
||||
def __init__(self,i2c_bus):
|
||||
self._device = i2c_bus
|
||||
self._address = 0x09
|
||||
|
||||
def _rreg(self,nbytes):
|
||||
'''Read memory address'''
|
||||
return self._device.readfrom(self._address, nbytes)
|
||||
|
||||
def acceleration(self):
|
||||
data_reg=self._rreg(3)
|
||||
return data_reg[0],data_reg[1],data_reg[2] #返回x y轴数值(0~180)及晃动值
|
||||
|
||||
try :
|
||||
gyro=ACC(onboard_i2c)
|
||||
except Exception as e:
|
||||
print("Warning: Failed to communicate with ACC or",e)
|
||||
|
||||
'''2RGB_WS2812''' #color_chase(),rainbow_cycle()方法移至类里
|
||||
from ws2812 import NeoPixel
|
||||
onboard_rgb = NeoPixel(Pin(12), 2, default=1)
|
||||
|
||||
'''3-Button'''
|
||||
class Button:
|
||||
def __init__(self, pin):
|
||||
self._pin = Pin(pin, Pin.IN)
|
||||
self._flag = True
|
||||
|
||||
def get_presses(self, delay = 1):
|
||||
last_time,presses = time.time(), 0
|
||||
while time.time() < last_time + delay:
|
||||
time.sleep(0.05)
|
||||
if self.was_pressed():
|
||||
presses += 1
|
||||
return presses
|
||||
|
||||
def is_pressed(self):
|
||||
return self._pin.value() == False
|
||||
|
||||
def was_pressed(self):
|
||||
if self._pin.value() != self._flag:
|
||||
time.sleep(0.01)
|
||||
self._flag = self._pin.value()
|
||||
if self._flag:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
def irq(self, handler, trigger):
|
||||
self._pin.irq(handler = handler, trigger = trigger)
|
||||
|
||||
button_p = Button(35)
|
||||
button_cw = Button(39)
|
||||
button_ccw = Button(36)
|
||||
|
||||
'''3-ADCSensor'''
|
||||
class ADCSensor:
|
||||
def __init__(self, pin):
|
||||
self._adc=ADC(Pin(pin))
|
||||
self._adc.atten(ADC.ATTN_11DB)
|
||||
|
||||
def read(self):
|
||||
return self._adc.read_u16()
|
||||
|
||||
def voltage(self):
|
||||
return round(self._adc.read_uv()*4.6/1000000,2)
|
||||
|
||||
adc1=ADCSensor(32)
|
||||
adc2=ADCSensor(33)
|
||||
|
||||
'''ADC conflicts with WiFi'''
|
||||
try:
|
||||
battery=ADCSensor(26)
|
||||
except:
|
||||
class Clash:
|
||||
def voltage(self):
|
||||
print("Warning: battery power collection conflicts with WiFi")
|
||||
return None
|
||||
battery=Clash()
|
||||
|
||||
'''2-LED''' #Repair brightness adjustment range 0-100%
|
||||
class LED:
|
||||
def __init__(self, pin):
|
||||
self._pin =PWM(Pin(pin),freq=5000,duty_u16=0)
|
||||
self.setbrightness(0)
|
||||
|
||||
def value(self, val):
|
||||
self.setonoff(val)
|
||||
|
||||
def setbrightness(self,val):
|
||||
if not 0 <= val <= 100:
|
||||
raise ValueError("Brightness must be in the range: 0-100%")
|
||||
self._brightness=val
|
||||
self._pin.duty_u16(val*65535//100)
|
||||
|
||||
def getbrightness(self):
|
||||
return self._brightness
|
||||
|
||||
def setonoff(self,val):
|
||||
if(val == -1):
|
||||
self.setbrightness(100) if self._brightness<50 else self.setbrightness(0)
|
||||
elif(val == 1):
|
||||
self.setbrightness(100)
|
||||
elif(val == 0):
|
||||
self.setbrightness(0)
|
||||
|
||||
def getonoff(self):
|
||||
return True if self._brightness>0 else False
|
||||
|
||||
rled = LED(2)
|
||||
gled = LED(4)
|
||||
|
||||
'''3-Motor'''
|
||||
class Motor:
|
||||
def __init__(self, apin,bpin):
|
||||
self._apin =PWM(Pin(apin),freq=5000,duty_u16=65535)
|
||||
self._bpin =PWM(Pin(bpin),freq=5000,duty_u16=65535)
|
||||
self.motion("P")
|
||||
|
||||
def motion(self,action,speed=0):
|
||||
if action=="N":
|
||||
self._apin.duty_u16(0)
|
||||
self._bpin.duty_u16(0)
|
||||
elif action=="P":
|
||||
self._apin.duty_u16(65535)
|
||||
self._bpin.duty_u16(65535)
|
||||
elif action=="CW":
|
||||
if speed >=0:
|
||||
self._apin.duty_u16(speed*65535//100)
|
||||
self._bpin.duty_u16(0)
|
||||
else:
|
||||
self._apin.duty_u16(0)
|
||||
self._bpin.duty_u16(-speed*65535//100)
|
||||
elif action=="CCW":
|
||||
if speed >=0:
|
||||
self._apin.duty_u16(0)
|
||||
self._bpin.duty_u16(speed*65535//100)
|
||||
else:
|
||||
self._apin.duty_u16(-speed*65535//100)
|
||||
self._bpin.duty_u16(0)
|
||||
else:
|
||||
raise ValueError('Invalid input, valid are "N","P","CW","CCW"')
|
||||
|
||||
motor1=Motor(23,27)
|
||||
motor2=Motor(18,19)
|
||||
motor3=Motor(13,14)
|
||||
|
||||
'''Reclaim memory'''
|
||||
gc.collect()
|
||||
Reference in New Issue
Block a user