K210(Sipeed M1) run face tracking PT demo with maixpy

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
zepan
Posts: 10
Joined: Sat Nov 17, 2018 9:01 am

K210(Sipeed M1) run face tracking PT demo with maixpy

Post by zepan » Wed Dec 05, 2018 2:08 pm

0. Hi, we are sipeed team, this post introduce how to run face tracking on Sipeed MAIX-I board.
We decide to not run demo automatically, and so you can try it yourself to run the face tracking demo in few lines of coding.
It is an rushed demo, architecture is not consider well, and we are moving to freeRTOS version SDK, this demo is almost last version of standalone SDK.
We will redesign the architecture in freeRTOS version, it will be more easy to use.
If you are interested in maixpy development and experienced in micropython, you can send email to support@sipeed.com, and we will send MAIX board to you for FREE~
This firmware can be download at here: https://drive.google.com/open?id=1uhoff ... JwlGwcMTAn

1. Stick PT on desk, Connect usb to board, and Install CH340 driver(http://www.wch.cn/download/CH341SER_ZIP.html).
2. Open any terminal software like XShell, set baudrate to 115200, and open corresponding serial port, press some ‘Enter’ key, and you can see ‘>>>’ come out.
3. You need to define a PT(PAN/TILT) object. use code as follow

Code: Select all

	  import PT
	  PT_test=PT.PT()
if you want set P parametr of PID controler(P default value of x axis is -0.07,and P of y axis is -0.05) , you can use code as follow,"xx" is the value you want

Code: Select all

PT_test = PT.PT(P_x = xx,P_y = xx)
4. Now , you can run PT by using code

Code: Select all

PT_test.run()
And you can move your face , the PT will follow your face.
if yo want to stop it ,you can press ctrl+C with keyboard
after you stop the PT_test,you can use the code to modify the P value of PID controler

Code: Select all

PT_test.set_P(P_x = xx,P_y = xx)
after that ,you can restart the PT with different P value

Code: Select all

PT_test.run()
Note:
The demo design to track one face, more than one face will case shake.
Servo have limited angle, if tracking too upside, it maybe reboot.
you can watch video on: https://www.youtube.com/watch?v=7bpNVzFbGa0
we will upload this face tracking PT to indiegogo perk soon: https://www.indiegogo.com/projects/sipe ... 19744832#/

The PT.py code(you can use " app.vi('/PT.py')" to edit it on board, or use machine.rz() to rcv file from PC):

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)
			

victagayun
Posts: 8
Joined: Fri Oct 26, 2018 1:38 am

Re: K210(Sipeed M1) run face tracking PT demo with maixpy

Post by victagayun » Wed Dec 05, 2018 3:38 pm

Hello
Will you consider the same discount to be given on those same backers in Indiegogo in case they would order again?
In my case, I am short of cash right now that I would order different package, but maybe after December I could order more.

zepan
Posts: 10
Joined: Sat Nov 17, 2018 9:01 am

Re: K210(Sipeed M1) run face tracking PT demo with maixpy

Post by zepan » Wed Dec 05, 2018 3:44 pm

victagayun wrote:
Wed Dec 05, 2018 3:38 pm
Hello
Will you consider the same discount to be given on those same backers in Indiegogo in case they would order again?
In my case, I am short of cash right now that I would order different package, but maybe after December I could order more.
Sure, We will give discount or even donate boards for developer(who push codes or post issues).

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

Re: K210(Sipeed M1) run face tracking PT demo with maixpy

Post by OutoftheBOTS_ » Wed Dec 05, 2018 10:17 pm

zepan wrote:
Wed Dec 05, 2018 3:44 pm
victagayun wrote:
Wed Dec 05, 2018 3:38 pm
Hello
Will you consider the same discount to be given on those same backers in Indiegogo in case they would order again?
In my case, I am short of cash right now that I would order different package, but maybe after December I could order more.
Sure, We will give discount or even donate boards for developer(who push codes or post issues).
You will need to start an English forum for your board. At the moment there only seems to be Chinese forum and google won't translate it. Once there is a English forum there will be much more sharing and learning of users. Also in the beginning the forum will need to be supported heavily by the manufacture but as more users learn and become experienced then the more experienced users can help the new users on the forum and this will lower the work load on the manufacturer.

kevinkk525
Posts: 270
Joined: Sat Feb 03, 2018 7:02 pm

Re: K210(Sipeed M1) run face tracking PT demo with maixpy

Post by kevinkk525 » Thu Dec 06, 2018 9:02 am

OutoftheBOTS_ wrote:
Wed Dec 05, 2018 10:17 pm
zepan wrote:
Wed Dec 05, 2018 3:44 pm
victagayun wrote:
Wed Dec 05, 2018 3:38 pm
Hello
Will you consider the same discount to be given on those same backers in Indiegogo in case they would order again?
In my case, I am short of cash right now that I would order different package, but maybe after December I could order more.
Sure, We will give discount or even donate boards for developer(who push codes or post issues).
You will need to start an English forum for your board. At the moment there only seems to be Chinese forum and google won't translate it. Once there is a English forum there will be much more sharing and learning of users. Also in the beginning the forum will need to be supported heavily by the manufacture but as more users learn and become experienced then the more experienced users can help the new users on the forum and this will lower the work load on the manufacturer.
Even corporations creating bigger devices like odroid do have an english forum supported by the developers and it works out very well for them.

