Epreuve 5 mk2
This commit is contained in:
parent
4aca719414
commit
6a3fb3f502
@ -13,6 +13,8 @@
|
|||||||
|
|
||||||
import sys, time
|
import sys, time
|
||||||
|
|
||||||
|
import uasyncio as asyncio
|
||||||
|
|
||||||
|
|
||||||
class CPU:
|
class CPU:
|
||||||
pc: int = 0
|
pc: int = 0
|
||||||
@ -37,12 +39,16 @@ class Simulator:
|
|||||||
self.cpu = CPU()
|
self.cpu = CPU()
|
||||||
self.program_size = len(program)
|
self.program_size = len(program)
|
||||||
self.motorCallback = None
|
self.motorCallback = None
|
||||||
|
self.registerCallback = None
|
||||||
|
|
||||||
# ---- Getter / Setter pour interfacer le robot
|
# ---- Getter / Setter pour interfacer le robot
|
||||||
|
|
||||||
def setMotorCallback(self, callback):
|
def setMotorCallback(self, callback):
|
||||||
self.motorCallback = callback
|
self.motorCallback = callback
|
||||||
|
|
||||||
|
def setRegisterCallback(self, callback):
|
||||||
|
self.registerCallback = callback
|
||||||
|
|
||||||
|
|
||||||
# ----------------- utilitaires mémoire / pile -----------------
|
# ----------------- utilitaires mémoire / pile -----------------
|
||||||
|
|
||||||
@ -126,6 +132,8 @@ class Simulator:
|
|||||||
r = b & 0b11
|
r = b & 0b11
|
||||||
instr = f"POP R{r}"
|
instr = f"POP R{r}"
|
||||||
c.regs[r] = self.pop()
|
c.regs[r] = self.pop()
|
||||||
|
if (setRegisterCallback != None):
|
||||||
|
setRegisterCallback(r, c.regs[r])
|
||||||
|
|
||||||
# --- MOV Rx valeur / SUB Rx valeur / CMP Rx valeur ---
|
# --- MOV Rx valeur / SUB Rx valeur / CMP Rx valeur ---
|
||||||
elif (b & 0b11111100) == 0b11100000: # MOV Rx valeur
|
elif (b & 0b11111100) == 0b11100000: # MOV Rx valeur
|
||||||
@ -134,6 +142,8 @@ class Simulator:
|
|||||||
size = 2
|
size = 2
|
||||||
instr = f"MOV R{r}, {imm}"
|
instr = f"MOV R{r}, {imm}"
|
||||||
c.regs[r] = imm
|
c.regs[r] = imm
|
||||||
|
if (setRegisterCallback != None):
|
||||||
|
setRegisterCallback(r, c.regs[r])
|
||||||
|
|
||||||
elif (b & 0b11111100) == 0b00010000: # SUB Rx valeur
|
elif (b & 0b11111100) == 0b00010000: # SUB Rx valeur
|
||||||
r = b & 0b11
|
r = b & 0b11
|
||||||
@ -141,6 +151,8 @@ class Simulator:
|
|||||||
size = 2
|
size = 2
|
||||||
instr = f"SUB R{r}, {imm}"
|
instr = f"SUB R{r}, {imm}"
|
||||||
c.regs[r] = (c.regs[r] - imm) & 0xFF
|
c.regs[r] = (c.regs[r] - imm) & 0xFF
|
||||||
|
if (setRegisterCallback != None):
|
||||||
|
setRegisterCallback(r, c.regs[r])
|
||||||
|
|
||||||
elif (b & 0b11111100) == 0b10010000: # CMP Rx valeur
|
elif (b & 0b11111100) == 0b10010000: # CMP Rx valeur
|
||||||
r = b & 0b11
|
r = b & 0b11
|
||||||
@ -157,12 +169,16 @@ class Simulator:
|
|||||||
src = b & 0b11
|
src = b & 0b11
|
||||||
instr = f"MOV R{dst}, R{src}"
|
instr = f"MOV R{dst}, R{src}"
|
||||||
c.regs[dst] = c.regs[src]
|
c.regs[dst] = c.regs[src]
|
||||||
|
if (setRegisterCallback != None):
|
||||||
|
setRegisterCallback(dst, c.regs[dst])
|
||||||
|
|
||||||
elif (b & 0b11110000) == 0b11010000: # SUB Rx Ry
|
elif (b & 0b11110000) == 0b11010000: # SUB Rx Ry
|
||||||
dst = (b >> 2) & 0b11
|
dst = (b >> 2) & 0b11
|
||||||
src = b & 0b11
|
src = b & 0b11
|
||||||
instr = f"SUB R{dst}, R{src}"
|
instr = f"SUB R{dst}, R{src}"
|
||||||
c.regs[dst] = (c.regs[dst] - c.regs[src]) & 0xFF
|
c.regs[dst] = (c.regs[dst] - c.regs[src]) & 0xFF
|
||||||
|
if (setRegisterCallback != None):
|
||||||
|
setRegisterCallback(dst, c.regs[dst])
|
||||||
|
|
||||||
elif (b & 0b11110000) == 0b00110000: # CMP Rx Ry
|
elif (b & 0b11110000) == 0b00110000: # CMP Rx Ry
|
||||||
dst = (b >> 2) & 0b11
|
dst = (b >> 2) & 0b11
|
||||||
@ -183,6 +199,8 @@ class Simulator:
|
|||||||
eff = (addr + c.regs[src]) & 0xFF
|
eff = (addr + c.regs[src]) & 0xFF
|
||||||
c.regs[dst] = self.ram[eff]
|
c.regs[dst] = self.ram[eff]
|
||||||
extra_cycles = 1 # 2 octets -> 2 cycles +1 = 3
|
extra_cycles = 1 # 2 octets -> 2 cycles +1 = 3
|
||||||
|
if (setRegisterCallback != None):
|
||||||
|
setRegisterCallback(dst, c.regs[dst])
|
||||||
|
|
||||||
elif (b & 0b11110000) == 0b01110000: # STR Rx Ry _label
|
elif (b & 0b11110000) == 0b01110000: # STR Rx Ry _label
|
||||||
dst = (b >> 2) & 0b11
|
dst = (b >> 2) & 0b11
|
||||||
@ -257,21 +275,13 @@ class Simulator:
|
|||||||
self.step()
|
self.step()
|
||||||
steps += 1
|
steps += 1
|
||||||
|
|
||||||
def motorCallback(motG, motD):
|
def StartCPU(program, callback, registerCallback):
|
||||||
sMotG = 1 - ((motG >> 2) & 0b10)
|
|
||||||
sMotD = 1 - ((motD >> 2) & 0b10)
|
|
||||||
motG = ((motG & 0b0111) * sMotG) * 100 / 7
|
|
||||||
motD = ((motD & 0b0111) * sMotD) * 100 / 7
|
|
||||||
print("Mot G :", motG)
|
|
||||||
print("Mot D :", motD)
|
|
||||||
|
|
||||||
|
|
||||||
def StartCPU(program, callback):
|
|
||||||
sim = Simulator(program)
|
sim = Simulator(program)
|
||||||
sim.setMotorCallback(callback)
|
sim.setMotorCallback(callback)
|
||||||
|
sim.setRegisterCallback(setRegisterCallback)
|
||||||
while sim.cpu.running:
|
while sim.cpu.running:
|
||||||
sim.run(max_steps = 1)
|
sim.run(max_steps = 1)
|
||||||
time.sleep(0.1)
|
#time.sleep(0.1)
|
||||||
|
|
||||||
import time
|
import time
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
@ -286,6 +296,6 @@ if __name__ == "__main__":
|
|||||||
print("filename: " + filename)
|
print("filename: " + filename)
|
||||||
with open(filename, "rb") as f:
|
with open(filename, "rb") as f:
|
||||||
program = f.read()
|
program = f.read()
|
||||||
StartCPU(program, motorCallback)
|
StartCPU(program, None, None)
|
||||||
else:
|
else:
|
||||||
print("Needs *.bin as parameter")
|
print("Needs *.bin as parameter")
|
||||||
|
|||||||
63
Path.asm
63
Path.asm
@ -1,41 +1,44 @@
|
|||||||
_main:
|
_main:
|
||||||
|
|
||||||
; Start
|
; Start
|
||||||
MOV R0 59
|
|
||||||
|
MOV R0 17
|
||||||
OUT R0
|
OUT R0
|
||||||
TIM 132 ; Demi-tour OK
|
TIM 130 ; Slow Start OK
|
||||||
|
|
||||||
|
MOV R0 34
|
||||||
|
OUT R0
|
||||||
|
TIM 130 ; Slow Start OK
|
||||||
|
|
||||||
|
MOV R0 51
|
||||||
|
OUT R0
|
||||||
|
TIM 162 ; Ligne droite OK
|
||||||
|
|
||||||
|
MOV R0 49 ; 0b 0011 0001
|
||||||
|
OUT R0
|
||||||
|
TIM 130 ; Rotation droite OK
|
||||||
|
|
||||||
|
MOV R0 51
|
||||||
|
OUT R0
|
||||||
|
TIM 152 ; Ligne droite
|
||||||
|
|
||||||
|
MOV R0 19 ; 0b 0001 0011
|
||||||
|
OUT R0
|
||||||
|
TIM 130 ; Rotation gauche
|
||||||
|
;TIM 50
|
||||||
|
|
||||||
|
MOV R0 51
|
||||||
|
OUT R0
|
||||||
|
TIM 136 ; Ligne droite
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
MOV R0 0
|
MOV R0 0
|
||||||
OUT R0
|
OUT R0 ; STOP
|
||||||
TIM 100 ; P'tite pause OK
|
RET
|
||||||
|
|
||||||
MOV R0 59
|
|
||||||
OUT R0
|
|
||||||
TIM 128
|
|
||||||
TIM 100 ; Demi-tour pour marche arriere OK
|
|
||||||
|
|
||||||
MOV R0 0
|
|
||||||
OUT R0
|
|
||||||
TIM 100 ; P'tite pause OK
|
|
||||||
|
|
||||||
MOV R0 204
|
|
||||||
OUT R0
|
|
||||||
TIM 142 ; Michael Jackson OK
|
|
||||||
|
|
||||||
MOV R0 0
|
|
||||||
OUT R0
|
|
||||||
TIM 100 ; P'tite pause OK
|
|
||||||
|
|
||||||
MOV R0 179
|
|
||||||
OUT R0
|
|
||||||
TIM 132 ; Demi-tour
|
|
||||||
|
|
||||||
|
_sleep:
|
||||||
MOV R0 0
|
MOV R0 0
|
||||||
OUT R0
|
OUT R0
|
||||||
TIM 100 ; P'tite pause
|
TIM 100 ; P'tite pause
|
||||||
|
|
||||||
MOV R0 204
|
|
||||||
OUT R0
|
|
||||||
TIM 144 ; Michael Jackson
|
|
||||||
|
|
||||||
RET
|
RET
|
||||||
36
main.py
36
main.py
@ -10,12 +10,11 @@ import buzzer
|
|||||||
import binascii
|
import binascii
|
||||||
import uasyncio as asyncio
|
import uasyncio as asyncio
|
||||||
import RobotBleServer
|
import RobotBleServer
|
||||||
#import base64
|
|
||||||
|
|
||||||
|
|
||||||
from Interpreteur import StartCPU
|
from Interpreteur import StartCPU
|
||||||
|
|
||||||
motorSpeedFactor = 50
|
motorSpeedFactor = 50
|
||||||
|
motorDCompensation = 1.04
|
||||||
|
|
||||||
alphabot = AlphaBot_v2()
|
alphabot = AlphaBot_v2()
|
||||||
oled = SSD1306_I2C(128, 64, alphabot.i2c)
|
oled = SSD1306_I2C(128, 64, alphabot.i2c)
|
||||||
@ -23,36 +22,19 @@ oled = SSD1306_I2C(128, 64, alphabot.i2c)
|
|||||||
oled.fill(0)
|
oled.fill(0)
|
||||||
oled.show()
|
oled.show()
|
||||||
|
|
||||||
|
|
||||||
def motorCallback(motG, motD):
|
def motorCallback(motG, motD):
|
||||||
sMotG = 1 - ((motG >> 2) & 0b10)
|
sMotG = 1 - ((motG >> 2) & 0b10)
|
||||||
sMotD = 1 - ((motD >> 2) & 0b10)
|
sMotD = 1 - ((motD >> 2) & 0b10)
|
||||||
motG = ((motG & 0b0111) * sMotG) * motorSpeedFactor / 7
|
motG = ((motG & 0b0111) * sMotG) * motorSpeedFactor / 7
|
||||||
motD = ((motD & 0b0111) * sMotD) * motorSpeedFactor / 7
|
motD = (((motD & 0b0111) * sMotD) * motorSpeedFactor / 7) * motorDCompensation
|
||||||
print("Mot G :", motG)
|
print("Mot G :", motG)
|
||||||
print("Mot D :", motD)
|
print("Mot D :", motD)
|
||||||
alphabot.setMotors(left=motG, right=motD)
|
alphabot.setMotors(left=motG, right=motD)
|
||||||
|
|
||||||
|
def registerCallback(register, value):
|
||||||
# while True:
|
print(f"Register R{register} changed to {value}")
|
||||||
# joystickButton = alphabot.getJoystickValue()
|
pass
|
||||||
# if (joystickButton == "center"):
|
|
||||||
# oled.text("Coucou !", 0, 0)
|
|
||||||
# oled.show()
|
|
||||||
# utime.sleep(1)
|
|
||||||
# oled.fill(0)
|
|
||||||
# oled.show()
|
|
||||||
# if (joystickButton == "left"):
|
|
||||||
# alphabot.setMotors(left=-100, right=100)
|
|
||||||
# utime.sleep(1)
|
|
||||||
# alphabot.stop()
|
|
||||||
# if (joystickButton == "right"):
|
|
||||||
# StartCPU("./out.bin", motorCallback)
|
|
||||||
# alphabot.stop()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -70,13 +52,9 @@ def onMsgToRobot(data:str|bytes):
|
|||||||
:param data: message received"""
|
:param data: message received"""
|
||||||
checksum = binascii.crc32(data)
|
checksum = binascii.crc32(data)
|
||||||
print('received', data, '=>', checksum)
|
print('received', data, '=>', checksum)
|
||||||
#data = data.encode("ascii")
|
|
||||||
#data = base64.decodebytes(data)
|
|
||||||
print(data)
|
print(data)
|
||||||
|
|
||||||
|
StartCPU(data, motorCallback, registerCallback)
|
||||||
|
|
||||||
StartCPU(data, motorCallback)
|
|
||||||
alphabot.stop()
|
alphabot.stop()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -162,7 +162,7 @@ toupie 76 100ms = 360 + 90°
|
|||||||
|
|
||||||
{"type": "connect", "name": "Nogard"}
|
{"type": "connect", "name": "Nogard"}
|
||||||
|
|
||||||
{"type": "msg", "format": "base64", "string": "4Crw+FCA"}
|
{"type": "msg", "format": "base64", "string": "4Dvw+IKA"}
|
||||||
|
|
||||||
Demi tour
|
Demi tour
|
||||||
{"type": "msg", "format": "base64", "string": "4Dvw+IHgAPCA"}
|
{"type": "msg", "format": "base64", "string": "4Dvw+IHgAPCA"}
|
||||||
@ -171,6 +171,6 @@ Demi tour + fuite
|
|||||||
{"type": "msg", "format": "base64", "string": "4Dvw+ID4MuAA8Phk4Mzw+KXgAPDgAPD4ZIA="}
|
{"type": "msg", "format": "base64", "string": "4Dvw+ID4MuAA8Phk4Mzw+KXgAPDgAPD4ZIA="}
|
||||||
|
|
||||||
Parcours
|
Parcours
|
||||||
{"type": "msg", "format": "base64", "string": "4Dvw+ITgAPD4ZOA78PiA+GTgAPD4ZODM8PiO4ADw+GTgs/D4hOAA8Phk4Mzw+JCA"}
|
{"type": "msg", "format": "base64", "string": "4BHw+ILgIvD4guAz8Pii4DHw+ILgM/D4mOAT8PiC4DPw+IjgAPCA4ADw+GSA"}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user