Maixy face tracking robot issues

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
corruptedlink
Posts: 2
Joined: Fri Aug 16, 2019 8:50 am

Maixy face tracking robot issues

Post by corruptedlink » Fri Aug 16, 2019 2:49 pm

Hello everyone,

Please let me preface this by saying i am a noob when it comes to coding so please be gentle.

I was recently given a Maixy dan dock (K210 RISC-V AI SoC) as a gift and have been looking at the face tracking pan/tilt demo that I loaded on there. I was wondering if I would be able to interface an arduino with it to control an added back and forward motion with a track platform I have. Im VERY new to Micropython (literally weeks old) and would like some guidance towards an answer, I'm looking to learn several things, they are:

1. could I modify the original code to add a Z axis to control the forward and backwards motion, it already takes the X and Y axis for the pan/tilt function.

2. Should I look at writing separate code for the arduino and not bother with taking the info from the Maixy board, so it would use an ultrasonic sensor on the pan/tilt module to track the person (this sounds like it would erroneous and plagued with issues) and keep within a certain range.

3. Can the Maixy be interfaced with the arduino to parse the information to drive the motors so distance and centralisation is decided by the Maixy but driven by the arduino. (this is my favourite solution)

I would like learning experiences more than solutions... if that makes sense. I'm looking to be able to say "i did this" rather than "i copied and pasted this"

If anyone knows where i could look to find the answers to these issues i would be grateful, it will be going into an instructible to show that new coders can get to a decent level of competencies if they ask the right questions and study. I think I may need to learn about I2C, but not sure if this would be the right direction.

Below is the pan/tilt face tracking code:

Code: Select all

import machine
from fpioa_manager import *
global fm
class PT:
	def __init__(self,P_x = -0.07,P_y = -0.05,x_init_ang = 90,y_init_ang = 9
		global fm
		if x_init_ang > 160 or x_init_ang<30:
			print("x_init_ang value error")
			return None
		if y_init_ang > 160 or y_init_ang<30:
			print("y_init_ang value error")
			return None
		if P_x > 0 :
			print("x axis P_value(P_x) must <0")
			return None
		if P_y > 0 :
			print("y axis P_value(P_y) must <0")
			return None
		self.Px = P_x
		self.Py = P_y
		self.x_angle = x_init_ang
		self.y_angle = y_init_ang
		self.print_en = pr_en
		self.limite_value = limited_val				 
		self.cam=machine.ov2640();				      
		self.lcd=machine.st7789();				      
		self.demo=machine.demo_face_detect();			   
		self.demo.init();					       
		self.cam.init();						
		self.lcd.init();						
		fm.registered(34,fm.fpioa.TIMER1_TOGGLE1);		      
		fm.registered(35,fm.fpioa.TIMER1_TOGGLE2);		      
		self.y_axis=machine.pwm(machine.pwm.TIMER1,machine.pwm.CHANEEL0,
		self.x_axis=machine.pwm(machine.pwm.TIMER1,machine.pwm.CHANEEL1,
		self.x_axis.duty(7.5)					   
		self.y_axis.duty(7.5)					   
										
	def set_P(self,P_x = 0,P_y = 0):					
		if P_y >= 0 :						   
			print("y axis P_value(P_y) must <0")		    
			return False					    
		if P_x >= 0 :						   
			print("x axis P_value(P_x) must <0")		  
			return False					    
		self.Px = P_x						   
		self.Py = P_y						   
	def run(self):							  
		ex = 0.0							
		ey = 0.0							
		ux_angle = 0.0						  
		uy_angle = 0.0						  
		x = 0.0							 
		y = 0.0							 
		x1 = 0							  
		x2 = 0							  
		y1 = 0							  
		y2 = 0							  
		image = bytearray(320*240*2);				   
		while(1):						       
			self.cam.get_image(image);			      
			data = self.demo.process_image(image);		  
			self.lcd.draw_picture_default(image);		   
			x1=data[0];					     
			x2=data[1];					     
			y1=data[2];		
			y2=data[3];					     
										
			if x1 > 320 or x2 > 320 or y1 > 240 or y2 > 240 :       
				continue					
			x =(x2 - x1)/2+x1;				      
			y =(y2 - y1)/2+y1;				      
										
			ex = (160 - x);					 
			ey = (120 - y);					 
										
			ux_angle = ex  * self.Px				
			if abs(ex) < 5 : ux_angle = 0			   
			if ux_angle >= 15 : ux_angle = 15		       
			if ux_angle <= -15 : ux_angle = -15		     
										
			uy_angle = ey  * self.Py				
			if abs(ey) < 5 : ux_angle = 0			   
			if uy_angle >= self.limite_value : uy_angle = self.limit
			if uy_angle <= -self.limite_value : uy_angle = -self.lim
										
			self.x_angle = ux_angle + self.x_angle		  
			self.y_angle = uy_angle + self.y_angle       
			if self.x_angle * 0.055 >= 9.8 :			
			    self.x_angle = 180				  
			    ux_angle = 0					
			if self.x_angle * 0.055 <= 0.5 :			
			    self.x_angle = 0				    
			    ux_angle = 0					
			x_angle_duty =  (self.x_angle) * 0.055		  
			y_angle_duty =  (self.y_angle) * 0.055		  
										
			if self.print_en == 1 :				 
				print("x = ",x)				 
				print("y = ",y)				 
				print("ux_angle = ",ux_angle)		   
				print("uy_angle = ",uy_angle)		   
				print("x_angle = ",self.x_angle)		
				print("y_angle = ",self.y_angle)		
			self.x_axis.duty(2.5+x_angle_duty)		      
			self.y_axis.duty(2.5+y_angle_duty)

OutoftheBOTS_
Posts: 679
Joined: Mon Nov 20, 2017 10:18 am

Re: Maixy face tracking robot issues

Post by OutoftheBOTS_ » Fri Aug 16, 2019 8:57 pm

Maxy can be easily interfaced with Arduino via UART serial but I can't see why you would want to do this because the Maxy would have 1000x the processing power of an Ardunio and I would suggest you just add the Z axis code to the Maxy code.

I made a robot ant many years ago that would follow around a ball see https://www.youtube.com/watch?v=BzI1C4z0uG4&t=48s it used a pixy cam and colour blob tracking. I basically made the pan/tilt of the camera follow the ball then I made the forward and backward and steering of the ant robot follow the pan/tilt of the camera i.e if the cam was pan to the left then the robot would walk left and if the cam was tilted up to see the ball the robot would walk forward

corruptedlink
Posts: 2
Joined: Fri Aug 16, 2019 8:50 am

Re: Maixy face tracking robot issues

Post by corruptedlink » Sat Aug 17, 2019 12:07 am

Thanks,

I was thinking I may be over complicating a simpler solution.

Post Reply