-1

I am writing a basic code for a robot to follow the blue color and whenever I am running the code it gives that out put

the y is :0
the x is :0
moving to the right
moving forward
Segmentation fault

my python code is the following

#!/usr/bin/env python
import RPi.GPIO as GPIO
from time import sleep
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import numpy as np

#initializing thre servo pins to be used on the rasberrypi
servo1pin = 2
servo2pin = 3

#GPIO settings for the following project
GPIO.setmode(GPIO.BCM)
GPIO.setup (servo1pin, GPIO.OUT)
GPIO.setup (servo2pin, GPIO.OUT)

# initialzing the camera settings and start the video streaming 
camera = PiCamera()# making an object of the PiCamera Class
camera.rotation=180
camera.resolution = (640, 480) # resolution of the video to be used 
camera.framerate = 50 # at what speed the camera will stream videos
camera.hflip = True
rawCapture = PiRGBArray(camera, size=(640, 480))
time.sleep(0.1)



#these are the functions to control the robot movement 
def motorright():
    print("moving to the right")
    servo1 = GPIO.PWM(servo1pin, 50)
    servo2 = GPIO.PWM (servo2pin, 50)
    servo1.start(10.5)
    servo2.start(10.5)
    time.sleep(2)
    servo1.stop()
    servo2.stop()

def motorforward():
    print("moving forward")
    servo1 = GPIO.PWM(servo1pin, 50)
    servo2 = GPIO.PWM (servo2pin, 50)
    servo1.start(4.5)
    servo2.start(10.5)
    time.sleep(2)
    servo1.stop()
    servo2.stop()

def motorbackward():
    print("moving backward")
    servo1 = GPIO.PWM(servo1pin, 50)
    servo2 = GPIO.PWM (servo2pin, 50)
    servo1.start(10.5)
    servo2.start(4.5)
    time.sleep(2)
    servo1.stop()
    servo2.stop()


def motorleft():
    print("moving to the left")
    servo1 = GPIO.PWM(servo1pin, 50)
    servo2 = GPIO.PWM (servo2pin, 50)
    servo1.start(4.5)
    servo2.start(4.5)
    time.sleep(2)
    servo1.stop()
    servo2.stop()

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
        image = frame.array

        blur = cv2.blur(image, (3,3))


        lower = np.array([76,31,4],dtype="uint8")
        upper = np.array([210,90,70], dtype="uint8")

        thresh = cv2.inRange(blur, lower, upper)
        thresh2 = thresh.copy()

        contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

        max_area = 0
        best_cnt = 1
        for cnt in contours:
                area = cv2.contourArea(cnt)
                if area > max_area:
                        max_area = area
                        best_cnt = cnt

        M = cv2.moments(best_cnt)
        cx,cy = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
        print ("the y is :"+str(cy))
        print ("the x is :"+str(cx))


        #tracking the object and centring the object on the x axis
        #if (215<cx<265):
        #    motorstop()
        if (cx < 270):
            motorright()
            if (cy<200):
                motorforward()
            elif (cy>280):
                motorbackward()

        elif (cx > 380):
            motorleft()
            if (cy<200):
                motorforward()
            elif (cy>280):
                motorbackward()

        #tracking the object and centring the object on the y axis
        #if (300<cy<345):
        #    motorstop()
        cv2.circle(blur,(cx,cy),10,(0,0,255),-1)
        cv2.imshow("Frame", blur)
        key = cv2.waitKey(1) & 0xFF

        rawCapture.truncate(0)

        if key == ord("q"):
            break
1

A "segmentation fault" is your program trying to violate its memory space. It sounds scarier than it is--nothing really got broken. If a debugger is unavailable to you, simply put in additional print(...) statements to pinpoint exactly where your segmentation fault is. Adding print() statements sounds silly but is surprisingly effective. Put one after each line if necessary to find exact line that died.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.