Categories
Software Config

Motors control

Tonight I tested the control of steppers and L298n board. Everything seems to work well with the test code I grabbed from the internet.

This I a big step forward in this project as we now know the hardware is working well. There will, of course, still be a lot of work to configure LIDAR and computer vision but it is s step forward.

Here is the code I tested.

#!/usr/bin/env python
# -*- coding: latin-1 -*-

# ----------------------------------------------------------------------
#  ZumoL298.py - Contrôle d'un Pont-H L298 avec Raspberry-Pi
#  
#  Tutoriel complet: 
#     http://wiki.mchobby.be/index.php?title=Rasp-Hack-L298 
#
#  Initialement conçu pour le chassis Zumo 
#     http://shop.mchobby.be/product.php?id_product=447
#  Mais fonctionne parfaitement avec de nombreuses plateformes(cfr tuto)
# ----------------------------------------------------------------------
#  Ecrit par Meurisse D. pour MCHobby.be ( shop.mchobby.be )
#  Licence: CC-BY-SA  - MCHobby.be vente de kit et composant
# ----------------------------------------------------------------------
# 9 jun 2014 - Dom - Création

import RPi.GPIO as GPIO
import time
import sys

GPIO.setmode(GPIO.BCM)

#--- Cablage --------------------
MOTORA_IN1 = 25 # GPIO 25
MOTORA_IN2 = 24 # GPIO 24
MOTORA_ENABLE = 4 # GPIO 4 - Activation moteur A
MOTORB_IN3 = 23 # GPIO 23
MOTORB_IN4 = 18 # GPIO 18
MOTORB_ENABLE = MOTORA_ENABLE # aussi utiliser GPIO 4 pour commander moteur B

#--- Initialisation -------------
# GPIO.setwarnings(FALSE)
GPIO.setmode(GPIO.BCM)
GPIO.setup( MOTORA_IN1, GPIO.OUT )
GPIO.setup( MOTORA_IN2, GPIO.OUT )
GPIO.setup( MOTORB_IN3, GPIO.OUT )
GPIO.setup( MOTORB_IN4, GPIO.OUT )
GPIO.setup( MOTORA_ENABLE, GPIO.OUT )
if( MOTORB_ENABLE != MOTORA_ENABLE ):
	GPIO.setup( MOTORB_ENABLE, GPIO.OUT )

# Assure l'arret moteur
GPIO.setup( MOTORA_ENABLE, GPIO.LOW )
if( MOTORB_ENABLE != MOTORA_ENABLE ):
	GPIO.setup( MOTORB_ENABLE, GPIO.LOW )

# --- Controle du L298 --------------------------
# Sens de rotation du moteur
SENS_AVANT = 1
SENS_ARRIERE = 2
SENS_ARRET = 3

def Desactiver():
	""" Désactivation des pont-H """
	GPIO.output( MOTORA_ENABLE, GPIO.LOW )
	if( MOTORB_ENABLE != MOTORA_ENABLE ):
		GPIO.output( MOTORB_ENABLE, GPIO.LOW )

def Activer():
	""" Activation des pont-H """
	GPIO.output( MOTORA_ENABLE, GPIO.HIGH )
	if( MOTORB_ENABLE != MOTORA_ENABLE ):
		GPIO.output( MOTORB_ENABLE, GPIO.HIGH )

def MarcheMotorA( sens ):
	""" Controle le sens de Marche pour Moteur A """
	if( sens == SENS_AVANT ):
		GPIO.output( MOTORA_IN1, GPIO.HIGH )
		GPIO.output( MOTORA_IN2, GPIO.LOW )
	elif( sens == SENS_ARRIERE ):
		GPIO.output( MOTORA_IN1, GPIO.LOW )
		GPIO.output( MOTORA_IN2, GPIO.HIGH )
	elif( sens == SENS_ARRET ):
		GPIO.output( MOTORA_IN1, GPIO.LOW )
		GPIO.output( MOTORA_IN2, GPIO.LOW )

def MarcheMotorB( sens ):
	"""Controle le sens de Marche pour Moteur B """
	if( sens == SENS_AVANT ):
		GPIO.output( MOTORB_IN3, GPIO.HIGH )
		GPIO.output( MOTORB_IN4, GPIO.LOW )
	elif( sens == SENS_ARRIERE ):
		GPIO.output( MOTORB_IN3, GPIO.LOW )
		GPIO.output( MOTORB_IN4, GPIO.HIGH )
	elif( sens == SENS_ARRET ):
		GPIO.output( MOTORB_IN3, GPIO.LOW )
		GPIO.output( MOTORB_IN4, GPIO.LOW ) 

def Avant():
	""" Marche avant toute """
	print( "Marche Avant" )
	Desactiver()
	MarcheMotorA( SENS_AVANT )
	MarcheMotorB( SENS_AVANT )
	Activer()

def Arriere():
	""" Marche arrière toute """
	print( "Marche Arrière" )
	Desactiver()
	MarcheMotorA( SENS_ARRIERE )
	MarcheMotorB( SENS_ARRIERE )
	Activer()

def TournerDroite():
	""" Fait tourner à droite """
	print( "Tourner à droite" )
	Desactiver()
	MarcheMotorA( SENS_ARRIERE )
	MarcheMotorB( SENS_AVANT )
	Activer()

def TournerGauche():
	""" Fait tourner a gauche """
	print( "Tourner à gauche" )
	Desactiver()
	MarcheMotorA( SENS_AVANT )
	MarcheMotorB( SENS_ARRIERE )
	Activer()
		
def Stop():
	""" Arret """
	print "Arret"
	Desactiver()
	MarcheMotorA( SENS_ARRET )
	MarcheMotorB( SENS_ARRET )
	Activer()

if __name__ == '__main__':
	Avant()
	time.sleep( 5 )
	TournerDroite()
	time.sleep( 3 )
	Avant()
	time.sleep( 5 )
	TournerGauche()
	time.sleep( 3 )
	Arriere()
	time.sleep( 5 )
	Stop()