<< return to Pixycam.com

User Tools

Site Tools


wiki:v2:python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
wiki:v2:python [2021/12/08 13:52]
kees 211208 - Added installation section
wiki:v2:python [2021/12/30 13:02]
kees Added set_next_turn() and set_default_turn() and removed some typos
Line 1: Line 1:
-**!!This page is under construction!!** 
- 
 ====== Pixy2 Python tutorial ====== ====== Pixy2 Python tutorial ======
  
Line 17: Line 15:
 In this tutorial we will describe the following platforms: In this tutorial we will describe the following platforms:
  
-  * Raspberry Pi: using USB and the libpixyusb2 library. 
   * LEGO Mindstorms EV3 using Python on the ev3dev operating system.   * LEGO Mindstorms EV3 using Python on the ev3dev operating system.
   * LEGO Mindstorms EV3 using MicroPython with PyBricks.   * LEGO Mindstorms EV3 using MicroPython with PyBricks.
 +  * Raspberry Pi: using USB and the libpixyusb2 library.
  
 This tutorial belongs to Pixy2 and Pixy2.1. If you own the previous Pixy, go This tutorial belongs to Pixy2 and Pixy2.1. If you own the previous Pixy, go
 to the chapter "Pixy for LEGO Mindstorms"​ in the tutorial to the chapter "Pixy for LEGO Mindstorms"​ in the tutorial
-[[https://​github.com/​KWSmit/​Pixy_ev3dev|over here]]. That tutorial is written +[[https://​github.com/​KWSmit/​Pixy_ev3dev|over here]]written 
-by one of our customers. +by one of our customers ​which explains how to use Pixy on LEGO Mindstorms EV3.
- +
-===== Rasberry Pi ===== +
- +
-<Work in progress>​+
  
 ===== LEGO Mindstorms EV3 ===== ===== LEGO Mindstorms EV3 =====
Line 122: Line 116:
 === 2. Adding the API to your project folder === === 2. Adding the API to your project folder ===
  
-This method works for both Python3 and MicroPython. Visit the repository+This method works for both Python3 and MicroPython
 + 
 +In your project folder create a new folder ''​pixycamev3''​. Visit the repository
 on [[https://​github.com/​charmedlabs/​pixycamev3/​tree/​main/​pixycamev3|Github]]. on [[https://​github.com/​charmedlabs/​pixycamev3/​tree/​main/​pixycamev3|Github]].
-Add the file pixy2.py to your project ​folder. The easiest way is to open this +Add the file pixy2.py to your folder ​''​pixycamev3''​. The easiest way is to open 
-file in Github and click on the ''​Raw''​ button right above the code window. +this file in Github and click on the ''​Raw''​ button right above the code window. 
-Then select all text of the file and copy it to a new file names ''​pixy2.py''​ +Then select all text of the file and copy it to a new file named ''​pixy2.py''​ 
-in your project folder.+in your ''​pixycamev3''​ folder. 
 + 
 +Your project folder ​structure should look now like this: 
 + 
 +<​code>​ 
 +project 
 +  ├── pixycamev3 
 +  │     ​└── pixy2.py 
 +  └── main.py 
 +</​code>​
  
-The same Python ​file works for both Python and MicroPython. When you copied+The file ''​pixy2.py'' ​works for both Python and MicroPython. When you copied
 the file you're ready to program! the file you're ready to program!
  
Line 147: Line 152:
 | **Example:​** ​                                                              ^^ | **Example:​** ​                                                              ^^
 | <​code>​ | <​code>​
-from pixy2 import Pixy2+from pixycamev3.pixy2 import Pixy2
  
 pixy2 = Pixy2(port=1,​ i2c_address=0x54) pixy2 = Pixy2(port=1,​ i2c_address=0x54)
Line 166: Line 171:
 | **Example:​** ​                                                              ^^ | **Example:​** ​                                                              ^^
 | <​code>​ | <​code>​
-from pixy2 import Pixy2+from pixycamev3.pixy2 import Pixy2
  
-prixy2 ​= Pixy2(port=1,​ i2c_address=0x54)+pixy2 = Pixy2(port=1,​ i2c_address=0x54)
  
 # Get version # Get version
Line 187: Line 192:
 | **Example:​** ​                                                              ^^ | **Example:​** ​                                                              ^^
 | <​code>​ | <​code>​
-from pixy2 import Pixy2+from pixycamev3.pixy2 import Pixy2
  
-prixy2 ​= Pixy2(port=1,​ i2c_address=0x54)+pixy2 = Pixy2(port=1,​ i2c_address=0x54)
  
 # Get frame resolution # Get frame resolution
Line 208: Line 213:
 | <​code>​ | <​code>​
 from time import sleep from time import sleep
-from pixy2 import Pixy2+from pixycamev3.pixy2 import Pixy2
  
-prixy2 ​= Pixy2(port=1,​ i2c_address=0x54)+pixy2 = Pixy2(port=1,​ i2c_address=0x54)
  
 # Turn upper leds on for 2 seconds, then turn off # Turn upper leds on for 2 seconds, then turn off
Line 231: Line 236:
 | **Example:​** ​                                                              ^^ | **Example:​** ​                                                              ^^
 | <​code>​ | <​code>​
-from pixy2 import Pixy2, Pixy2Mode+from pixycamev3.pixy2 import Pixy2, Pixy2Mode
  
 pixy2 = Pixy2(port=1,​ i2c_address=0x54) pixy2 = Pixy2(port=1,​ i2c_address=0x54)
Line 261: Line 266:
 | **Example:​** ​                                                              ^^ | **Example:​** ​                                                              ^^
 | <​code>​ | <​code>​
-from pixy2 import Pixy2+from pixycamev3.pixy2 import Pixy2
  
 pixy2 = Pixy2(port=1,​ i2c_address=0x54) pixy2 = Pixy2(port=1,​ i2c_address=0x54)
Line 286: Line 291:
 | MainFeatures object: ​                                                      ^^ | MainFeatures object: ​                                                      ^^
 | error                   | Data error when value True                        | | error                   | Data error when value True                        |
-| length_of_payload ​      | Number of bytes in payload ​                       +%%length_of_payload%%       | Number of bytes in payload ​                   
-| number_of_vectors ​      | Number of vectors detected ​                       +%%number_of_vectors%%       | Number of vectors detected ​                   
-| number_of_intersections | Number of intersections detected ​                 +%%number_of_intersections%% | Number of intersections detected ​             
-| number_of_barcodes ​     | Number of barcodes detected ​                      ​|+%%number_of_barcodes%%      | Number of barcodes detected ​                  ​|
 | vectors ​                | List with Vector objects (length number_of_vectors) | | vectors ​                | List with Vector objects (length number_of_vectors) |
 | intersections ​          | List with Intersection objects (length number_of_intersections) | | intersections ​          | List with Intersection objects (length number_of_intersections) |
Line 300: Line 305:
 | y1             | Y location endpoint vector ​                                | | y1             | Y location endpoint vector ​                                |
 | index          | Index of vector in array                                   | | index          | Index of vector in array                                   |
-| flags          | <TODO: ??> ​                                                |+| flags          | Internal state, used for testing/​debugging ​                |
 |                                                                            ^^ |                                                                            ^^
 | Intersection object: ​                                                      ^^ | Intersection object: ​                                                      ^^
 | x              | X location of intersection ​                                | | x              | X location of intersection ​                                |
 | y              | Y location of intersection ​                                | | y              | Y location of intersection ​                                |
-| nr_of_branches | Number of branches starting at this intersection ​          ​|+%%nr_of_branches%% | Number of branches starting at this intersection ​      ​|
 | branches ​      | Array with Branch objects (length number_of_branches) ​     | | branches ​      | Array with Branch objects (length number_of_branches) ​     |
 |                                                                            ^^ |                                                                            ^^
Line 311: Line 316:
 | x              | X location center of barcode ​                              | | x              | X location center of barcode ​                              |
 | y              | Y location center of barcode ​                              | | y              | Y location center of barcode ​                              |
-| flags          | <TODO: ??> ​                                                |+| flags          | Internal state, used for testing/​debugging ​                |
 | code           | Number of barcode (see Pixy2 wiki)                         | | code           | Number of barcode (see Pixy2 wiki)                         |
 |                                                                            ^^ |                                                                            ^^
Line 321: Line 326:
 | **Example:​** ​                                                              ^^ | **Example:​** ​                                                              ^^
 | <​code>​ | <​code>​
-from pixy2 import Pixy2+from pixycamev3.pixy2 import Pixy2 
 + 
 +pixy2 = Pixy2(port=1,​ i2c_address=0x54)
  
 # Detect barcodes and print their number # Detect barcodes and print their number
Line 329: Line 336:
         for i in range(0, data.number_of_barcodes)         for i in range(0, data.number_of_barcodes)
             print('​Barcode number: ', data.barcodes[i].code)             print('​Barcode number: ', data.barcodes[i].code)
 +</​code> ​                                                                     ^^
 +
 +^ %%set_next_turn(angle)%% ​                                                  ^^
 +| **Description:​** ​                                                          ^^
 +| Set direction for turn at the next intersection. After this intersection ​  ^^
 +| Pixy2 will choose it's default turn again. ​                                ^^
 +| **Parameters:​** ​                                                           ^^
 +| angle | Direction to choose at the next intersection (-180, 180  degrees) ​  |
 +| **Returns:​** ​                                                              ^^
 +| None                                                                       ^^
 +| **Example:​** ​                                                              ^^
 +| <​code>​
 +from time import sleep
 +from pixycamev3.pixy2 import Pixy2
 +
 +pixy2 = Pixy2(port=1,​ i2c_address=0x54)
 +
 +# Set next turn to 90 degrees
 +pixy2.set_next_turn(90)
 +</​code> ​                                                                     ^^
 +
 +^ %%set_default_turn(angle)%% ​                                               ^^
 +| **Description:​** ​                                                          ^^
 +| Set default direction for all intersections. ​                              ^^
 +| **Parameters:​** ​                                                           ^^
 +| angle | Default direction (-180, 180 degrees) ​                              |
 +| **Returns:​** ​                                                              ^^
 +| None                                                                       ^^
 +| **Example:​** ​                                                              ^^
 +| <​code>​
 +from time import sleep
 +from pixycamev3.pixy2 import Pixy2
 +
 +pixy2 = Pixy2(port=1,​ i2c_address=0x54)
 +
 +# Set default turn to 0 degrees (straight on)
 +pixy2.set_default_turn(0)
 </​code> ​                                                                     ^^ </​code> ​                                                                     ^^
  
Line 335: Line 379:
 You'll find more practical sample code in the You'll find more practical sample code in the
 [[https://​github.com/​charmedlabs/​pixycamev3|Github repository]]. [[https://​github.com/​charmedlabs/​pixycamev3|Github repository]].
 +
 +===== Rasberry Pi =====
 +
 +<Work in progress>​
  
wiki/v2/python.txt · Last modified: 2021/12/30 14:48 by kees