zepan
Posts: 10
Joined: Sat Nov 17, 2018 9:01 am

Re: K210(Sipeed M1) run face tracking PT demo with maixpy

Post by zepan » Thu Dec 06, 2018 10:09 am

kevinkk525 wrote:
Thu Dec 06, 2018 9:02 am
OutoftheBOTS_ wrote:
Wed Dec 05, 2018 10:17 pm
zepan wrote:
Wed Dec 05, 2018 3:44 pm


Sure, We will give discount or even donate boards for developer(who push codes or post issues).
You will need to start an English forum for your board. At the moment there only seems to be Chinese forum and google won't translate it. Once there is a English forum there will be much more sharing and learning of users. Also in the beginning the forum will need to be supported heavily by the manufacture but as more users learn and become experienced then the more experienced users can help the new users on the forum and this will lower the work load on the manufacturer.
Even corporations creating bigger devices like odroid do have an english forum supported by the developers and it works out very well for them.
we are building forum now, will online next week.

loboris
Posts: 310
Joined: Fri Oct 02, 2015 6:19 pm

Re: K210(Sipeed M1) run face tracking PT demo with maixpy

Post by loboris » Mon Dec 17, 2018 8:44 pm

I've tested the PT face tracking demo and it works very well.

I've had some issues using kflash.py (on Ubuntu 18.04) to upload the firmware.
The upload speed was extremely slow, no difference if used with 115200 or 2000000 bd and always failed after some time.
Then I've tried using -l option and the binary bootloader file isp_flash.bin and the upload finished without issues:

Code: Select all

./kflash.py -p /dev/ttyUSB0 -b 2000000 -l isp_flash.bin sipeedm1_tracking_PT.kfpkg

lbattraw
Posts: 21
Joined: Sun May 10, 2015 11:35 am

Re: K210(Sipeed M1) run face tracking PT demo with maixpy

Post by lbattraw » Mon Jan 14, 2019 8:35 am

zepan wrote:
Thu Dec 06, 2018 10:09 am
we are building forum now, will online next week.
Hi, where can we find your forum? Also I tried compiling MP from the git repo and ran into problems when it tried to link it:

CC ../../lib/libc/string0.c
CC ../../lib/mp-readline/readline.c
CC ../../lib/netutils/netutils.c
CC kendryte-standalone-sdk/lib/bsp/crt.S
LINK micropython
/opt/lib/gcc/riscv64-unknown-elf/8.2.0/../../../../riscv64-unknown-elf/bin/ld: /opt/lib/gcc/riscv64-unknown-elf/8.2.0/../../../../riscv64-unknown-elf/lib/libc.a(lib_a-vfiprintf.o): in function `.L13':
vfprintf.c:(.text._vfiprintf_r+0x32): undefined reference to `pthread_setcancelstate'
/opt/lib/gcc/riscv64-unknown-elf/8.2.0/../../../../riscv64-unknown-elf/bin/ld: /opt/lib/gcc/riscv64-unknown-elf/8.2.0/../../../../riscv64-unknown-elf/lib/libc.a(lib_a-vfiprintf.o): in function `.L15':
vfprintf.c:(.text._vfiprintf_r+0x76): undefined reference to `pthread_setcancelstate'
...

There are a lot more lines with the same undefined reference. The repo I cloned is here: https://github.com/sipeed/MaixPy.git Hopefully that's the right one. It would be nice to have a complete "starter's guide", beginning with where to get the SDK/compiler environment (kendryte-gnu-toolchain) from as well as compiling and flashing MP. I downloaded and compiled the SDK and compiler successfully, but it will help others if there's a central howto/FAQ for getting started with the K210 toolchain and how to compile and flash MP. I would be happy to help, just not sure where to contribute.

Thanks!

xgarb
Posts: 1
Joined: Tue Jan 15, 2019 5:10 pm

Re: K210(Sipeed M1) run face tracking PT demo with maixpy

Post by xgarb » Tue Jan 15, 2019 5:15 pm

I'm no expert in this (I've written about 5 lines of Python in my life) but I've got MicroPython working using Win10 and WSL. I've put a tutorial in my blog here:

https://robotzero.one/sipeed-maix-micropython/

Might be helpful to someone.

User avatar
Roberthh
Posts: 1170
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: K210(Sipeed M1) run face tracking PT demo with maixpy

Post by Roberthh » Sun Jan 20, 2019 3:20 pm

At this place:
https://github.com/robert-hh/K210-Stuff
you'll find a few files that might be helpful when working with the K210. One of them fixes the bug with input(), sys.stdin.read() etc. It also includes an adapted version of the pye.py editor, which may be useful since you had to remove vi. A liitle bit more info is in the readme.md file there

Post Reply