├── .gitignore ├── DESCRIPTION.rst ├── LICENSE ├── MANIFEST.in ├── README.md ├── basic_examples ├── 10_imu_read.py ├── 11_sound_direction_read.py ├── 12_dual_touch_read.py ├── 13_camera_easy_use.py ├── 1_pidog_init.py ├── 2_legs_control.py ├── 3_head_control.py ├── 4_tail_control.py ├── 5_stop_actions.py ├── 6_do_preset_actions.py ├── 7_sound_effect.py ├── 8_ultrasonic_read.py └── 9_rgb_control.py ├── bin ├── pidog_app └── pidog_app_install.sh ├── examples ├── 0_calibration.py ├── 10_balance.py ├── 11_keyboard_control.py ├── 12_app_control.py ├── 13_ball_track.py ├── 1_wake_up.py ├── 2_function_demonstration.py ├── 3_patrol.py ├── 4_response.py ├── 5_rest.py ├── 6_be_picked_up.py ├── 7_face_track.py ├── 8_pushup.py ├── 9_howling.py ├── curses_utils.py ├── custom_actions.py ├── preset_actions.py └── servo_zeroing.py ├── gpt_examples ├── README.md ├── action_flow.py ├── gpt_dog.py ├── keys.py ├── openai_helper.py ├── preset_actions.py ├── tutorial_1.png ├── tutorial_2.png └── utils.py ├── i2samp.sh ├── pidog ├── __init__.py ├── actions_dictionary.py ├── dual_touch.py ├── pidog.py ├── rgb_strip.py ├── sh3001.py ├── sound_direction.py ├── trot.py ├── version.py └── walk.py ├── setup.py ├── sounds ├── angry.wav ├── confused_1.mp3 ├── confused_2.mp3 ├── confused_3.mp3 ├── growl_1.mp3 ├── growl_2.mp3 ├── howling.mp3 ├── pant.mp3 ├── single_bark_1.mp3 ├── single_bark_2.mp3 ├── snoring.mp3 └── woohoo.mp3 └── test ├── angry_bark.py ├── cover_photo.py ├── dual_touch_test.py ├── imu_test.py ├── power_test.py ├── rgb_strip_test.py ├── sound_direction_test.py ├── stand_test.py ├── tail.py ├── ultrasonic_iic_test.py └── ultrasonic_test.py /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/* 2 | dev/* 3 | photo 4 | test/photo* 5 | test/scene/* 6 | backups/* 7 | -------------------------------------------------------------------------------- /DESCRIPTION.rst: -------------------------------------------------------------------------------- 1 | Raspberry Pi 2 | ======================= 3 | Pidog Library for Raspberry Pi 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include DESCRIPTION.rst 2 | 3 | # Include the test suite (FIXME: does not work yet) 4 | # recursive-include tests * 5 | 6 | # If using Python 3.5 or less, then have to include package data, even though 7 | # it's already declared in setup.py 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pidog 2 | 3 | Pidog Python library for Raspberry Pi. 4 | 5 | Quick Links: 6 | 7 | - [Pidog](#pidog) 8 | - [Docs](#docs) 9 | - [Installation](#installation) 10 | - [About SunFounder](#about-sunfounder) 11 | - [Contact us](#contact-us) 12 | - [Credit](#credit) 13 | 14 | ---------------------------------------------- 15 | 16 | ## Docs 17 | 18 | - 19 | 20 | ---------------------------------------------- 21 | 22 | ## Installation 23 | 24 | - 25 | 26 | ### install tool 27 | 28 | ```bash 29 | sudo apt install git python3-pip python3-setuptools python3-smbus 30 | ``` 31 | 32 | ### robot-hat library 33 | 34 | ```bash 35 | cd ~/ 36 | git clone -b v2.0 https://github.com/sunfounder/robot-hat.git 37 | cd robot-hat 38 | sudo python3 setup.py install 39 | 40 | ``` 41 | 42 | ### vilib library 43 | 44 | ```bash 45 | cd ~/ 46 | git clone -b picamera2 https://github.com/sunfounder/vilib.git 47 | cd vilib 48 | sudo python3 install.py 49 | ``` 50 | 51 | ### pidog library 52 | 53 | ```bash 54 | cd ~/ 55 | git clone https://github.com/sunfounder/pidog.git 56 | cd pidog 57 | sudo python3 setup.py install 58 | ``` 59 | 60 | ### i2samp 61 | 62 | ``` 63 | cd ~/pidog 64 | sudo bash i2samp.sh 65 | ``` 66 | 67 | ---------------------------------------------- 68 | 69 | ## About SunFounder 70 | 71 | SunFounder is a technology company focused on Raspberry Pi and Arduino open source community development. Committed to the promotion of open source culture, we strives to bring the fun of electronics making to people all around the world and enable everyone to be a maker. Our products include learning kits, development boards, robots, sensor modules and development tools. In addition to high quality products, SunFounder also offers video tutorials to help you make your own project. If you have interest in open source or making something cool, welcome to join us! 72 | 73 | ---------------------------------------------- 74 | 75 | ## Contact us 76 | 77 | website: 78 | www.sunfounder.com 79 | 80 | E-mail: 81 | service@sunfounder.com, support@sunfounder.com 82 | 83 | ## Credit 84 | 85 | Most sound effect are from [Zapsplat.com](https://www.zapsplat.com) 86 | -------------------------------------------------------------------------------- /basic_examples/10_imu_read.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python3 2 | ''' read imu sh3001 data 3 | SH3001, an imu with integrated 3-axis accelerometer and 3-axis gyroscope 4 | Pay attention to the installation direction of the module when using. 5 | 6 | The data "accData" and "gyroData" of the imu will be continuously refreshed 7 | in the built-in thread of the Pidog class. 8 | 9 | API: 10 | Pidog.accData = [ax, ay, az] 11 | the acceleration value, default gravity 1G = -16384 12 | note that the accelerometer direction is opposite to the actual acceleration direction 13 | 14 | Pidog.gyroData = [gx, gy, gz] 15 | the gyro value 16 | 17 | more to see: ../pidog/sh3001.py 18 | 19 | ''' 20 | 21 | from pidog import Pidog 22 | import time 23 | 24 | my_dog = Pidog() 25 | 26 | while True: 27 | ax, ay, az = my_dog.accData 28 | gx, gy, gz = my_dog.gyroData 29 | print(f"accData: {ax}, {ay}, {az} gyroData: {gx}, {gy}, {gz}") 30 | time.sleep(0.2) -------------------------------------------------------------------------------- /basic_examples/11_sound_direction_read.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python3 2 | ''' read azimuth of sound direction 3 | 4 | API: 5 | Pidog.ears.isdetected(): 6 | return bool, whether the sound direction recognition module has detected the sound 7 | 8 | Pidog.ears.read() 9 | return int, the azimuth of the identified sound, 0 ~ 359 10 | 11 | more to see: ../pidog/sound_direction.py 12 | 13 | ''' 14 | 15 | from pidog import Pidog 16 | 17 | my_dog = Pidog() 18 | 19 | while True: 20 | if my_dog.ears.isdetected(): 21 | direction = my_dog.ears.read() 22 | print(f"sound direction: {direction}") 23 | -------------------------------------------------------------------------------- /basic_examples/12_dual_touch_read.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python3 2 | ''' read dual_touch module 3 | 4 | API: 5 | Pidog.dual_touch.read() 6 | - return str, dual_touch status: 7 | - 'N' no touch 8 | - 'L' left touched 9 | - 'LS' left slide 10 | - 'R' right touched 11 | - 'RS' right slide 12 | ''' 13 | 14 | from pidog import Pidog 15 | import time 16 | 17 | my_dog = Pidog() 18 | while True: 19 | touch_status = my_dog.dual_touch.read() 20 | print(f"touch_status: {touch_status}") 21 | time.sleep(0.5) -------------------------------------------------------------------------------- /basic_examples/13_camera_easy_use.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python3 2 | ''' camera easy use 3 | Use the image processing library [vilb] to easily implement functions such as: 4 | web-cam, color detection, face detection, take photos and so on. 5 | 6 | github: https://github.com/sunfounder/vilib.git 7 | 8 | more examples to see: ~/vilib/examples/ 9 | 10 | ''' 11 | 12 | from vilib import Vilib 13 | import time 14 | 15 | # Most of the functions in the vilib library are [@staticmethod], 16 | # you can use them directly without instantiating the Vilib class 17 | 18 | try: 19 | # start camera 20 | Vilib.camera_start(vflip=False,hflip=False) # vflip: image vertical flip, hflip:horizontal flip 21 | # display camera screen 22 | Vilib.display(local=True,web=True) # local: desktop window display, web: webcam display 23 | # enable face detection 24 | Vilib.face_detect_switch(True) 25 | # wait for vilib launch 26 | time.sleep(1) 27 | print('') 28 | 29 | while True: 30 | n = Vilib.detect_obj_parameter['human_n'] 31 | print(f"\r \033[032m{n:^3}\033[m faces are found.", end='', flush=True) 32 | time.sleep(1) 33 | print("\r \033[032m \033[m faces are found.", end='', flush=True) 34 | time.sleep(0.1) 35 | 36 | 37 | except KeyboardInterrupt: 38 | pass 39 | except Exception as e: 40 | print(f"\033[31mERROR: {e}\033[m") 41 | finally: 42 | print("") 43 | Vilib.camera_close() 44 | -------------------------------------------------------------------------------- /basic_examples/1_pidog_init.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python3 2 | ''' pidog initialization 3 | 4 | API: 5 | Class: Pidog() 6 | 7 | __init__(leg_pins=DEFAULT_LEGS_PINS, 8 | head_pins=DEFAULT_HEAD_PINS, 9 | tail_pin=DEFAULT_TAIL_PIN, 10 | leg_init_angles=None, 11 | head_init_angles=None, 12 | tail_init_angle=None) 13 | 14 | - leg_pins list, 8*1 list, the pins of the 8 servos on the legs, 15 | DEFAULT_LEGS_PINS = [2, 3, 7, 8, 0, 1, 10, 11] 16 | order: Left Front Leg, Left Front Leg, Right Front Leg, Right Front Leg, Left Hind Leg, Left Hind Leg, Right Hind Leg, Right Hind Leg 17 | 18 | - head_pins list, 3*1 list, the pins of the 3 servos on the head 19 | DEFAULT_HEAD_PINS = [4, 6, 5] 20 | order: Yaw, Roll, Pitch 21 | 22 | - tail_pin list, 1*1 list, the pin of the tail servos 23 | DEFAULT_TAIL_PIN = [9] 24 | 25 | - leg_init_angles list, 8*1 list, the initial angles of the legs servos 26 | - head_init_angles list, 3*1 list, the initial angles of the head servos 27 | - tail_init_angle list, 1*1 list, the initial angles of the tail servo 28 | 29 | ''' 30 | 31 | # Import Pidog class 32 | from pidog import Pidog 33 | 34 | # instantiate a Pidog with default parameters 35 | # my_dog = Pidog() 36 | 37 | # instantiate a Pidog with custom initialized servo angles 38 | my_dog = Pidog(leg_init_angles = [25, 25, -25, -25, 70, -45, -70, 45], 39 | head_init_angles = [0, 0, -25], 40 | tail_init_angle= [0] 41 | ) 42 | -------------------------------------------------------------------------------- /basic_examples/2_legs_control.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python3 2 | ''' legs control 3 | Basic use of leg servos control 4 | 5 | API: 6 | Pidog.legs_move(target_angles, immediately=True, speed=50) 7 | 8 | legs servo angles control, it will store the target_angles in a buffer, and the built-in leg servo control thread will control the angle of the servos 9 | 10 | - target_angles n*8 2D list, angle arrangements of leg servos, could be one group of angles, 11 | or multiple groups of angles, the order of the servos is: 12 | [[ left front leg, left front leg, right front leg, right front leg, 13 | left hind leg, left hind leg,right hind leg, right hind leg], 14 | [...], ...] 15 | 16 | - immediately bool, whether to execute the specified action immediately, if true: it will clear the buffer first, and store the target_angles, 17 | the set actions will be executed immediately; if false, the will add target_angles at the end of the buffer, the actions will be 18 | executed after previous actions have been executed 19 | 20 | - speed int, speed of action, 0 ~ 100 21 | 22 | Pidog.is_legs_done() 23 | whether all the actions of leg in the buffer to be executed 24 | 25 | Pidog.wait_legs_done() 26 | wait for all the actions of leg in the buffer to be executed 27 | 28 | Pidog.legs_stop() 29 | clear all the actions of leg in the buffer, to make legs stop 30 | 31 | ''' 32 | 33 | from pidog import Pidog 34 | import time 35 | 36 | my_dog = Pidog() 37 | 38 | # single action 39 | single_action = [ # half stand 40 | [45, 10, -45, -10, 45, 10, -45, -10], 41 | ] 42 | my_dog.legs_move(single_action, speed=30) 43 | 44 | single_action_2 = [ # push_up preparation 45 | [45, 35, -45, -35, 80, 70, -80, -70] 46 | ] 47 | my_dog.legs_move(single_action_2, immediately=False, speed=20) 48 | 49 | # wait all actions done 50 | my_dog.wait_legs_done() 51 | time.sleep(0.5) 52 | 53 | # multiple actions 54 | multiple_actions = [ # push_up 55 | [90, -30, -90, 30, 80, 70, -80, -70], 56 | [45, 35, -45, -35, 80, 70, -80, -70] 57 | ] 58 | 59 | while True: 60 | my_dog.legs_move(multiple_actions, immediately=False, speed=75) 61 | time.sleep(0.1) 62 | -------------------------------------------------------------------------------- /basic_examples/3_head_control.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python3 2 | ''' head control 3 | Basic use of head servos control 4 | 5 | API: 6 | Pidog.head_move_raw(target_angles, immediately=True, speed=50) 7 | 8 | head servo angles control with "raw servo angles", it will store the target_angles in a buffer, and the built-in head servo control thread 9 | will control the angle of the servos 10 | 11 | - target_angles n*3 2D list, angle arrangements of head servos, could be one group of angles, 12 | or multiple groups of angles, the order of the servos is: 13 | [[ yaw_servo, roll_servo, pitch_servo], [...], ...] 14 | 15 | - immediately bool, whether to execute the specified action immediately, if true: it will clear the buffer first, and store the target_angles, 16 | the set actions will be executed immediately; if false, the will add target_angles at the end of the buffer, the actions will be 17 | executed after previous actions have been executed 18 | 19 | - speed int, speed of action, 0 ~ 100 20 | 21 | Pidog.head_move(target_yrps, roll_comp=0, pitch_comp=0, immediately=True, speed=50) 22 | 23 | legs servo angles control with "relative ypr angles", You can set "roll_comp" and "pitch_comp" so that the initial position of the head is in a 24 | horizontal position to offset the influence of body tilt. In this way, the same set of c can be used for different body tilt angles 25 | 26 | - target_yrps n*3 2D list, relative angle arrangements of head servos 27 | 28 | - roll_comp angle compensation on roll axis 29 | 30 | - pitch_comp angle compensation on pitch axis 31 | 32 | - immediately bool, whether to execute the specified action immediately 33 | 34 | - speed int, speed of action, 0 ~ 100 35 | 36 | Pidog.is_head_done() 37 | whether all the head actions in the buffer to be executed 38 | 39 | Pidog.wait_head_done() 40 | wait for all the head actions in the buffer to be executed 41 | 42 | Pidog.head_stop() 43 | clear all the head actions of leg in the buffer, to make head servos stop 44 | 45 | ''' 46 | 47 | from pidog import Pidog 48 | import time 49 | 50 | my_dog = Pidog() 51 | 52 | 53 | # sit 54 | sit_action = [ 55 | [30, 60, -30, -60, 80, -45, -80, 45], 56 | ] 57 | my_dog.legs_move(sit_action, speed=30) 58 | # wait all legs actions done 59 | my_dog.wait_legs_done() 60 | 61 | 62 | # level-view by head_move_raw() 63 | # my_dog.head_move_raw([[0, 0, -30]], speed=80) 64 | 65 | # level-view by head_move() 66 | my_dog.head_move([[0, 0, 0]], pitch_comp=-30, speed=80) 67 | 68 | # actually "head_move_raw([[0, 0, -30]]" is the same as "head_move([[0, 0, 0]], pitch_comp=-30)" 69 | 70 | my_dog.wait_head_done() 71 | 72 | head_test_actions = [ # relative angles 73 | [0, 0, 0], [90, 0, 0], [0, 0, 0], [-90, 0, 0], [0, 0, 0], 74 | [0, 0, 0], [0, 60, 0], [0, 0, 0], [0, -60, 0], [0, 0, 0], 75 | [0, 0, 0], [0, 0, 45], [0, 0, 0], [0, 0, -60], [0, 0, 0], 76 | ] 77 | 78 | while True: 79 | my_dog.head_move(head_test_actions, pitch_comp=-30, speed=50) 80 | my_dog.wait_head_done() 81 | time.sleep(0.2) 82 | -------------------------------------------------------------------------------- /basic_examples/4_tail_control.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python3 2 | ''' tail control 3 | Basic use of tail servo control 4 | 5 | API: 6 | Pidog.tail_move(target_angles, immediately=True, speed=50) 7 | 8 | tail servo angles control, it will store the target_angles in a buffer, and the built-in tail servo control thread 9 | will control the angle of the tail servo 10 | 11 | - target_angles n*1 2D list, angle arrangements of tail servo, could be one group of angle, 12 | or multiple groups of angle, eg: 13 | [[30], [-30], [...], ...] 14 | 15 | - immediately bool, whether to execute the specified action immediately, if true: it will clear the buffer first, and store the target_angles, 16 | the set actions will be executed immediately; if false, the will add target_angles at the end of the buffer, the actions will be 17 | executed after previous actions have been executed 18 | 19 | - speed int, speed of action, 0 ~ 100 20 | 21 | Pidog.is_tail_done() 22 | whether all the tail actions in the buffer to be executed 23 | 24 | Pidog.wait_tail_done() 25 | wait for all the tail actions in the buffer to be executed 26 | 27 | Pidog.tail_stop() 28 | clear all the tail actions of leg in the buffer, to make tail servo stop 29 | 30 | ''' 31 | 32 | from pidog import Pidog 33 | import time 34 | 35 | my_dog = Pidog() 36 | 37 | # stand action 38 | stand_action = [ 39 | [25, 35, -25, -35, 35, 35, -35, -35], 40 | ] 41 | my_dog.legs_move(stand_action, speed=30) 42 | # wait all legs actions done 43 | my_dog.wait_legs_done() 44 | 45 | 46 | wag_tail_actions = [ 47 | [-30], [30], 48 | ] 49 | 50 | while True: 51 | my_dog.tail_move(wag_tail_actions, speed=100) 52 | my_dog.wait_tail_done() 53 | 54 | -------------------------------------------------------------------------------- /basic_examples/5_stop_actions.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python3 2 | ''' stop all actions 3 | 4 | 5 | API: 6 | Pidog.wait_all_done() 7 | wait for all the actions in the leg actions buffer, head buffer and tail buffer to be executed 8 | 9 | Pidog.body_stop() 10 | stop all the actions of legs, head and tail 11 | 12 | Pidog.stop_and_lie() 13 | stop all the actions of legs, head and tail, then reset to "lie" pose 14 | 15 | Pidog.close() 16 | stop all the actions, reset to "lie" pose, and close all the threads, 17 | usually used when exiting a program 18 | 19 | ''' 20 | 21 | from pidog import Pidog 22 | import time 23 | 24 | my_dog = Pidog() 25 | 26 | try: 27 | # push_up prepare 28 | push_up_prepare_action = [ 29 | [45, 35, -45, -35, 80, 70, -80, -70] 30 | ] 31 | my_dog.legs_move(push_up_prepare_action, speed=30) 32 | my_dog.head_move([[0, 0, 0]], pitch_comp=-10, speed=80) # head level 33 | my_dog.wait_all_done() # wait all the actions to be done 34 | time.sleep(0.5) 35 | 36 | # push_up 37 | push_up_action = [ 38 | [90, -30, -90, 30, 80, 70, -80, -70], 39 | [45, 35, -45, -35, 80, 70, -80, -70], 40 | ] 41 | head_up_down_action = [ 42 | [0, 0, -30], 43 | [0, 0, 20], 44 | ] 45 | # fill action buffers 46 | for _ in range(20): 47 | my_dog.legs_move(push_up_action, immediately=False, speed=50) 48 | my_dog.head_move(head_up_down_action, pitch_comp=-10, immediately=False, speed=50) 49 | print(f"legs buffer length (start): {len(my_dog.legs_action_buffer)}") 50 | time.sleep(5) 51 | print(f"legs buffer length (5s): {len(my_dog.legs_action_buffer)}") 52 | 53 | my_dog.body_stop() 54 | print(f"legs buffer length (stop): {len(my_dog.legs_action_buffer)}") 55 | time.sleep(1) 56 | 57 | except KeyboardInterrupt: 58 | pass 59 | except Exception as e: 60 | print(f"\033[31mERROR: {e}\033[m") 61 | finally: 62 | print("closing ...") 63 | my_dog.close() 64 | 65 | -------------------------------------------------------------------------------- /basic_examples/6_do_preset_actions.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python3 2 | ''' do preset actions 3 | 4 | API: 5 | Pidog.do_action(action_name, step_count=1, speed=50): 6 | do preset actions 7 | 8 | - action_name str, name of preset actions, eg: "stand", "sit", "forward", 9 | more to see: ../pidogg/actions_dictionary.py 10 | - step_count int, times to perform the action 11 | - speed int, speed of action, 0 ~ 100 12 | 13 | more to see: ../pidog/actions_dictionary.py 14 | 15 | ''' 16 | 17 | from pidog import Pidog 18 | import time 19 | 20 | my_dog = Pidog() 21 | 22 | try: 23 | my_dog.do_action("stand", speed=60) 24 | my_dog.wait_all_done() 25 | 26 | my_dog.do_action("push_up", step_count=10, speed=60) 27 | my_dog.wait_all_done() 28 | 29 | my_dog.do_action("half_sit", speed=60) 30 | my_dog.wait_all_done() 31 | 32 | my_dog.do_action("wag_tail", step_count=80,speed=90) 33 | my_dog.do_action("tilting_head", step_count=5, speed=20) 34 | my_dog.wait_head_done() 35 | my_dog.body_stop() 36 | 37 | except KeyboardInterrupt: 38 | pass 39 | except Exception as e: 40 | print(f"\033[31mERROR: {e}\033[m") 41 | finally: 42 | print("closing ...") 43 | my_dog.close() 44 | -------------------------------------------------------------------------------- /basic_examples/7_sound_effect.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python3 2 | ''' play sound effecfs 3 | Note that you need to run with "sudo" 4 | API: 5 | Pidog.speak(name, volume=100) 6 | play sound effecf in the file "../sounds" 7 | - name str, file name of sound effect, no suffix required, eg: "angry" 8 | - volume int, volume 0-100, default 100 9 | ''' 10 | from pidog import Pidog 11 | import os 12 | import time 13 | 14 | # change working directory 15 | abspath = os.path.abspath(os.path.dirname(__file__)) 16 | # print(abspath) 17 | os.chdir(abspath) 18 | 19 | my_dog = Pidog() 20 | 21 | print("\033[033mNote that you need to run with \"sudo\", otherwise there may be no sound.\033[m") 22 | 23 | # my_dog.speak("angry") 24 | # time.sleep(2) 25 | 26 | for name in os.listdir('../sounds'): 27 | name = name.split('.')[0] # remove suffix 28 | print(name) 29 | my_dog.speak(name) 30 | # my_dog.speak(name, volume=50) 31 | time.sleep(3) # Note that the duration of each sound effect is different 32 | print("closing ...") 33 | my_dog.close() -------------------------------------------------------------------------------- /basic_examples/8_ultrasonic_read.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python3 2 | ''' read ultrasonic distance data 3 | 4 | API: 5 | Pidog.read_distance() 6 | return the distance read by ultrasound 7 | - return float 8 | 9 | ''' 10 | 11 | from pidog import Pidog 12 | import time 13 | 14 | my_dog = Pidog() 15 | while True: 16 | distance = my_dog.read_distance() 17 | distance = round(distance,2) 18 | print(f"Distance: {distance} cm") 19 | time.sleep(0.5) 20 | -------------------------------------------------------------------------------- /basic_examples/9_rgb_control.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python3 2 | ''' rgb strip style control 3 | 4 | API: 5 | Pidog.rgb_strip.set_mode(style='breath', color='white', bps=1, brightness=1): 6 | 7 | Set the display mode of the rgb light strip 8 | 9 | - style str, display style, could be: "breath", "boom", "bark", "speak", "listen" 10 | - color str, list or hex, display color, 11 | could be: 16-bit rgb value, eg: #a10a0a; 12 | or 1*3 list of rgb, eg: [255, 100, 80]; 13 | or predefined colors:"white", "black", "red", "yellow", "green", "blue", "cyan", "magenta", "pink" 14 | - bps float or int, beats per second, this number of style actions executed per second 15 | 16 | Pidog.rgb_strip.close(): 17 | - turn off rgb display 18 | 19 | 20 | more to see: ../pidog/rgb_strip.py 21 | 22 | ''' 23 | 24 | from pidog import Pidog 25 | import time 26 | 27 | my_dog = Pidog() 28 | 29 | while True: 30 | # style="breath", color="pink" 31 | my_dog.rgb_strip.set_mode(style="breath", color='pink') 32 | time.sleep(3) 33 | 34 | # style:"listen", color=[0, 255, 255] 35 | my_dog.rgb_strip.set_mode(style="listen", color=[0, 255, 255]) 36 | time.sleep(3) 37 | 38 | # style:"boom", color="#a10a0a" 39 | my_dog.rgb_strip.set_mode(style="boom", color="#a10a0a") 40 | time.sleep(3) 41 | 42 | # style:"boom", color="#a10a0a", brightness=0.5, bps=2.5 43 | my_dog.rgb_strip.set_mode(style="boom", color="#a10a0a", bps=2.5, brightness=0.5) 44 | time.sleep(3) 45 | 46 | # close 47 | my_dog.rgb_strip.close() 48 | time.sleep(2) 49 | 50 | -------------------------------------------------------------------------------- /bin/pidog_app: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | # check permission 4 | # ======================== 5 | if [ $(id -u) -ne 0 ]; then 6 | printf "Script must be run as root. Try 'sudo pidog_app