mimi6060
Posts: 22
Joined: Sun Apr 19, 2015 10:56 pm

servomoteur

Wed Aug 12, 2015 2:17 am

bonjour,

j'ai acheter le servomoteur suivant : https://www.pololu.com/product/1248 .
Et j'aurais besoin besoin de votre aide pour le controler pouvez vous m'aider pour les branchements .
ET pour le code python a utiliser

merci d'avance Michel

ToOnS
Posts: 492
Joined: Sat Mar 23, 2013 10:29 am

Re: servomoteur

Wed Aug 12, 2015 1:59 pm

salut Michel , le cable noir sur GND le rouge sur +5 (ou alors le noir sur une autre alim ET sur le GND du RPI et le rouge sur l'autre alim) , le jaune celui qui recoit les impulsions sur un GPIO , pour le code tu peux utiliser celui la : viewtopic.php?p=352028#p352028 , bon c'est pour 4 moteurs (des esc mais un servo moteur ca marche pareil) et imagine que speed dans ton cas ca veut dire "angle" et que c'est sur 100% a la place de 360°

mimi6060
Posts: 22
Joined: Sun Apr 19, 2015 10:56 pm

Re: servomoteur

Wed Aug 12, 2015 2:21 pm

le code suivant ?
je peux brancher a n'importe quel port GPIO les 2 serveau moteur?

Code: Select all

 Using PWM with RPi.GPIO - requires RPi.GPIO 0.5.2a or higher

import RPi.GPIO as GPIO       # always needed with RPi.GPIO
from time import sleep        # pull in the sleep function from time module

GPIO.setmode(GPIO.BCM)        # choose BCM or BOARD numbering schemes. I use BCM

GPIO.setup(22, GPIO.OUT)      # set GPIO 22 as output for Motor 1
GPIO.setup(23, GPIO.OUT)      # set GPIO 23 as output for Motor 2
GPIO.setup(24, GPIO.OUT)      # set GPIO 24 as output for Motor 3
GPIO.setup(25, GPIO.OUT)      # set GPIO 25 as output for Motor 4

Frequency = 50             # Hz
Percent_Duty_Cycle_Mini = Frequency/10    # 1ms (Speed = 0%)
 
Motor1 = GPIO.PWM(22, Frequency)   # create object Motor1 for PWM on port 22 at "Frequency" Hertz
Motor2 = GPIO.PWM(23, Frequency)   # create object Motor2 for PWM on port 23 at "Frequency" Hertz
Motor3 = GPIO.PWM(24, Frequency)   # create object Motor3 for PWM on port 24 at "Frequency" Hertz
Motor4 = GPIO.PWM(25, Frequency)   # create object Motor4 for PWM on port 25 at "Frequency" Hertz

Motor1.start(Percent_Duty_Cycle_Mini)   # start at 0% Speed
Motor2.start(Percent_Duty_Cycle_Mini)   # start at 0% Speed
Motor3.start(Percent_Duty_Cycle_Mini)   # start at 0% Speed
Motor4.start(Percent_Duty_Cycle_Mini)   # start at 0% Speed
sleep(1)            # wait 1s for Motors arming

def MotorSpeed( arg1, arg2 ):       # arg1 is Motor number , arg2 is Speed percent
   Speed = Percent_Duty_Cycle_Mini + arg2 * Percent_Duty_Cycle_Mini / 100
   print "Motor %d at %d % (%f % of Duty Cycle , %f ms)" % (arg1, arg2 , Speed , Speed * 1/Frequency/100)
   if arg1 == 1:
      Motor1.ChangeDutyCycle(Speed)
   if arg1 == 2:
      Motor2.ChangeDutyCycle(Speed)
   if arg1 == 3:
      Motor3.ChangeDutyCycle(Speed)
   if arg1 == 4:
      Motor4.ChangeDutyCycle(Speed)
   return;

MotorSpeed( 1 , 5)          # Motor 1 at 5% of Speed
MotorSpeed( 2 , 10)          # Motor 2 at 10% of Speed
MotorSpeed( 3 , 20)          # Motor 3 at 20% of Speed
MotorSpeed( 4 , 30)          # Motor 3 at 30% of Speed

# now the fun starts, we'll vary the Speed to Motors

try:
    while True:
        for i in range(1,5):         # Motor1 to Motor4
       for j in range (0,101):     # Speed 0% to 100%
                MotorSpeed( i , j)
                sleep(0.5)
            break
            MotorSpeed( i , 0)       # Motor at 0%

except KeyboardInterrupt:
    Motor1.stop()                  # stop the Motor1 PWM output
    Motor2.stop()                    # stop the Motor2 PWM output
    Motor3.stop()                  # stop the Motor3 PWM output
    Motor4.stop()                    # stop the Motor4 PWM output

    GPIO.cleanup()                # clean up GPIO on CTRL+C exit

mimi6060
Posts: 22
Joined: Sun Apr 19, 2015 10:56 pm

Re: servomoteur

Thu Aug 13, 2015 1:06 pm

bonjour ,

j'ai essayer le code suivant ren ne bouge :

Code: Select all

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)

