Initial code with Python stubs for Lego Spike API, Go Robot Code and an example how to structure our code

This commit is contained in:
Lars Haferkamp 2023-02-14 17:43:41 +01:00
commit a58ba5fd6d
18 changed files with 4561 additions and 0 deletions

93
main.py Normal file
View file

@ -0,0 +1,93 @@
# LEGO type:standard slot:5 autostart
import os, sys
from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair
from spike.control import wait_for_seconds, wait_until, Timer
from hub import battery
from math import *
############## Allgemein: Prüfe Batteriezustand ###############################
if battery.voltage() < 8000: #set threshold for battery level
print("Spannung der Batterie zu niedrig: " + str(battery.voltage()) + " \n"
+ "--------------------------------------- \n "
+ "#### UNBEDINGT ROBOTER AUFLADEN !!! #### \n"
+ "---------------------------------------- \n")
else:
print("Spannung der Batterie " + str(battery.voltage()) + "\n")
################################################################################
############################## NICHT ÄNDERN ###############################
def importFile(slotid=0, precompiled=False, module_name='importFile'):
print("##### START # IMPORTING CODE FROM SLOT "+str(slotid)+" ##############")
import os, sys
suffix = ".py"
if precompiled:
suffix = ".mpy"
with open("/projects/.slots","rt") as f:
slots = eval(str(f.read()))
print(slots)
print(os.listdir("/projects/"+str(slots[slotid]["id"])))
with open("/projects/"+str(slots[slotid]["id"])+"/__init__"+suffix,"rb") as f:
print("trying to read import program")
program = f.read()
print(program)
try:
os.remove("/"+module_name+suffix)
except:
pass
with open("/"+module_name+suffix,"w+") as f:
print("trying to write import program")
f.write(program)
if (module_name in sys.modules):
del sys.modules[module_name]
#exec("from " + module_name + " import *")
print("##### END # IMPORTING CODE FROM SLOT "+str(slotid)+" ##############")
#####################################################################################
################ Importiere Code aus andere Dateien #################################
# Importiere Code aus der Datei "iqrobot.py"
# Dateiname und Modulname sollten gleich sein, dann kann man Code Completion nutzen
importFile(slotid=6, precompiled=True, module_name="iqrobot")
import iqrobot as iq
print(iq.HELLO)
# Importiere Go Robot Code
#importFile(slotid=3, precompiled=True, module_name="gorobot")
#import gorobot as gr
#gr.exampleFour()
#gr.db.gyroRotation(90, 25, 35, 25)
################### Hauptcode ####################################
'''
Code zum Lösen einer Aufgabe, kann oben importierten Code nutzen
Es können auch pro Aufgabe eigene Funktionen geschrieben werden
Wichtig ist, dass die PORTS der Sensoren überall gleich sind
und auch `hub` als Instanz von PrimeHub
dh auch an die Funktionen im importierten Code übergeben werde
'''
# Definiere an welchen Ports die Sensoren angeschlossen sind
COLOR_SENSOR_PORT = 'E'
LEFT_MOTOR_PORT = 'A'
RIGHT_MOTOR_PORT = 'B'
# Initialisieren des Hubs, der Aktoren und Sensoren
hub = PrimeHub()
# Initialisiere Robot Klasse mit unseren Funktionen
iqRobot: iq.IQRobot = iq.IQRobot(hub, LEFT_MOTOR_PORT, RIGHT_MOTOR_PORT, COLOR_SENSOR_PORT)
# Führe Funktionen aus unser Robot Klasse aus:
iqRobot.show('HAPPY')
iqRobot.driveForward(2.0)
colorIntensity = iqRobot.getColorIntensity()
print("Farbintensität: " + str(colorIntensity))