GPIO_InitTypeDef GPIO_RGB_InitStruct; TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig; __HAL_RCC_GPIOB_CLK_ENABLE(); // PB7 - TIMER17 GPIO_RGB_InitStruct.Pin = GPIO_PIN_7; GPIO_RGB_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_RGB_InitStruct.Pull = GPIO_NOPULL; GPIO_RGB_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_RGB_InitStruct.Alternate = GPIO_AF2_TIM17; HAL_GPIO_Init(GPIOB, &GPIO_RGB_InitStruct); __HAL_RCC_TIM17_CLK_ENABLE(); // Init TIMER17 for 800KHz frequency // 48MHz/(19+1)/(2+1) = 800Khz TIMER_RGB_InitStruct.Instance = TIM17; TIMER_RGB_InitStruct.Init.Prescaler = 0; TIMER_RGB_InitStruct.Init.CounterMode = TIM_COUNTERMODE_UP; TIMER_RGB_InitStruct.Init.Period = 59; TIMER_RGB_InitStruct.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; HAL_TIM_Base_Init(&TIMER_RGB_InitStruct); HAL_TIM_PWM_Init(&TIMER_RGB_InitStruct); // Output Compare Configuration TIMER_RGB_OC_InitStruct.OCMode = TIM_OCMODE_PWM1; TIMER_RGB_OC_InitStruct.OCIdleState = TIM_OCIDLESTATE_RESET; TIMER_RGB_OC_InitStruct.OCNIdleState = TIM_OCNIDLESTATE_RESET;// ATTENTION ! TIM_OCNIDLESTATE_RESET TIMER_RGB_OC_InitStruct.Pulse = 1;// PWM 0 % TIMER_RGB_OC_InitStruct.OCPolarity = TIM_OCPOLARITY_HIGH; TIMER_RGB_OC_InitStruct.OCNPolarity = TIM_OCNPOLARITY_HIGH;// ATTENTION ! TIM_OCNPOLARITY_HIGH TIMER_RGB_OC_InitStruct.OCFastMode = TIM_OCFAST_DISABLE; HAL_TIM_PWM_ConfigChannel(&TIMER_RGB_InitStruct, &TIMER_RGB_OC_InitStruct, TIM_CHANNEL_1); HAL_TIMEx_PWMN_Start(&TIMER_RGB_InitStruct,TIM_CHANNEL_1);// ATTENTION HAL_TIMEx_PWMN_Start
Author: macerobotics
Promotion sur le robot MRPiZ
Promotion sur le robot MRPiZ version starter de -10% du 30/04/2018 au 14/05/2018.
Boutique : https://shop.macerobotics.com/
Avancer vers un but avec gestion des obstacles
Un programme python pour faire avancer le robot MRPiZ vers un but avec la prise en compte des obstacles.
#!/usr/bin/env python # Mace Robotics from mrpiZ_lib import * import time # erreur odometrie error_odo = 20 # sleep 2 secondes time.sleep(2) # but en millimetre goal_robot = 200 # activer controle controlEnable() # lecture du capteur avant sensor_p3 = proxSensor(3) # lecture position du robot (axe X) position_robot = robotPositionX() distance_robot = goal_robot while (position_robot < goal_robot-error_odo): forwardmm(10,distance_robot)# avancer vers le but sensor_p3 = proxSensor(3)# lecture capteur time.sleep(0.2) # pause 200 ms position_robot = robotPositionX()# lecture position robot # si obstacle if (sensor_p3 < 100): stop()# arret du robot distance_robot = goal_robot - position_robot # erreur sur la distance #end
Mise à jour firmware MRPiZ
MRPiZ – odometry
import sys from mrpiZ_lib import * import time time.sleep(5) controlEnable() forward_mm(10,220) turnLeft_degree(10,90) forward_mm(10,170) turnLeft_degree(10,90) forward_mm(10,170) turnRight_degree(10,90) forward_mm(10,290) turnRight_degree(10,90) forward_mm(10,375) turnRight_degree(10,90) forward_mm(10,450)
Envoyer des commandes SSH avec paramiko
Un script pour envoyer des commandes SSH avec paramiko.
Ce script permet de contrôler en SSH le robot MRPiZ à partir d’un PC.
#!/usr/bin/env python import sys, paramiko import time hostname = '192.168.42.1' # IP du robot MRPiZ password = 'raspberry' username = 'pi' port = 22 command = 'echo "#MF,30!" > /dev/ttyAMA0' ##################################################################################"" ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.load_system_host_keys() ssh.connect(hostname, port=port, username=username, password=password) ssh.exec_command(command)
Dance de robots MRPiZ
La gestion des robots en parallèle est gérer par fabric (www.fabfile.org). Un outil SSH permettant d’envoyer plusieurs commandes SSH en parallèle. Chaques robots MRPiZ est identifié par une adresse IP unique:
- IP du robot n°1: 192.168.1.41
- IP du robot n°2: 192.168.1.37
- IP du robot n°3: 192.168.1.47
Le script de contrôle des robots :
#!/usr/bin/env python
import fabric
from fabric.api import run, env, task
from fabric.api import *
import time
env.hosts = ['192.168.1.41', '192.168.1.37', '192.168.1.47']
env.user = 'pi'
env.password = 'raspberry'
@parallel
def hello():
run('python /home/pi/MRPiZ/Dance.py')
Le script python pour les robots :
#!/usr/bin/env python import sys from mrpiZ_lib import * import time controlEnable() while 1: forward_mm(13,120) back_mm(11,100) forward_mm(11,100) turnRight_degree(25,360) time.sleep(1) turnRight_degree(15,90) turnLeft_degree(15,180) turnRight_degree(15,90) back_mm(12,90)
Partie 1 : OpenCV et le robot MRPiZ
Ce tutoriel permet de faire du traitement d’images avec le robot MRPiZ en langage Python.
Le matériel nécessaire pour le tutoriel :
- Un robot MRPiZ
- Un support caméra pour le robot MRPiZ
- Une caméra Raspberry Pi 8MP
Installation
La première étape est d’installer la camera sur la carte Raspberry Pi :
- Installation de la camera Raspberry Pi 8MP : ici
- Mise à jour du système:
sudo apt-get update
sudo apt-get upgrade
- Installation de la bibliothèque de traitement d’image OpenCv :
sudo apt-get install python-opencv
- Installation de imutils:
sudo easy_install pip
sudo pip install imutil
sudo apt-get install python-picamera python3-picamera
Une simple photo
Prendre une simple photo avec la camera et l’enregistrer dans une image, voic
#!/usr/bin/python import picamera camera = picamera.PiCamera() # initialisation de la resolution camera.resolution = (1920, 1080) # capture d'une image camera.capture('image.jpg')
Simple photo
Lecture d’une image
Un script pour la lecture d’une image avec OpenCv :
#!/usr/bin/python import picamera import cv2 camera = picamera.PiCamera() # initialisation de la resolution camera.resolution = (100, 100) # capture d'une image camera.capture('image.jpg') image = cv2.read('image.jpg') print image
Conversion en HSV
Conversion colorimétrique.
HSV : Hue Saturation Value
#!/usr/bin/python import picamera import cv2 camera = picamera.PiCamera() # initialisation de la resolution camera.resolution = (1920, 1080) # capture d'une image camera.capture('image.jpg') # lecture de l'image frame = cv2.imread('image.jpg') # conversion RGB en HSV de l'image hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # enregistrer image hsv cv2.imwrite('image_hsv.png', hsv)
image HSV
Fin de la première partie.
MRPiZ obstacle avoidance
Exemple de gestion des obstacles avec le robot MRPiZ et 5 capteurs de distance ToF:
#!/usr/bin/python # Mace Robotics (www.macerobotics.com) # Author : Mace Robotics # Exemple : alarm # Date : 30/12/2015 # Version : 0.1 from mrpiZ_lib import * import time, sys import random #main program print "Exemple : Obstacle avoidance" limit = 20 speed = 45 speedSlow = 30 try: while 1: p1 = proxSensor(1) p2 = proxSensor(2) p3 = proxSensor(3) p4 = proxSensor(4) p5 = proxSensor(5) #----------------- if((p1 < limit)or(p2 < limit)) and (p4 > limit): turnRight(speed) time.sleep(0.5) r = random.randint(0,2) if (r == 0): back(speedSlow) elif ((p4 < limit)or(p5 < limit)) and (p2 > limit): turnLeft(speed) time.sleep(0.5) r = random.randint(0,2) if (r == 1): back(speedSlow) elif (p3 < limit): r = random.randint(0,3) if (r == 0): back(speedSlow) time.sleep(0.6) elif (r == 1): back(speedSlow) time.sleep(0.9) turnRight(35) time.sleep(0.5) elif (r == 2): back(speedSlow) time.sleep(0.7) turnLeft(35) time.sleep(0.5) elif (r == 3): back(speedSlow) elif (p2 < limit) and (p4 < limit): turnLeft(35) time.sleep(1) else: forward(speed) except: print "Fin programme" stop() print "stop" exit()
MRPiZ en Java
Dans ce tutorial vous allez apprendre à programmer le robot MRPiZ en langage Java.
Remarque : le robot MRPiZ doit etre connecté à internet.
Installation
- Mise à jour et installation de java :
sudo apt-get update && sudo apt-get install oracle-java7-jdk
- Installation de la librairie Pi4J pour l’accès hardware de la Raspberry Pi:
curl -s get.pi4j.com | sudo bash
Librairie
La librairie java est disponible sur le Github : ici
Exemple
Voici un exemple de programme (Exemple1.java):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import java.io.IOException; import java.util.Date; import java.util.concurrent.TimeUnit; public class Exemple1 { /****************/ public static void main(String args[]) throws InterruptedException, IOException { float tension_batterie; float distance_sensor1; float distance_sensor2; MRPiZ robot = new MRPiZ(); // Lecture de la tension de la batterie tension_batterie = robot.proxSensor(1); System.out.println("Batterie = " + tension_batterie); // avancer le robot pendant 1 seconde robot.forward(30); TimeUnit.SECONDS.sleep(2); // reculer le robot pendant 1 seconde robot.back(30); TimeUnit.SECONDS.sleep(2); // tourner à droite robot.turnRight(30); TimeUnit.SECONDS.sleep(1); // arret du robot robot.stop(); // Lecture du capteur de proximitè n°1 et n°2 distance_sensor1 = robot.proxSensor(1); distance_sensor2 = robot.proxSensor(2); // afficher les valeurs System.out.println("Sensor 1 = " + distance_sensor1); System.out.println("Sensor 2 = " + distance_sensor2); } } |
- Compilation du programme :
javac -classpath .:classes:/opt/pi4j/lib/'*' -d . Exemple1.java
- Exécution du programme :
sudo java -classpath .:classes:/opt/pi4j/lib/'*' Exemple1