GPIO.setup(22, GPIO.OUT)


Frequency = 50 #HZ
Percent_Duty_Cycle_Mini = Frequency/10 # 1ms(Speed = 0%)

Motor1 = GPIO.PWM(22,Frequency)

Motor1.start(Percent_Duty_Cycle_Mini)

Motor1.ChangeDutyCycle(4)


GPIO.cleanup()







ToOnS
Posts: 492
Joined: Sat Mar 23, 2013 10:29 am

Re: servomoteur

Thu Aug 13, 2015 11:32 pm

oui tu peux brancher 2 servo sur n'importe quel gpio ,
essai en remplacant :

Code: Select all

Motor1.ChangeDutyCycle(4)
par :

Code: Select all

def MotorSpeed( arg1, arg2 ):       # arg1 is Motor number , arg2 is Speed percent
   Speed = Percent_Duty_Cycle_Mini + arg2 * Percent_Duty_Cycle_Mini / 100
   print "Motor %d at %d % (%f % of Duty Cycle , %f ms)" % (arg1, arg2 , Speed , Speed * 1/Frequency/100)
   if arg1 == 1:
      Motor1.ChangeDutyCycle(Speed)
   if arg1 == 2:
      Motor2.ChangeDutyCycle(Speed)
   if arg1 == 3:
      Motor3.ChangeDutyCycle(Speed)
   if arg1 == 4:
      Motor4.ChangeDutyCycle(Speed)
   return;

MotorSpeed( 1 , 0)          # Motor 1 at 0% of Speed
sleep(1)
MotorSpeed( 1 , 25)          # Motor 1 at 25% of Speed
sleep(1)
MotorSpeed( 1 , 50)          # Motor 1 at 50% of Speed
sleep(1)
MotorSpeed( 1 , 75)          # Motor 1 at 75% of Speed
sleep(1)
MotorSpeed( 1 , 100)          # Motor 1 at 100% of Speed
4 est de toute facon trop petit pour Motor1.ChangeDutyCycle(4) , le minimum (pour avoir le servo a fond a gauche) est 5 (ce qui donne un signal de 1ms) , le maxi est 10 (a fond a droite) ce qui donne un signal de 2ms , au milieu 7.5 (pour un signal de 1.5ms) et etc , la fonction MotorSpeed calculera ca pour toi , si ca bouge toujours pas et que l'alim du servo est branchée sur le RPI c'est que le RPI fourni pas assez de courant

mimi6060
Posts: 22
Joined: Sun Apr 19, 2015 10:56 pm

Re: servomoteur

Fri Aug 14, 2015 3:10 am

j'ai le code suivant :

Code: Select all

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)

GPIO.setup(1, GPIO.OUT)


Frequency = 50 #HZ
Percent_Duty_Cycle_Mini = Frequency/10 # 1ms(Speed = 0%)

Motor1 = GPIO.PWM(1,Frequency)

Motor1.start(Percent_Duty_Cycle_Mini)

def MotorSpeed( arg1, arg2 ):       # arg1 is Motor number , arg2 is Speed percent
   Speed = Percent_Duty_Cycle_Mini + arg2 * Percent_Duty_Cycle_Mini / 100
#   print "Motor %d at %d % (%f % of Duty Cycle , %f ms)" % (arg1, arg2 , Speed , Speed * 1/Frequency/100)
   if arg1 == 1:
      Motor1.ChangeDutyCycle(Speed)
   if arg1 == 2:
      Motor2.ChangeDutyCycle(Speed)
   if arg1 == 3:
      Motor3.ChangeDutyCycle(Speed)
   if arg1 == 4:
      Motor4.ChangeDutyCycle(Speed)
   return;

MotorSpeed( 1 , 0)          # Motor 1 at 0% of Speed
sleep(1)
MotorSpeed( 1 , 25)          # Motor 1 at 25% of Speed
sleep(1)
MotorSpeed( 1 , 50)          # Motor 1 at 50% of Speed
sleep(1)
MotorSpeed( 1 , 75)          # Motor 1 at 75% of Speed
sleep(1)
MotorSpeed( 1 , 100)          # Motor 1 at 100% of Speed

GPIO.cleanup()



mais rien ne tourne

ToOnS
Posts: 492
Joined: Sat Mar 23, 2013 10:29 am

Re: servomoteur

Fri Aug 14, 2015 12:02 pm

ca devrait , as tu bien branché le cable jaune sur le bon GPIO ? (utilise le 22 au depart pour les tests car c'est le meme pour toutes les versions de RPI : http://raspi.tv/wp-content/uploads/2014 ... inouts.png et de preference des GPIO jaunes)
le print si il est decommenté il t'affiche bien des ms entre 1 et 2 (et un speed entre 5 et 10) ?
as tu essayé en branchant le cable rouge et le noir sur autre chose que le RPI pour fournir le courant ? (en gardant quand meme en plus le noir sur un gnd du RPI) ?

mimi6060
Posts: 22
Joined: Sun Apr 19, 2015 10:56 pm

Re: servomoteur

Fri Aug 14, 2015 3:22 pm

oui je suis brancher sur les bons pins .
j'ai trouver un code qui me les fais bouger donc je vais modifier le code suivant :

merci quand meme :)

Code: Select all

# Author: Ingmar Stapel 
# Usage: Servo test program
# URL: http://www.raspberry-pi-car.com/top-story-en/raspberry-pi-controlling-servo-motors/7028
# Version: 0.1 beta

import RPi.GPIO as GPIO
import time
import os

GPIO.setwarnings(False)
# Set the layout for the pin declaration
GPIO.setmode(GPIO.BOARD)
# The pin 11 is the out pin for the PWM signal for the servo.
GPIO.setup(11, GPIO.OUT)

# menu info
print "l = move to the left"
print "r = move to the right"
print "m = move to the middle"
print "t = test sequence"
print "q = stop and exit"

while True:
	# Now we will start with a PWM signal at 50Hz at pin 11. 
	# 50Hz should work for many servos very will. If not you can play with the frequency if you like.
	Servo = GPIO.PWM(11, 50)						

	# This command sets the left position of the servo
	Servo.start(2.5)

	# Now the program asks for the direction the servo should turn.
	input = raw_input("Selection: ") 

	# You can play with the values.
	# 7.5 is in most cases the middle position
	# 12.5 is the value for a 180 degree move to the right
	# 2.5 is the value for a -90 degree move to the left
	if(input == "t"):
		print "move to the center position:"
		Servo.ChangeDutyCycle(7.5)
		time.sleep(1)
		print "move to the right position:"
		Servo.ChangeDutyCycle(12.5)
		time.sleep(1)
		print "move to the left position:"
		Servo.ChangeDutyCycle(2.5)
		time.sleep(1)
		# this stops the PWM signal
		print "Move back to start position."
		Servo.stop()

	# direction right
	if(input == "r"):

		# how many steps should the move take.
		steps = raw_input("steps (1 - 10): ") 
		print steps, "steps to the right"
		stepslength = 12.5 / int(steps)
		for Counter in range(int(steps)):
			Servo.ChangeDutyCycle(stepslength * (Counter + 1))
			print stepslength * (Counter + 1)
			time.sleep(0.5)
			
		time.sleep(1)	
		# PWM stop
		print "Move back to start position."
		Servo.stop()

	# move to the center position
	elif(input == "m"):
		print "Move back to the center position."
		Servo.start(7.5)
		time.sleep(1)
		# PWM stop
		print "Move back to start position."
		Servo.stop()
	
	# move to the left
	elif(input == "l"):
		print "Move  to the max right position and then to the left position."
		Servo.start(12.5)
		# how many steps...
		steps = raw_input("steps (1 - 10): ") 
		print steps, "steps to the right"
		stepslength = 12.5 / int(steps)
		for Counter in range(int(steps)):
			Servo.ChangeDutyCycle(12.5 - (stepslength * (Counter + 1)))
			print (12.5 - (stepslength * (Counter + 1)))
			time.sleep(0.5)
		
		time.sleep(1)
		# PWM stop
		print "Move back to start position."
		Servo.stop()
	
	# close program
	elif(input == "q"):
		print "stop the program and exit......"
		os._exit(1)
		Servo.stop()
		GPIO.cleanup()
		
	# input not valid
	else:
		print "input not valid!"


mimi6060
Posts: 22
Joined: Sun Apr 19, 2015 10:56 pm

Re: servomoteur

Sat Aug 15, 2015 8:10 pm

lorsque je lance le code mon rasperry s'étient tout seul c'est normale ?

jareczek
Posts: 170
Joined: Tue Jul 23, 2013 2:45 pm
Location: Auvergne

Re: servomoteur

Fri Aug 21, 2015 1:07 pm

mimi6060 wrote:lorsque je lance le code mon rasperry s'étient tout seul c'est normale ?
Non.
Schéma du cablage.
Sauf erreur ou omission . . .

Return to “Français”