flow control

This commit is contained in:
leo 2022-11-23 12:23:40 +01:00
parent ca62e8ef89
commit 9441662d95
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
5 changed files with 41 additions and 13 deletions

View File

@ -5,6 +5,7 @@
void loop(); void loop();
void setup(); void setup();
long millis(); long millis();
long micros();
int abs(int in); int abs(int in);
void pinMode(int pin, int mode); void pinMode(int pin, int mode);
int analogRead(int pin); int analogRead(int pin);

View File

@ -33,8 +33,10 @@ int main(int argc, char** argv){
} }
cmd_buff[cmd_buff_ind++] = c; cmd_buff[cmd_buff_ind++] = c;
} }
printf("$dt,%ld;", micros());
loop(); loop();
printf("millis %ld\n", millis()); printf("*");
fflush(stdout);
} }
} }
@ -45,6 +47,13 @@ long millis(){
return milli; return milli;
} }
long micros(){
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
uint64_t milli = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
return milli;
}
int abs(int in){ int abs(int in){
return abs(in); return abs(in);
} }

View File

@ -9,9 +9,15 @@ class aip_parser:
return return
self.robot.analogWrite(*map(int, args)) self.robot.analogWrite(*map(int, args))
def updateDt(self, args):
if len(args) != 1:
return
self.robot.setTime(int(args[0]))
cmds = { cmds = {
"dw" : digitalWrite, "dw" : digitalWrite,
"aw" : analogWrite "aw" : analogWrite,
"dt" : updateDt,
} }
def __init__(self, stdin, stdout, robot): def __init__(self, stdin, stdout, robot):
@ -23,6 +29,8 @@ class aip_parser:
def poll(self): def poll(self):
c=self.stdout.read(1) c=self.stdout.read(1)
if not c: if not c:
return True
if c == b'*':
return False return False
try: try:
print(c.decode(), end='') print(c.decode(), end='')

View File

@ -2,9 +2,9 @@ from math import cos, sin, pi, atan2, tan
class Robot: class Robot:
# position # position
pos=(1,2) pos=(1,1.5)
# orientation # orientation
rot=0 rot=-1
# velocite # velocite
v=0 v=0
# velocite angulaire # velocite angulaire
@ -12,7 +12,7 @@ class Robot:
# velocite des roues (non angulaire) # velocite des roues (non angulaire)
wv=[0,0] wv=[0,0]
# velocite max # velocite max
wmv=0.01 wmv=0.1
# espace entre les deux roues # espace entre les deux roues
l=0.1 l=0.1
# time # time
@ -28,7 +28,7 @@ class Robot:
# ecart entre les deux photores # ecart entre les deux photores
photores_l = 0.1 photores_l = 0.1
# longeur du séparateur entre les photorésistances # longeur du séparateur entre les photorésistances
photores_sep_l = 0.1 photores_sep_l = 0.05
# constantes de résistance des photoresistances # constantes de résistance des photoresistances
PHOTORES_LIGHT = 100 PHOTORES_LIGHT = 100
PHOTORES_SHADOW = 20000 PHOTORES_SHADOW = 20000
@ -36,13 +36,17 @@ class Robot:
pr_l_pin = 0 # A0 voir adapter_ard.h pr_l_pin = 0 # A0 voir adapter_ard.h
pr_r_pin = 1 # A1 pr_r_pin = 1 # A1
def __init__(self, dt): def __init__(self):
self.dt = dt
self.pr_max_ang = tan((self.photores_l/2)/self.photores_sep_l) self.pr_max_ang = tan((self.photores_l/2)/self.photores_sep_l)
self.t = 0
self.dt = -1
self.angle2spot = 0
def update(self): def update(self):
if(self.dt == -1):
return
self.compute_light() self.compute_light()
self.t += self.dt print("***********", self.t, "****", self.dt)
# https://robotics.stackexchange.com/questions/106/what-is-a-suitable-model-for-two-wheeled-robots # https://robotics.stackexchange.com/questions/106/what-is-a-suitable-model-for-two-wheeled-robots
self.v = 1/2*(self.wv[0]+self.wv[1]) self.v = 1/2*(self.wv[0]+self.wv[1])
self.angv = 1/self.l*(self.wv[1]-self.wv[0]) self.angv = 1/self.l*(self.wv[1]-self.wv[0])
@ -81,4 +85,9 @@ class Robot:
def digitalWrite(self, pin, level): def digitalWrite(self, pin, level):
self.analogWrite(pin, level*255) self.analogWrite(pin, level*255)
def setTime(self, t):
self.dt = (t/1000000 - self.t)
self.t = t/1000000
assert(self.dt < 0.1)

View File

@ -4,6 +4,7 @@ from parser import aip_parser
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
import fcntl import fcntl
import os import os
from time import sleep
# Pygame # Pygame
from math import degrees, cos, sin from math import degrees, cos, sin
@ -17,7 +18,7 @@ black = 0, 0, 0
screen = pygame.display.set_mode(size) screen = pygame.display.set_mode(size)
# le robot # le robot
r = Robot(1/60) r = Robot()
# on lance le programme arduino # on lance le programme arduino
ard_process = Popen(["build/app"], stdin=PIPE, stdout=PIPE); ard_process = Popen(["build/app"], stdin=PIPE, stdout=PIPE);
@ -42,11 +43,12 @@ while True:
# On demarre un tour de simulation # On demarre un tour de simulation
ard_process.stdin.write(b'@') ard_process.stdin.write(b'@')
ard_process.stdin.flush()
# tant qu'il y as qqu chose sur le bus on le parse # tant qu'il y as qqu chose sur le bus on le parse
while(parser.poll()): while(parser.poll()):
pass pass
# on met à jour la simulation # on met à jour la simulation
r.update() r.update()
@ -66,4 +68,3 @@ while True:
center = rsurf_r.get_rect().center center = rsurf_r.get_rect().center
screen.blit(rsurf_r, (r.pos[0]*100-center[0],r.pos[1]*100-center[1])) screen.blit(rsurf_r, (r.pos[0]*100-center[0],r.pos[1]*100-center[1]))
pygame.display.flip() pygame.display.flip()