diff --git a/Files/labyrinthe.py b/Files/labyrinthe.py new file mode 100644 index 0000000000000000000000000000000000000000..417c71fc06eacc00474c9cc901f56a0fafad173b --- /dev/null +++ b/Files/labyrinthe.py @@ -0,0 +1,316 @@ +#!/usr/bin/env pybricks-micropython + +from pybricks.hubs import EV3Brick +from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, + InfraredSensor, UltrasonicSensor, GyroSensor) +from pybricks.parameters import Port, Stop, Direction, Button, Color +from pybricks.tools import wait, StopWatch, DataLog +from pybricks.robotics import DriveBase +from pybricks.media.ev3dev import SoundFile, ImageFile +import threading + +# This program requires LEGO EV3 MicroPython v2.0 or higher. +# Click "Open user guide" on the EV3 extension tab for more information. + +# Initialisation de la brique EV3 +ev3 = EV3Brick() + +# Initialisation des moteurs +left_motor = Motor(Port.D) +right_motor = Motor(Port.A) + +# Initialisation des capteurs +gyro_sensor = GyroSensor(Port.S2) +ultrasonic_sensor=UltrasonicSensor(Port.S3) +color_sensor=ColorSensor(Port.S4) + + +#Fonction +def calibration(): + GYRO_CALIBRATION_LOOP_COUNT = 200 + global GYRO_OFFSET_FACTOR + gyro_minimum_rate, gyro_maximum_rate = 440, -440 + gyro_sum = 0 + for _ in range(GYRO_CALIBRATION_LOOP_COUNT): + gyro_sensor_value = gyro_sensor.speed() + gyro_sum += gyro_sensor_value + if gyro_sensor_value > gyro_maximum_rate: + gyro_maximum_rate = gyro_sensor_value + if gyro_sensor_value < gyro_minimum_rate: + gyro_minimum_rate = gyro_sensor_value + wait(5) + gyro_offset = gyro_sum / GYRO_CALIBRATION_LOOP_COUNT + return gyro_offset + +def regulation(vitesse_ref,direction): + + global psi, K, GYRO_OFFSET_FACTOR, temps_moyen_boucle, gyro_sensor, left_motor, right_motor, gyro_offset + global Kp1, Kp2, Kp3, Kp4, theta_ref, theta_ancien + + # Mesure des variables d'états + + # Calcul de psi et psi_d + + gyro_sensor_value = gyro_sensor.speed() + gyro_offset *= (1 - GYRO_OFFSET_FACTOR) + gyro_offset += GYRO_OFFSET_FACTOR * gyro_sensor_value + psi_d = gyro_sensor_value - gyro_offset + psi=psi/K + psi += psi_d * temps_moyen_boucle + + #Calcul de l'angle theta et de la vitesse angulaire des roues + + left_motor_angle = left_motor.angle() + right_motor_angle = right_motor.angle() + + theta_1=right_motor_angle + theta_2=left_motor_angle + + theta = (left_motor_angle+right_motor_angle)/2 + + #left_motor_rate = left_motor.speed() + #right_motor_rate = right_motor.speed() + + #theta_d = (left_motor_rate+right_motor_rate)/2 + + theta_d = (theta - theta_ancien)/temps_moyen_boucle + theta_ancien=theta + + #ev3.screen.print("theta =", theta) + #ev3.screen.print("theta_d =", theta_d) + #ev3.screen.print("psi =",psi) + #ev3.screen.print("psi_d=",psi_d) + + # Conversion en radian + + theta = K*theta + theta_d = K*theta_d + psi = K*psi + psi_d = K*psi_d + + # Erreur + + theta_ref= theta_ref + vitesse_ref*temps_moyen_boucle + erreur_theta= theta - theta_ref + + erreur_vitesse_theta= theta_d - vitesse_ref + + # Retour d'état + + retour = Kp1*theta + Kp2*psi + Kp3*theta_d + Kp4*psi_d + + retour = Kp1*erreur_theta + Kp2*psi + Kp3*erreur_vitesse_theta + Kp4*psi_d + + commande_moteur= -retour + + #if commande_moteur > 100: + #commande_moteur = 100 + #if commande_moteur < -100: + #commande_moteur = -100 + + left_motor.dc(commande_moteur/2+direction) + right_motor.dc(commande_moteur/2-direction) + + return + +def regulation_deviation(theta_diff): + global init, diff_actuel + theta_2 = left_motor.angle() + theta_1= right_motor.angle() + if init==0: + vitesse_ref=2 + diff_actuel=theta_2 - theta_1 + init=1 + direction=0.02*(theta_diff - (theta_2 - theta_1 - diff_actuel)) + return direction + +#Thread et fonction pour retourner la couleur détéctée + +#Initialisation de la couleur et du niveau de gris +coul=0 +gris=0 + +def couleur(): + global coul + global gris + while True: + c=color_sensor.rgb() + coul=couleur_rgb(c) + gris=(c[0]+c[1]+c[2])/3 + print(coul) + print(c) + wait(200) + +def couleur_rgb(c): + if c[0]<10 and c[1]<10 and c[2]<10: + return "noire" + elif c[0]<10 and c[1]<=10 and c[2]>=10: + if c[0]==7: + return 'blanc' + else: + return "bleu" + elif c[0]>10 and c[1]<10 and c[2]<10: + return "rouge" + elif c[0]<15 and c[1]>=27 and c[2]<15: + return "vert" + else: + return "blanc" + + +threading.Thread(target=couleur).start() + + +# Kp retour d'état + +Kp1=-20 +Kp2=-1700.2 +Kp3=-20 +Kp4=-130.5693 + + +#Initialisation d'un timer et d'un compteur pour déterminer le temps d'une boucle +timer = StopWatch() +nb_iter=0 + +#Compteur utilisé pour la résolution du labyrinthe +timer_global = StopWatch() + +#Période d'échantillonage +Ts=0.030 + +#Reinitialisation des codeurs des moteurs +left_motor.reset_angle(0) +right_motor.reset_angle(0) + +#Gain pour la conversion en radian +pi=3.1415 +K = pi/180 + +#Initialisation des variables pour la dérivation et l'intégration +psi=0 +theta_ref=0 +theta_ancien=0 + + +# Variables de référence +vitesse_ref=2 #rad/s, on commence par le suivi ligne +direction=0 + + +#Calibration du gyroscope + +GYRO_OFFSET_FACTOR = 0.0005 +gyro_offset=calibration() + +#Emission d'un bruit lorsque la calibration est fini + +ev3.speaker.beep() + +#ETAT DU ROBOT +suivi_ligne=1 +intersection=0 +init=0 +obstacle=0 + +#Pour les intersections +etape=0 +compteur_bleu=0 + +#POUR LE SUIVI LIGNE +BLACK = 4 +WHITE = 40 +seuil = (BLACK + WHITE) / 2 + +#Boucle de regulation + +timer_global.reset() + +while True: + + #Calcul du temps moyen d'une boucle + + if nb_iter == 0: + temps_moyen_boucle = Ts + timer.reset() + else: + temps_moyen_boucle = (timer.time() / 1000 /nb_iter) + nb_iter += 1 + + #Suivi ligne + + if suivi_ligne==1: + vitesse_ref=1.5 + #direction=regulation_deviation(0) + gain=0.1 + if coul=='noire': + gain=0.2 + direction=gain*(gris-seuil) + + + """if coul=='blanc': + vitesse_ref=0 + init=0""" + + if coul=="bleu": + compteur_bleu=compteur_bleu+1 + else: + compteur_bleu=0 + + if compteur_bleu==10: + etape+=1 + suivi_ligne=0 + intersection=1 + ev3.speaker.beep() + init=0 + direction=0 + theta_actuel=left_motor.angle() + tourner=0 + timer_global.reset() + devia=direction + + if coul=="vert": + suivi_ligne=0 + obstacle=1 + ev3.speaker.beep() + vitesse_ref=0 + init=0 + timer_global.reset() + + #Intersection + + if intersection==1: + if etape==3 or etape==5: + direction=-devia + else: + direction=11 + if (coul=="noire" or coul=="blanc") and timer_global.time()>1000: + suivi_ligne=1 + intersection=0 + #ev3.speaker.beep() + init=0 + timer_global.reset() + + + #Demi_tour + + if obstacle==1: + if timer_global.time()>1500: + direction=regulation_deviation(4*240) + if direction<3 and direction>-3: + obstacle=0 + vitesse_ref=2 + direction=0 + suivi_ligne=1 + init=0 + + regulation(vitesse_ref,direction) + + + wait(Ts*1000) + + + + + + + diff --git a/Files/stabilisation.py b/Files/stabilisation.py new file mode 100644 index 0000000000000000000000000000000000000000..2ab23e771bf4085f19309ccdd0987b001e2fded5 --- /dev/null +++ b/Files/stabilisation.py @@ -0,0 +1,179 @@ +#!/usr/bin/env pybricks-micropython + +from pybricks.hubs import EV3Brick +from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, + InfraredSensor, UltrasonicSensor, GyroSensor) +from pybricks.parameters import Port, Stop, Direction, Button, Color +from pybricks.tools import wait, StopWatch, DataLog +from pybricks.robotics import DriveBase +from pybricks.media.ev3dev import SoundFile, ImageFile +import threading + +# This program requires LEGO EV3 MicroPython v2.0 or higher. +# Click "Open user guide" on the EV3 extension tab for more information. + + +# Initialisation de la brique EV3 +ev3 = EV3Brick() + +# Initialisation des moteurs +left_motor = Motor(Port.D) +right_motor = Motor(Port.A) + +# Initialisation des capteurs +gyro_sensor = GyroSensor(Port.S2) +color_sensor = ColorSensor(Port.S4) + +#Fonction +def calibration(): + GYRO_CALIBRATION_LOOP_COUNT = 200 + global GYRO_OFFSET_FACTOR + gyro_minimum_rate, gyro_maximum_rate = 440, -440 + gyro_sum = 0 + for _ in range(GYRO_CALIBRATION_LOOP_COUNT): + gyro_sensor_value = gyro_sensor.speed() + gyro_sum += gyro_sensor_value + if gyro_sensor_value > gyro_maximum_rate: + gyro_maximum_rate = gyro_sensor_value + if gyro_sensor_value < gyro_minimum_rate: + gyro_minimum_rate = gyro_sensor_value + wait(5) + gyro_offset = gyro_sum / GYRO_CALIBRATION_LOOP_COUNT + return gyro_offset + +def regulation(vitesse_ref,deviation): + + global psi, K, GYRO_OFFSET_FACTOR, temps_moyen_boucle, gyro_sensor, left_motor, right_motor, gyro_offset + global Kp1, Kp2, Kp3, Kp4, theta_ref, theta_ancien + + + # Mesure des variables d'états + + # Calcul de psi et psi_d + + gyro_sensor_value = gyro_sensor.speed() + gyro_offset *= (1 - GYRO_OFFSET_FACTOR) + gyro_offset += GYRO_OFFSET_FACTOR * gyro_sensor_value + psi_d = gyro_sensor_value - gyro_offset + psi=psi/K + psi += psi_d * temps_moyen_boucle + + #Calcul de l'angle theta et de la vitesse angulaire des roues + + left_motor_angle = left_motor.angle() + right_motor_angle = right_motor.angle() + + theta = (left_motor_angle+right_motor_angle)/2 + + #left_motor_rate = left_motor.speed() + #right_motor_rate = right_motor.speed() + + #theta_d = (left_motor_rate+right_motor_rate)/2 + + theta_d = (theta - theta_ancien)/temps_moyen_boucle + theta_ancien=theta + + #ev3.screen.print("theta =", theta) + #ev3.screen.print("theta_d =", theta_d) + #ev3.screen.print("psi =",psi) + #ev3.screen.print("psi_d=",psi_d) + + # Conversion en radian + + theta = K*theta + theta_d = K*theta_d + psi = K*psi + psi_d = K*psi_d + + # Erreur + + theta_ref= theta_ref + vitesse_ref*temps_moyen_boucle + erreur_theta= theta - theta_ref + + erreur_vitesse_theta= theta_d - vitesse_ref + + # Retour d'état + + retour = Kp1*theta + Kp2*psi + Kp3*theta_d + Kp4*psi_d + + retour = Kp1*erreur_theta + Kp2*psi + Kp3*erreur_vitesse_theta + Kp4*psi_d + + commande_moteur= -retour + + #if commande_moteur > 100: + #commande_moteur = 100 + #if commande_moteur < -100: + #commande_moteur = -100 + + left_motor.dc(commande_moteur/2+deviation) + right_motor.dc(commande_moteur/2-deviation) + + return + + + + +# Kp retour d'état +Kp1=-20 +Kp2=-1700.2 +Kp3=-20 +Kp4=-130.5693 + +#Initialisation d'un timer et d'un compteur pour déterminer le temps d'une boucle +timer = StopWatch() +nb_iter=0 + +#Période d'échantillonage +Ts=0.030 + +#Reinitialisation des codeurs des moteurs +left_motor.reset_angle(0) +right_motor.reset_angle(0) + +#Gain pour la conversion en radian +pi=3.1415 +K = pi/180 + +#Initialisation des variables pour la dérivation et l'intégration +psi=0 +theta_ref=0 +theta_ancien=0 + +# Variables de référence +vitesse_ref=0 #rad/s +deviation=0 + +#Calibration du gyroscope + +GYRO_OFFSET_FACTOR = 0.0005 +gyro_offset=calibration() + +#Emission d'un bruit lorsque la calibration est fini + +ev3.speaker.beep() + +#Boucle de regulation + + +while True: + + #Calcul du temps moyen d'une boucle + + if nb_iter == 0: + temps_moyen_boucle = Ts + timer.reset() + else: + temps_moyen_boucle = (timer.time() / 1000 /nb_iter) + nb_iter += 1 + + + regulation(vitesse_ref,deviation) + + wait(Ts*1000) + + + + + + + diff --git a/README.md b/README.md index f1956b729ff2d482984d0877d9113d1511e7fd0c..8a8d808c4ec9508340cca6c85aabd51203348d5e 100644 --- a/README.md +++ b/README.md @@ -1,92 +1,11 @@ # PFE Segway Intelligent +Ce projet de fin d'études consiste à développer un programme permettant à un robot lego Segway +de parcourir de manière autonome un labyrinthe muni d’indications colorées. +Ce github comprend un programme permettant au robot de se stabiliser debout et un programme permettant +au robot de naviguer le long du labyrinthe. -## Getting started +Chaque programme, une fois exécuté sur le robot, comprend une phase d'initialisation durant laquelle il faut +tenir le robot debout verticalement. Un bip sonore marque la fin de cette phase. -To make it easy for you to get started with GitLab, here's a list of recommended next steps. - -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://gitlab.univ-lorraine.fr/mecirdi1u/pfe-segway-intelligent.git -git branch -M main -git push -uf origin main -``` - -## Integrate with your tools - -- [ ] [Set up project integrations](https://gitlab.univ-lorraine.fr/mecirdi1u/pfe-segway-intelligent/-/settings/integrations) - -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. - -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. - -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. - -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. - -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. - -## Contributing -State if you are open to contributions and what your requirements are for accepting them. - -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. - -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. - -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. - -## License -For open source projects, say how it is licensed. - -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.