Skip to content

Updated code #1

@TissueKing

Description

@TissueKing

I have been trying to use this code to track a roomba like you have done, but I am getting many errors. I have re-written most of the code and imported other libraries such as pigpio, but am still having trouble. I was wondering if this code could be modified for python 3?

import sys
import smbus
from time import time, sleep
import my_pigpio
import RPi.GPIO as GPIO

device = 1
channel = 1

class IRcam:

def __init__(self):
	self.sensorAddress = 0x58
	self.device = smbus.SMBus (1)
	sleep (1)
	
	self.positions = {'found':False, '1':[0,0], '2':[0,0],'3':[0,0], '4':[0,0]}
	self.initCMDs = [0x30, 0x01, 0x30, 0x08, 0x06, 0x90, 0x08, 0xC0, 0x1A, 0x40, 0x33, 0x33]
	for i,j in zip(self.initCMDs[0::2], self.initCMDs[1::2]):
		self.device.write_byte_data(0x58, i, j)
		sleep(0.01)

def getPositions(self):
	coordinates = 1
	self.device.write_byte_data(0x58, 0x36, 32)
	print("I have written")
	#coordinates = self.device.read_i2c_block_data(0x58, 0x36)
	print("I have read")
	sleep(.05)
	
    
	x = [0x00] * 4
	y = [0x00] * 4
	i = 0
	for j in range(1,11,3): #Decoding of data from I2c device
		x[i]= coordinates[j]+((coordinates[j+2] & 0x30) << 4)
		y[i]=coordinates[j+1]+((coordinates[j+2] & 0xC0) << 2)
		i+=1
	i = 0
	for j in ('1','2','3','4'): # positions into dictionary
		self.positions[j][0]=x[i]
		self.positions[j][1]=y[i]
		i+=1
	if ( all(i ==1023 for i in x) and all(i == 1023 for i in y) ): # no IR at 1023
		self.positions['found'] = False
	else:
		self.positions['found'] = True

update_rate = 0.01 # 100hz update rate

camera = IRcam() # Sensor initialization

while True:
current = time() # Counters for loop
elapsed = 0
print("I made it this far")
camera.getPositions() # Update found IR objects
print("Found Camera, have data")

if camera.positions['found']: # If an IR object is found, print the information
	print ("Object 1: %d, %d | Object 2: %d, %d | Object 3: %d, %d | Object 4: %d, %d"  (camera.positions['1'][0],camera.positions['1'][1],camera.positions['2'][0],camera.positions['2'][1],camera.positions['3'][0],camera.positions['3'][1],camera.positions['4'][0],camera.positions['4'][1]))

while elapsed < update_rate:
elapsed = time() - current # Wait until the loop completes, perfect update_rate loop

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions