Commit c2a7f6df authored by Nelso Jost's avatar Nelso Jost

NEW: fairly functional respirometro joystick

parents
.PHONY: paigamu setup run clean firmware
all: help
help:
@ echo "Usage: make <target> where target can be"
@ echo ""
@ echo " setup install all dependencies"
@ echo " firmware install joystick software on Arduino board"
@ echo " run execute the game"
run:
python3 main.py
firmware:
cd firmware && make
setup:
@ python3 -c "import pygame" || $(MAKE) install-pygame
@ python3 -c "import paigamu" || $(MAKE) get-paigamu
$(MAKE) install-pip
$(MAKE) install-python-deps
cd firmware && $(MAKE) setup
@ echo "-----------------------------------------------------------"
@ echo "[OK] paigamu lib installed on current directory"
@ echo "[OK] pip3 installed on system python3"
@ echo "[OK] pygame installed on system python3"
@ echo "[OK] python requirements installed on system python3"
install-pygame:
sudo apt-get install python3-dev python3-numpy libsdl-dev libsdl-image1.2-dev \
libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsmpeg-dev libportmidi-dev \
libavformat-dev libswscale-dev libjpeg-dev libfreetype6-dev mercurial
hg clone https://bitbucket.org/pygame/pygame
cd pygame && python3 setup.py build
cd pygame && sudo python3 setup.py install
rm -rf pygame
get-paigamu:
rm -rf paigamu paigamu.git paigamu.zip
wget -O paigamu.zip https://git.cta.if.ufrgs.br/ctautils/paigamu/repository/archive.zip
unzip paigamu.zip
cp -rf paigamu.git/paigamu .
rm -rf paigamu.git paigamu.zip
get-platformio:
wget -O platformio.zip https://git.cta.if.ufrgs.br/ctautils/platformiowizard/repository/archive.zip
unzip platformio.zip
cd platformiowizard.git/firmware && cp -r .scripts Makefile ../../firmware/
rm -rf platformiowizard.git platformio.zip firmware/blink
install-pip:
wget https://bootstrap.pypa.io/get-pip.py
sudo -H python3 get-pip.py
@ rm get-pip.py
install-python-deps:
sudo -H pip3 install -r requirements.txt
clean:
rm -rf paigamu
cd firmware && rm -rf .scripts Makefile .build
#define APIN 0
char command;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available()) {
command = Serial.read();
if (command == 'p')
{
Serial.println(analogRead(APIN));
}
}
}
from paigamu import *
import serial
import time
class AppLabirintoGame(GameApp):
RESOLUTION = (640, 480)
FRAMERATE = 0
def on_setup(self):
self.main_scene = MainScene(app=self, surface=self.screen)
self.joystick_respirometro = JoystickRespirometro()
# self.timer = Timer(dt=0.5, callback=lambda: print("Oi"))
def on_gameloop(self):
# self.timer.update()
self.main_scene.update()
self.main_scene.draw()
def on_keydown_event(self, event):
if event.key == pygame.K_SPACE:
self.main_scene['player'].centery = self.main_scene.rect.centery
class MainScene(Scene):
def on_setup(self):
self['player'] = PlayerSprite(
color='green',
size=(50, 50),
left=100,
centery=self.rect.centery,
vel=(0, 100))
class Timer:
def __init__(self, dt, callback):
self.current_time = time.time()
self.dt, self.callback = dt, callback
self.elapsed = 0
def update(self):
t = time.time()
self.elapsed += t - self.current_time
self.current_time = t
if self.elapsed > self.dt:
self.callback()
self.elapsed = 0
# print('elapsed', self.elapsed)
class PlayerSprite(Sprite):
running = False
def __init__(self, **kwargs):
super().__init__(**kwargs)
def update(self, dt):
self.handle_joystick()
if self.running:
self.vel.y += self.app.dt * self.accel.y
self.y += self.app.dt * self.vel.y
def handle_keydown(self, event):
if event.key == pygame.K_DOWN:
self.vel.j, self.running = 1, True
if event.key >= pygame.K_UP:
self.vel.j, self.running = -1, True
def handle_keypressed(self):
if self.app.is_key_pressed('K_DOWN'):
self.vel.j, self.running = 1, True
elif self.app.is_key_pressed('K_UP'):
self.vel.j, self.running = -1, True
else:
self.running = False
def handle_joystick(self):
_MAX_VEL = 300
self.app.joystick_respirometro.update()
if self.app.joystick_respirometro.inhaling():
self.accel.y = 200
self.vel.j, self.running = 1, True
# print('inhaling', self.pos, self.vel, self.app.dt)
elif self.app.joystick_respirometro.exhaling():
self.accel.y = -200
self.vel.j, self.running = -1, True
# print('exhaling', self.pos, self.vel, self.app.dt)
else:
self.running = False
# print('idle', self.pos, self.vel, self.app.dt)
if(self.vel.ymag > _MAX_VEL):
self.vel.ymag = _MAX_VEL
class JoystickRespirometro:
_STATES = {'in': 1, 'ex': -1, 'idle': 0}
_STATESR = {1: 'in', -1: 'ex', 0: 'idle'}
_AMPLITUDE = 2
def __init__(self):
self._state = 0
self._last_read = 0
self._sercon = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
print(self._sercon)
self.timer = Timer(dt=0.2, callback=self.refresh)
def inhaling(self):
return self._state == self._STATES['in']
def exhaling(self):
return self._state == self._STATES['ex']
def refresh(self):
self._sercon.write(b'p')
try:
raw_read = self._sercon.readline()
read = int(raw_read.decode('ascii'))
except:
return
dv = (read - self._last_read)
if dv > self._AMPLITUDE:
self._state = self._STATES['in']
elif dv < -self._AMPLITUDE:
self._state = self._STATES['ex']
else:
self._state = self._STATES['idle']
self._last_read = read
print(read, dv, self._STATESR[self._state])
def update(self):
self.timer.update()
if __name__ == '__main__':
AppLabirintoGame().run()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment