Question
I have a gopigo attached with couple sensors (camera, GPS, air quality sensor). I am trying to put all 4 code together to have one
I have a gopigo attached with couple sensors (camera, GPS, air quality sensor). I am trying to put all 4 code together to have one program(control, camera(live video stream), GPS(location) and air quality sensor. I want all of the 4 codes to run at the same time so that I can control the GOPIGO, view the video which is my eyes, know my location and analyze the condition of the environment with air quality sensor.
Control code in python
from gopigo import * #Has the basic functions for controlling the GoPiGo Robot
import sys #Used for closing the running program
print ("This is a basic example for the GoPiGo Robot control")
print ("Press: \tw: Move GoPiGo Robot forward \ta: Turn GoPiGo Robot left \td: Turn GoPiGo Robot right \ts: Move GoPiGo Robot backward \tt: Increase speed \tg: Decrease speed \tx: Stop GoPiGo Robot \tz: Exit ")
while True:
print ("Enter the Command:")
a=raw_input() # Fetch the input from the terminal
if a=='w':
fwd() # Move forward
elif a=='a':
left() # Turn left
elif a=='d':
right() # Turn Right
elif a=='s':
bwd() # Move back
elif a=='x':
stop() # Stop
elif a=='t':
increase_speed() # Increase speed
elif a=='g':
decrease_speed() # Decrease speed
elif a=='z':
print ("Exiting") # Exit
sys.exit()
else:
print ("Wrong Command, Please Enter Again")
time.sleep(.1)
Camera video streaming code
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
# allow the camera to warmup
time.sleep(0.1)
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
image = frame.array
# show the frame
cv2.imshow("Frame", image)
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
Air quality sensor code
import time import gopigo # Connect the Grove Air Quality Sensor to analog port A0 # SIG,NC,VCC,GND air_sensor = 1 gopigo.pinMode(air_sensor,"INPUT") while True: try: # Get sensor value sensor_value = gopigo.analogRead(air_sensor) if sensor_value > 500: print ("Danger-High Pollution Air Quality") elif sensor_value > 300: print ("Low pollution") else: print ("Air fresh safe to go in with caution") print ("sensor_value ="+ str(sensor_value)) time.sleep(.5) except IOError: print ("Error")
GPS sensor code
from gopigo import *
import serial, time
import smbus
import math
import RPi.GPIO as GPIO
import struct
import sys
import ir_receiver_check
if ir_receiver_check.check_ir():
print "Disable IR receiver before continuing"
exit()
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout = 0) #Open the serial port at 9600 baud
ser.flush()
def readlineCR():
rv = ""
while True:
time.sleep(0.01) # This is the critical part. A small pause
# works really well here.
ch = ser.read()
rv += ch
if ch==' ' or ch=='':
return rv
while True:
#readlineCR()
x=readlineCR()
print x
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started