Epreuve 5 mk2

This commit is contained in:
Nogard 2026-03-22 04:49:01 +01:00
parent 4aca719414
commit 6a3fb3f502
6 changed files with 65 additions and 74 deletions

View File

@ -13,6 +13,8 @@
import sys, time
import uasyncio as asyncio
class CPU:
pc: int = 0
@ -37,12 +39,16 @@ class Simulator:
self.cpu = CPU()
self.program_size = len(program)
self.motorCallback = None
self.registerCallback = None
# ---- Getter / Setter pour interfacer le robot
def setMotorCallback(self, callback):
self.motorCallback = callback
def setRegisterCallback(self, callback):
self.registerCallback = callback
# ----------------- utilitaires mémoire / pile -----------------
@ -126,6 +132,8 @@ class Simulator:
r = b & 0b11
instr = f"POP R{r}"
c.regs[r] = self.pop()
if (setRegisterCallback != None):
setRegisterCallback(r, c.regs[r])
# --- MOV Rx valeur / SUB Rx valeur / CMP Rx valeur ---
elif (b & 0b11111100) == 0b11100000: # MOV Rx valeur
@ -134,6 +142,8 @@ class Simulator:
size = 2
instr = f"MOV R{r}, {imm}"
c.regs[r] = imm
if (setRegisterCallback != None):
setRegisterCallback(r, c.regs[r])
elif (b & 0b11111100) == 0b00010000: # SUB Rx valeur
r = b & 0b11
@ -141,6 +151,8 @@ class Simulator:
size = 2
instr = f"SUB R{r}, {imm}"
c.regs[r] = (c.regs[r] - imm) & 0xFF
if (setRegisterCallback != None):
setRegisterCallback(r, c.regs[r])
elif (b & 0b11111100) == 0b10010000: # CMP Rx valeur
r = b & 0b11
@ -157,12 +169,16 @@ class Simulator:
src = b & 0b11
instr = f"MOV R{dst}, R{src}"
c.regs[dst] = c.regs[src]
if (setRegisterCallback != None):
setRegisterCallback(dst, c.regs[dst])
elif (b & 0b11110000) == 0b11010000: # SUB Rx Ry
dst = (b >> 2) & 0b11
src = b & 0b11
instr = f"SUB R{dst}, R{src}"
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
dst = (b >> 2) & 0b11
@ -183,6 +199,8 @@ class Simulator:
eff = (addr + c.regs[src]) & 0xFF
c.regs[dst] = self.ram[eff]
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
dst = (b >> 2) & 0b11
@ -257,21 +275,13 @@ class Simulator:
self.step()
steps += 1
def motorCallback(motG, motD):
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):
def StartCPU(program, callback, registerCallback):
sim = Simulator(program)
sim.setMotorCallback(callback)
sim.setRegisterCallback(setRegisterCallback)
while sim.cpu.running:
sim.run(max_steps = 1)
time.sleep(0.1)
#time.sleep(0.1)
import time
# ---------------------------------------------------------
@ -286,6 +296,6 @@ if __name__ == "__main__":
print("filename: " + filename)
with open(filename, "rb") as f:
program = f.read()
StartCPU(program, motorCallback)
StartCPU(program, None, None)
else:
print("Needs *.bin as parameter")

0
LED.asm Normal file
View File

View File

@ -1,41 +1,44 @@
_main:
; Start
MOV R0 59
MOV R0 17
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
OUT R0
TIM 100 ; P'tite pause OK
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
OUT R0 ; STOP
RET
_sleep:
MOV R0 0
OUT R0
TIM 100 ; P'tite pause
MOV R0 204
OUT R0
TIM 144 ; Michael Jackson
RET

36
main.py
View File

@ -10,12 +10,11 @@ import buzzer
import binascii
import uasyncio as asyncio
import RobotBleServer
#import base64
from Interpreteur import StartCPU
motorSpeedFactor = 50
motorDCompensation = 1.04
alphabot = AlphaBot_v2()
oled = SSD1306_I2C(128, 64, alphabot.i2c)
@ -23,36 +22,19 @@ oled = SSD1306_I2C(128, 64, alphabot.i2c)
oled.fill(0)
oled.show()
def motorCallback(motG, motD):
sMotG = 1 - ((motG >> 2) & 0b10)
sMotD = 1 - ((motD >> 2) & 0b10)
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 D :", motD)
alphabot.setMotors(left=motG, right=motD)
# while True:
# joystickButton = alphabot.getJoystickValue()
# 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()
def registerCallback(register, value):
print(f"Register R{register} changed to {value}")
pass
@ -70,13 +52,9 @@ def onMsgToRobot(data:str|bytes):
:param data: message received"""
checksum = binascii.crc32(data)
print('received', data, '=>', checksum)
#data = data.encode("ascii")
#data = base64.decodebytes(data)
print(data)
StartCPU(data, motorCallback)
StartCPU(data, motorCallback, registerCallback)
alphabot.stop()

View File

@ -162,7 +162,7 @@ toupie 76 100ms = 360 + 90°
{"type": "connect", "name": "Nogard"}
{"type": "msg", "format": "base64", "string": "4Crw+FCA"}
{"type": "msg", "format": "base64", "string": "4Dvw+IKA"}
Demi tour
{"type": "msg", "format": "base64", "string": "4Dvw+IHgAPCA"}
@ -171,6 +171,6 @@ Demi tour + fuite
{"type": "msg", "format": "base64", "string": "4Dvw+ID4MuAA8Phk4Mzw+KXgAPDgAPD4ZIA="}
Parcours
{"type": "msg", "format": "base64", "string": "4Dvw+ITgAPD4ZOA78PiA+GTgAPD4ZODM8PiO4ADw+GTgs/D4hOAA8Phk4Mzw+JCA"}
{"type": "msg", "format": "base64", "string": "4BHw+ILgIvD4guAz8Pii4DHw+ILgM/D4mOAT8PiC4DPw+IjgAPCA4ADw+GSA"}

BIN
out.bin

Binary file not shown.