import cv2 import mediapipe as mp import numpy as np import time import RPi.GPIO as GPIO

import RPi.GPIO as GPIO

varialbe define

total_step = 0 step_num = 0 before_total_step = 0 counter = 0 lastcounter = 0 lastStateCLK = 1 lastStateDT = 1 dir = 0 ySensing = 0 IRSignalRight = 0 IRSignalLeft = 0 rotationMode = "None" text = "stop"

mode define

rotationMode = "stop" encoderOnOff = "Off" visionOnOff = "On" IROnOff = "On" workMode = "None"

#channel define

DIR = 22 STP = 27 EN = 0 SW = 13 DT = 19 CLK = 26 IRChannel = 5 visionChannel = 6 IR_R = 21 IR_L = 20

signal setup

GPIO.setmode(GPIO.BCM) GPIO.setup(IR_R, GPIO.IN) GPIO.setup(IR_L, GPIO.IN) GPIO.setup(STP, GPIO.OUT) GPIO.setup(DIR, GPIO.OUT) GPIO.setup(CLK, GPIO.IN) GPIO.setup(DT, GPIO.IN) GPIO.setup(SW, GPIO.IN) GPIO.setup(IRChannel, GPIO.IN) GPIO.setup(visionChannel, GPIO.IN)

#define function

def IRSensing() :

global step_num
global dir
global IRNoSensingCount
global IRdeltaTime
global IRSignalRight
global IRSignalLeft

if GPIO.input(IR_R) == 0 :
    IRSignalRight = 0
    IRNoSensingCount = 3
if GPIO.input(IR_L) == 0 :
    IRSignalLeft = 0
    IRNoSensingCount = 3

if IRNoSensingCount > 0  :
    step_num = 50
else :
    step_num = 0

if GPIO.input(IR_R) == 0 and GPIO.input(IR_L) == 1 :
    dir = 0
elif GPIO.input(IR_R) == 1 and GPIO.input(IR_L) == 0 :
    dir = 1

if GPIO.input(IR_R) == 1 and GPIO.input(IR_L) == 1 and IRdeltaTime > 0.05 :
    IRNoSensingCount = IRNoSensingCount - 1

def Drive(stepVariable, directionVariable, timeInterval) :

global total_step

GPIO.output(DIR, directionVariable)
i=0

while True:

    GPIO.output(STP, GPIO.HIGH)
    time.sleep(timeInterval)
    GPIO.output(STP, GPIO.LOW)
    time.sleep(timeInterval)

    i=i+1

    if i > stepVariable :
        break

if directionVariable == GPIO.HIGH :
    total_step = total_step + stepVariable
else :
    total_step = total_step - stepVariable

def AccelDrive(stepVariable) :

global total_step
global dir

GPIO.output(DIR, dir)
i=0

while True:

    GPIO.output(STP, GPIO.HIGH)
    time.sleep(0.0001*i)
    GPIO.output(STP, GPIO.LOW)
    time.sleep(0.0001*i)

    i=i+1

    if i > stepVariable :
        break

if dir == 0 :
    total_step = total_step + stepVariable
else :
    total_step = total_step - stepVariable

# please check minsung~

def DriveTest(stepVariable, directionVariable) :

global total_step

if directionVariable == 0 :
    total_step = total_step + stepVariable
else :
    total_step = total_step - stepVariable