初始化提交
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
250
boards/default_src/micropython_esp32s2/origin/build/lib/ce_go.py
Normal file
250
boards/default_src/micropython_esp32s2/origin/build/lib/ce_go.py
Normal file
@@ -0,0 +1,250 @@
|
||||
"""
|
||||
CE GO -Onboard resources
|
||||
|
||||
MicroPython library for the CE GO (Smart Car base for MixGo CE)
|
||||
MicroPython library for the CE GO (Smart Car base for MixGo CE)
|
||||
=======================================================
|
||||
|
||||
#Preliminary composition 20220830
|
||||
|
||||
dahanzimin From the Mixly Team
|
||||
"""
|
||||
|
||||
import time, gc, math
|
||||
from tm1931 import TM1931
|
||||
from machine import Pin, SoftI2C, ADC
|
||||
|
||||
'''i2c-onboard'''
|
||||
i2c = SoftI2C(scl = Pin(4, pull=Pin.PULL_UP), sda = Pin(5, pull=Pin.PULL_UP), freq = 400000)
|
||||
i2c_scan = i2c.scan()
|
||||
|
||||
'''Version judgment'''
|
||||
if 0x50 in i2c_scan:
|
||||
version = 1
|
||||
else:
|
||||
version = 0
|
||||
|
||||
'''Judging the type of external motor'''
|
||||
Mi2c = 0
|
||||
for addr in i2c_scan:
|
||||
if addr in [0x30, 0x31, 0x32, 0x33]:
|
||||
Mi2c = addr
|
||||
break
|
||||
|
||||
'''i2c-motor'''
|
||||
def i2c_motor(speed):
|
||||
i2c.writeto(Mi2c, b'\x00\x00' + speed.to_bytes(1, 'little') + b'\x00')
|
||||
|
||||
'''TM1931-Expand'''
|
||||
class CAR(TM1931):
|
||||
'''Infrared line patrol obstacle avoidance mode'''
|
||||
CL=0 #Turn off infrared to reduce power consumption
|
||||
OA=1 #Obstacle avoidance mode only
|
||||
LP=2 #Line patrol mode only
|
||||
LS=3 #Light seeking mode only
|
||||
AS=4 #Automatic mode switching
|
||||
|
||||
'''TM1931 port corresponding function definition'''
|
||||
OAOU=5 #obstacle avoidance
|
||||
LPOU=4 #Line patrol control
|
||||
LSOU=3 #Light control
|
||||
WLED=12 #Headlamp port
|
||||
GLED=[17,8,6,15] #Green LED port
|
||||
RLED=[16,7,9,18] #Red LED port
|
||||
UCOU=[1,2] #Typec external port
|
||||
MOTO=[[13,14],[10,11],[1,2]] #Motor port
|
||||
|
||||
def __init__(self, i2c_bus):
|
||||
super().__init__(i2c_bus)
|
||||
self._mode = self.CL
|
||||
self.atten = 0.82 if version else 1
|
||||
self.adc0 = ADC(Pin(9), atten=ADC.ATTN_11DB)
|
||||
self.adc1 = ADC(Pin(10), atten=ADC.ATTN_11DB)
|
||||
self.adc2 = ADC(Pin(1), atten=ADC.ATTN_11DB)
|
||||
self.adc3 = ADC(Pin(2), atten=ADC.ATTN_11DB)
|
||||
|
||||
def ir_mode(self, select=0):
|
||||
'''Infrared line patrol obstacle avoidance mode'''
|
||||
self._mode=select
|
||||
if select==self.CL:
|
||||
self.pwm(self.OAOU,0)
|
||||
self.pwm(self.LPOU,0)
|
||||
self.pwm(self.LSOU,0)
|
||||
if select==self.OA:
|
||||
self.pwm(self.OAOU,255)
|
||||
self.pwm(self.LPOU,0)
|
||||
self.pwm(self.LSOU,0)
|
||||
if select==self.LP:
|
||||
self.pwm(self.OAOU,0)
|
||||
self.pwm(self.LPOU,255)
|
||||
self.pwm(self.LSOU,0)
|
||||
if select==self.LS:
|
||||
self.pwm(self.OAOU,0)
|
||||
self.pwm(self.LPOU,0)
|
||||
self.pwm(self.LSOU,255)
|
||||
time.sleep_ms(2)
|
||||
|
||||
def obstacle(self):
|
||||
'''Read the obstacle avoidance sensor'''
|
||||
if self._mode==self.AS:
|
||||
self.pwm(self.OAOU,255)
|
||||
self.pwm(self.LPOU,0)
|
||||
self.pwm(self.LSOU,0)
|
||||
time.sleep_ms(2)
|
||||
if self._mode==self.OA or self._mode==self.AS :
|
||||
return self.adc2.read_u16(),self.adc1.read_u16(),self.adc0.read_u16(),self.adc3.read_u16()
|
||||
else:
|
||||
raise ValueError('Mode selection error, obstacle avoidance data cannot be read')
|
||||
|
||||
def patrol(self):
|
||||
'''Read the line patrol sensor'''
|
||||
if self._mode==self.AS:
|
||||
self.pwm(self.OAOU,0)
|
||||
self.pwm(self.LPOU,255)
|
||||
self.pwm(self.LSOU,0)
|
||||
time.sleep_ms(2)
|
||||
if self._mode==self.LP or self._mode==self.AS:
|
||||
return self.adc3.read_u16(),self.adc2.read_u16(),self.adc1.read_u16(),self.adc0.read_u16()
|
||||
else:
|
||||
raise ValueError('Mode selection error, line patrol data cannot be read')
|
||||
|
||||
def light(self):
|
||||
'''Read the light seeking sensor'''
|
||||
if self._mode==self.AS:
|
||||
self.pwm(self.OAOU,0)
|
||||
self.pwm(self.LPOU,0)
|
||||
self.pwm(self.LSOU,255)
|
||||
time.sleep_ms(2)
|
||||
if self._mode==self.LS or self._mode==self.AS:
|
||||
return self.adc3.read_u16(),self.adc2.read_u16(),self.adc1.read_u16(),self.adc0.read_u16()
|
||||
else:
|
||||
raise ValueError('Mode selection error, light seeking data cannot be read')
|
||||
|
||||
def motor(self, index, action, speed=0):
|
||||
speed = round(max(min(speed, 100), -100) * self.atten)
|
||||
if action=="N":
|
||||
if (index == [1, 2]) and Mi2c:
|
||||
i2c_motor(0)
|
||||
else:
|
||||
self.pwm(index[0], 255)
|
||||
self.pwm(index[1], 255)
|
||||
elif action=="P":
|
||||
if (index == [1, 2]) and Mi2c:
|
||||
i2c_motor(0)
|
||||
else:
|
||||
self.pwm(index[0], 0)
|
||||
self.pwm(index[1], 0)
|
||||
elif action=="CW":
|
||||
if (index == [1, 2]) and Mi2c:
|
||||
i2c_motor(speed)
|
||||
else:
|
||||
if speed >= 0:
|
||||
self.pwm(index[0], speed * 255 // 100)
|
||||
self.pwm(index[1], 0)
|
||||
else:
|
||||
self.pwm(index[0], 0)
|
||||
self.pwm(index[1], - speed * 255 // 100)
|
||||
elif action=="CCW":
|
||||
if (index == [1, 2]) and Mi2c:
|
||||
i2c_motor(- speed)
|
||||
else:
|
||||
if speed >= 0:
|
||||
self.pwm(index[0], 0)
|
||||
self.pwm(index[1], speed * 255 // 100)
|
||||
else:
|
||||
self.pwm(index[0], - speed * 255 // 100)
|
||||
self.pwm(index[1], 0)
|
||||
else:
|
||||
raise ValueError('Invalid input, valid are "N","P","CW","CCW"')
|
||||
|
||||
def move(self,action,speed=100):
|
||||
if action=="N":
|
||||
self.motor(self.MOTO[0],"N")
|
||||
self.motor(self.MOTO[1],"N")
|
||||
elif action=="P":
|
||||
self.motor(self.MOTO[0],"P")
|
||||
self.motor(self.MOTO[1],"P")
|
||||
elif action=="F":
|
||||
self.motor(self.MOTO[0],"CCW",speed)
|
||||
self.motor(self.MOTO[1],"CW",speed)
|
||||
elif action=="B":
|
||||
self.motor(self.MOTO[0],"CW",speed)
|
||||
self.motor(self.MOTO[1],"CCW",speed)
|
||||
elif action=="L":
|
||||
self.motor(self.MOTO[0],"CW",speed)
|
||||
self.motor(self.MOTO[1],"CW",speed)
|
||||
elif action=="R":
|
||||
self.motor(self.MOTO[0],"CCW",speed)
|
||||
self.motor(self.MOTO[1],"CCW",speed)
|
||||
else:
|
||||
raise ValueError('Invalid input, valid are "N","P","F","B","L","R"')
|
||||
|
||||
def setbrightness(self,index,val):
|
||||
if not 0 <= val <= 100:
|
||||
raise ValueError("Brightness must be in the range: 0-100%")
|
||||
self.pwm(index,val)
|
||||
|
||||
def getrightness(self,index):
|
||||
return self.duty(index)
|
||||
|
||||
def setonoff(self,index,val):
|
||||
if(val == -1):
|
||||
if self.getrightness(index) < 50:
|
||||
self.setbrightness(index,100)
|
||||
else:
|
||||
self.setbrightness(index,0)
|
||||
elif(val == 1):
|
||||
self.setbrightness(index,100)
|
||||
elif(val == 0):
|
||||
self.setbrightness(index,0)
|
||||
|
||||
def getonoff(self,index):
|
||||
return True if self.getrightness(index)>0 else False
|
||||
|
||||
try :
|
||||
car=CAR(i2c) #Including LED,motor,patrol,obstacle
|
||||
except Exception as e:
|
||||
print("Warning: Failed to communicate with TM1931 (ME GO CAR) or", e)
|
||||
|
||||
'''2Hall_HEP'''
|
||||
class HALL:
|
||||
|
||||
_pulse_turns=1/480 if version else 1/400 #圈数= 1/(减速比*磁极)
|
||||
_pulse_distance=_pulse_turns*math.pi*4.4 #距离= 圈数*π*轮胎直径
|
||||
|
||||
def __init__(self, pin):
|
||||
self.turns = 0
|
||||
self.distance = 0 #cm
|
||||
self._speed = 0 #cm/s
|
||||
self._on_receive = None
|
||||
self._time = time.ticks_ms()
|
||||
Pin(pin, Pin.IN).irq(handler=self._receive_cb, trigger=(Pin.IRQ_RISING | Pin.IRQ_FALLING))
|
||||
|
||||
def _receive_cb(self, event_source):
|
||||
self.turns += self._pulse_turns
|
||||
self.distance += self._pulse_distance
|
||||
self._speed += self._pulse_distance
|
||||
if self._on_receive:
|
||||
self._on_receive(round(self.turns, 2),round(self.distance, 2))
|
||||
|
||||
def irq_cb(self, callback):
|
||||
self._on_receive = callback
|
||||
|
||||
def initial(self, turns=None, distance=None):
|
||||
if not (turns is None):
|
||||
self.turns = turns
|
||||
if not (distance is None):
|
||||
self.distance = distance
|
||||
|
||||
@property
|
||||
def speed(self):
|
||||
value=self._speed/time.ticks_diff(time.ticks_ms(), self._time)*1000 if self._speed>0 else 0
|
||||
self._time = time.ticks_ms()
|
||||
self._speed = 0
|
||||
return round(value, 2)
|
||||
|
||||
hall_A = HALL(6)
|
||||
hall_B = HALL(7)
|
||||
|
||||
'''Reclaim memory'''
|
||||
gc.collect()
|
||||
@@ -0,0 +1,243 @@
|
||||
"""
|
||||
MixGo CE-Onboard resources
|
||||
|
||||
Micropython library for the MixGo CE-Onboard resources
|
||||
=======================================================
|
||||
|
||||
#Preliminary composition 20220901
|
||||
|
||||
dahanzimin From the Mixly Team
|
||||
"""
|
||||
|
||||
import time,gc
|
||||
from machine import Pin,SoftI2C,ADC,PWM,RTC
|
||||
from ws2812 import NeoPixel
|
||||
from music import MIDI
|
||||
|
||||
'''RTC'''
|
||||
rtc_clock=RTC()
|
||||
|
||||
'''i2c-onboard'''
|
||||
onboard_i2c=SoftI2C(scl = Pin(41), sda = Pin(42), freq = 400000)
|
||||
onboard_i2c_scan = onboard_i2c.scan()
|
||||
|
||||
'''Version judgment'''
|
||||
if 0x15 in onboard_i2c_scan:
|
||||
version=1
|
||||
elif 0x26 in onboard_i2c_scan:
|
||||
version=0
|
||||
else:
|
||||
print("Warning: Mixgo CE board is not detected, which may cause usage errors")
|
||||
|
||||
'''Matrix16x8'''
|
||||
try :
|
||||
import matrix16x8
|
||||
onboard_matrix = matrix16x8.Matrix(onboard_i2c)
|
||||
except Exception as e:
|
||||
print("Warning: Failed to communicate with Matrix16x8 or",e)
|
||||
|
||||
'''6-Button'''
|
||||
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 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)
|
||||
|
||||
A1key = Button(14)
|
||||
A2key = Button(21)
|
||||
A3key = Button(36)
|
||||
A4key = Button(37)
|
||||
B1key = Button(0)
|
||||
B2key = Button(35)
|
||||
|
||||
'''4-TouchPad'''
|
||||
class Touch_Pad:
|
||||
_pins = [4, 5, 6, 7]
|
||||
__species = {}
|
||||
__first_init = True
|
||||
def __new__(cls, pin, *args, **kwargs):
|
||||
if pin not in cls.__species.keys():
|
||||
cls.__first_init = True
|
||||
cls.__species[pin]=object.__new__(cls)
|
||||
return cls.__species[pin]
|
||||
|
||||
def __init__(self, pin):
|
||||
if self.__first_init:
|
||||
self.__first_init = False
|
||||
from machine import TouchPad
|
||||
self._pin = TouchPad(Pin(self._pins[pin]))
|
||||
|
||||
def is_touched(self,value):
|
||||
return self._pin.read() > value
|
||||
|
||||
def raw_value(self):
|
||||
return self._pin.read()
|
||||
|
||||
#Touch with function call
|
||||
def touched(pin,value=33000):
|
||||
return Touch_Pad(pin).is_touched(value) if value else Touch_Pad(pin).raw_value()
|
||||
|
||||
'''2-LED''' #Modify indexing method
|
||||
class LED:
|
||||
def __init__(self, pins=[]):
|
||||
self._pins = [PWM(Pin(pin), duty_u16=65535) for pin in pins]
|
||||
self._brightness = [0 for _ in range(len(self._pins))]
|
||||
|
||||
def setbrightness(self, index, val):
|
||||
if not 0 <= val <= 100:
|
||||
raise ValueError("Brightness must be in the range: 0-100%")
|
||||
self._brightness[index - 1] = val
|
||||
self._pins[index - 1].duty_u16(65535 - val * 65535 // 100)
|
||||
|
||||
def getbrightness(self, index):
|
||||
return self._brightness[index - 1]
|
||||
|
||||
def setonoff(self, index, val):
|
||||
if val == -1:
|
||||
self.setbrightness(index, 100) if self.getbrightness(index) < 50 else self.setbrightness(index, 0)
|
||||
elif val == 1:
|
||||
self.setbrightness(index, 100)
|
||||
elif val == 0:
|
||||
self.setbrightness(index, 0)
|
||||
|
||||
def getonoff(self, index):
|
||||
return True if self.getbrightness(index) > 50 else False
|
||||
|
||||
onboard_led = LED(pins=[33, 34])
|
||||
|
||||
'''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(13)
|
||||
|
||||
'''3-ADCSensor'''
|
||||
class ADCSensor:
|
||||
__species = {}
|
||||
__first_init = True
|
||||
def __new__(cls, pin, *args, **kwargs):
|
||||
if pin not in cls.__species.keys():
|
||||
cls.__first_init = True
|
||||
cls.__species[pin]=object.__new__(cls)
|
||||
return cls.__species[pin]
|
||||
|
||||
def __init__(self, pin):
|
||||
if self.__first_init:
|
||||
self.__first_init = False
|
||||
self._adc=ADC(Pin(pin))
|
||||
self._adc.atten(ADC.ATTN_11DB)
|
||||
self._switch = Pin(39, Pin.OUT)
|
||||
|
||||
def read(self):
|
||||
return self._adc.read_u16()
|
||||
|
||||
def switch(self,val):
|
||||
self._switch.value(val)
|
||||
|
||||
def get_brightness():
|
||||
return ADCSensor(15).read()
|
||||
|
||||
'''Function definition of different versions'''
|
||||
if version==0:
|
||||
'''ACC-Sensor'''
|
||||
try:
|
||||
import msa301
|
||||
onboard_acc = msa301.MSA301(onboard_i2c,front=True)
|
||||
except Exception as e:
|
||||
print("Warning: Failed to communicate with MSA301 or",e)
|
||||
|
||||
'''4-RGB'''
|
||||
if Pin(40, Pin.IN).value():
|
||||
onboard_rgb = NeoPixel(Pin(8), 4)
|
||||
else:
|
||||
onboard_rgb = NeoPixel(Pin(8), 4, ORDER=(0, 1, 2, 3))
|
||||
|
||||
'''1Buzzer-Music'''
|
||||
Pin(40, Pin.OUT).value(1)
|
||||
onboard_music =MIDI(17)
|
||||
|
||||
'''temperature'''
|
||||
def get_temperature():
|
||||
adc_val = ADCSensor(16).read()
|
||||
return adc_val * 3.9 / 5900
|
||||
|
||||
def infrared_left():
|
||||
print("Warning: Old version, without this function")
|
||||
|
||||
def infrared_right():
|
||||
print("Warning: Old version, without this function")
|
||||
|
||||
if version==1:
|
||||
'''ACC-Sensor'''
|
||||
try:
|
||||
import mxc6655xa
|
||||
onboard_acc = mxc6655xa.MXC6655XA(onboard_i2c,front=True)
|
||||
|
||||
'''temperature'''
|
||||
def get_temperature():
|
||||
return onboard_acc.temperature()
|
||||
|
||||
except Exception as e:
|
||||
print("Warning: Failed to communicate with MXC6655XA or",e)
|
||||
|
||||
'''4-RGB'''
|
||||
onboard_rgb = NeoPixel(Pin(8), 4)
|
||||
|
||||
'''1Buzzer-Music'''
|
||||
onboard_music =MIDI(40)
|
||||
|
||||
def infrared_left():
|
||||
ADCSensor(3).switch(1)
|
||||
time.sleep(0.01)
|
||||
adc=ADCSensor(3).read()
|
||||
ADCSensor(3).switch(0)
|
||||
return adc
|
||||
|
||||
def infrared_right():
|
||||
ADCSensor(16).switch(1)
|
||||
time.sleep(0.01)
|
||||
adc=ADCSensor(16).read()
|
||||
ADCSensor(16).switch(0)
|
||||
return adc
|
||||
|
||||
'''Reclaim memory'''
|
||||
gc.collect()
|
||||
Reference in New Issue
Block a user