|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## License\n", |
| 8 | + " GoPiGo for the Raspberry Pi: an open source robotics platform for the Raspberry Pi.\n", |
| 9 | + " Copyright (C) 2017 Dexter Industries\n", |
| 10 | + "This program is free software: you can redistribute it and/or modify\n", |
| 11 | + "it under the terms of the GNU General Public License as published by\n", |
| 12 | + "the Free Software Foundation, either version 3 of the License, or\n", |
| 13 | + "(at your option) any later version.\n", |
| 14 | + "This program is distributed in the hope that it will be useful,\n", |
| 15 | + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n", |
| 16 | + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n", |
| 17 | + "GNU General Public License for more details.\n", |
| 18 | + "You should have received a copy of the GNU General Public License\n", |
| 19 | + "along with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>." |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "code", |
| 24 | + "execution_count": null, |
| 25 | + "metadata": {}, |
| 26 | + "outputs": [], |
| 27 | + "source": [ |
| 28 | + "import struct\n", |
| 29 | + "import sys\n", |
| 30 | + "import signal\n", |
| 31 | + "from time import sleep\n", |
| 32 | + "from easygopigo3 import EasyGoPiGo3\n", |
| 33 | + "from builtins import input\n", |
| 34 | + "import threading\n", |
| 35 | + "import gopigo3\n", |
| 36 | + "import atexit\n", |
| 37 | + "\n", |
| 38 | + "debug = False # Print raw values when debugging\n", |
| 39 | + "signal_not_called = True # used to stop reading values from the mouse\n", |
| 40 | + "\n", |
| 41 | + "MOUSE_THRESH = 20 # the mouse's sensitivity - the bigger the number, the less sensible the mouse. Idem vice-versa.\n", |
| 42 | + "\n", |
| 43 | + "# ensure that upon exit the robot stops moving\n", |
| 44 | + "def cleanup_func(gopigo3):\n", |
| 45 | + " print(\"stopping GoPiGo3\")\n", |
| 46 | + " gopigo3.stop()\n", |
| 47 | + "\n", |
| 48 | + "# upon exit, stop reading values from the mouse\n", |
| 49 | + "# used in conjunction with signal_not_called var\n", |
| 50 | + "def signal_handler(signal, frame):\n", |
| 51 | + " print(\"stop reading mouse values\")\n", |
| 52 | + " global signal_not_called\n", |
| 53 | + " signal_not_called = False\n", |
| 54 | + "\n", |
| 55 | + "# bLeft is 1 if the left mouse button is pressed and 0 if it isn't\n", |
| 56 | + "# bMiddle is 1 if the middle mouse button is pressed and 0 if it isn't\n", |
| 57 | + "# bRight is 1 if the right mouse button is pressed and 0 if it isn't\n", |
| 58 | + "# x is the position of the mouse on the x axis\n", |
| 59 | + "# y is the position of the mouse on the y axis\n", |
| 60 | + "def getMouseValues(file_input):\n", |
| 61 | + "\n", |
| 62 | + " buf = file_input.read(3)\n", |
| 63 | + "\n", |
| 64 | + " # if ran with Python 3\n", |
| 65 | + " # ord function will throw an exception, since\n", |
| 66 | + " # buf[0] already is an integer, as opposed in Python 2\n", |
| 67 | + " # where buf[0] is a string\n", |
| 68 | + " try:\n", |
| 69 | + " button = ord(buf[0])\n", |
| 70 | + " except TypeError as msg:\n", |
| 71 | + " button = buf[0]\n", |
| 72 | + " if debug is True:\n", |
| 73 | + " print(str(msg))\n", |
| 74 | + "\n", |
| 75 | + " print(button)\n", |
| 76 | + " left_button = (button & 0x1) > 0\n", |
| 77 | + " middle_button = (button & 0x4) > 0\n", |
| 78 | + " right_button = (button & 0x2) > 0\n", |
| 79 | + " x_axis, y_axis = struct.unpack(\"bb\", buf[1:])\n", |
| 80 | + "\n", |
| 81 | + " if debug is True:\n", |
| 82 | + " print(\"Left but: {}, Middle but: {}, Right but: {}, x pos: {}, y pos: {}\".format(left_button, middle_button, right_button, x_axis, y_axis))\n", |
| 83 | + "\n", |
| 84 | + " return (left_button, middle_button, right_button, x_axis, y_axis)\n", |
| 85 | + "\n", |
| 86 | + "\n", |
| 87 | + "def Main():\n", |
| 88 | + "\n", |
| 89 | + " print(\" _____ _____ _ _____ ____ \")\n", |
| 90 | + " print(\" / ____| | __ (_)/ ____| |___ \\ \")\n", |
| 91 | + " print(\" | | __ ___ | |__) || | __ ___ __) |\")\n", |
| 92 | + " print(\" | | |_ |/ _ \\| ___/ | | |_ |/ _ \\ |__ < \")\n", |
| 93 | + " print(\" | |__| | (_) | | | | |__| | (_) | ___) |\")\n", |
| 94 | + " print(\" \\_____|\\___/|_| |_|\\_____|\\___/ |____/ \")\n", |
| 95 | + " print(\" \")\n", |
| 96 | + "\n", |
| 97 | + " print(\"To move the robot around using the mouse buttons press 1 and enter.\")\n", |
| 98 | + " print(\"To move the robot around using the movements of the mouse press 2 and enter.\")\n", |
| 99 | + "\n", |
| 100 | + " # read data from the keyboard\n", |
| 101 | + " # if it fails reading the right values, then the script exits\n", |
| 102 | + " try:\n", |
| 103 | + " choice = int(input(\"choice (1/2) = \"))\n", |
| 104 | + " except ValueError:\n", |
| 105 | + " print(\"Invalid number read\")\n", |
| 106 | + " sys.exit(1)\n", |
| 107 | + "\n", |
| 108 | + " if not (choice == 1 or choice == 2):\n", |
| 109 | + " print(\"Invalid number entered\")\n", |
| 110 | + " sys.exit(1)\n", |
| 111 | + "\n", |
| 112 | + " # now the choice var can either be 1 or 2\n", |
| 113 | + " # show different menus depending on the choice var\n", |
| 114 | + " print(\"\\nWith this script you can control your GoPiGo3 robot with a wireless mouse.\")\n", |
| 115 | + " if choice == 1:\n", |
| 116 | + " print(\"1. Left + Right buttons of the mouse - move the GoPiGo3 forward\")\n", |
| 117 | + " print(\"2. Left button of the mouse - move the GoPiGo3 to the left\")\n", |
| 118 | + " print(\"3. Right button of the mouse - move the GoPiGo3 to the right\")\n", |
| 119 | + " print(\"4. Middle button of the mouse - move the GoPiGo3 backward\")\n", |
| 120 | + " else:\n", |
| 121 | + " print(\"1. Move the mouse forward - for moving the GoPiGo3 forward\")\n", |
| 122 | + " print(\"2. Move the mouse backward - for moving the GoPiGo3 backward\")\n", |
| 123 | + " print(\"3. Move the mouse to the left - for rotating the GoPiGo3 to the left\")\n", |
| 124 | + " print(\"4. Move the mouse to the right - for rotating the GoPiGo3 to the right\")\n", |
| 125 | + "\n", |
| 126 | + " # Wait for an input to start\n", |
| 127 | + " input(\"\\nPress Enter to start\")\n", |
| 128 | + "\n", |
| 129 | + " # create an instance of the EasyGoPiGo3 class\n", |
| 130 | + " # if it fails instantiating the object, then the scripts exits\n", |
| 131 | + " try:\n", |
| 132 | + " robot = EasyGoPiGo3()\n", |
| 133 | + "\n", |
| 134 | + " except IOError:\n", |
| 135 | + " print(\"GoPiGo3 not detected\")\n", |
| 136 | + " sys.exit(1)\n", |
| 137 | + "\n", |
| 138 | + " except gopigo3.FirmwareVersionError:\n", |
| 139 | + " print(\"Please update your GoPiGo3 firmware\")\n", |
| 140 | + " sys.exit(1)\n", |
| 141 | + "\n", |
| 142 | + " except Exception:\n", |
| 143 | + " print(\"Something went wrong\")\n", |
| 144 | + " sys.exit(1)\n", |
| 145 | + "\n", |
| 146 | + " # stops the robot from moving when exiting the script\n", |
| 147 | + " # the cleanup_func is called after the signal_handler function\n", |
| 148 | + " atexit.register(cleanup_func, gopigo3 = robot)\n", |
| 149 | + "\n", |
| 150 | + " print(\"\\nIn order to stop the script, press CTRL-C and move your mouse a little bit\")\n", |
| 151 | + "\n", |
| 152 | + " # open file for reading the continuous stream of data from the mouse\n", |
| 153 | + " with open(\"/dev/input/mice\", \"rb\") as mouse_input:\n", |
| 154 | + "\n", |
| 155 | + " left_button = 0\n", |
| 156 | + " middle_button = 0\n", |
| 157 | + " right_button = 0\n", |
| 158 | + " x_axis = 0\n", |
| 159 | + " y_axis = 0\n", |
| 160 | + "\n", |
| 161 | + " # as long as CTRL-C hasn't been pressed\n", |
| 162 | + " while signal_not_called:\n", |
| 163 | + "\n", |
| 164 | + " # read the mouse's values\n", |
| 165 | + " # this is a blocking function\n", |
| 166 | + " (left_button, middle_button, right_button, x_axis, y_axis) = getMouseValues(mouse_input)\n", |
| 167 | + "\n", |
| 168 | + " # if we have the first choice (see the menu)\n", |
| 169 | + " if choice == 1:\n", |
| 170 | + "\n", |
| 171 | + " # when both the mouse's buttons are pressed\n", |
| 172 | + " # move forward\n", |
| 173 | + " if left_button == True and right_button == True:\n", |
| 174 | + " robot.forward()\n", |
| 175 | + " # when just the left button is pressed\n", |
| 176 | + " # move to the left\n", |
| 177 | + " elif left_button == True and right_button == False:\n", |
| 178 | + " robot.left()\n", |
| 179 | + " # when just the right button is pressed\n", |
| 180 | + " # move to the right\n", |
| 181 | + " elif left_button == False and right_button == True:\n", |
| 182 | + " robot.right()\n", |
| 183 | + " # when the middle button is pressed\n", |
| 184 | + " # move backward\n", |
| 185 | + " elif middle_button == True:\n", |
| 186 | + " robot.backward()\n", |
| 187 | + " # when no button is pressed\n", |
| 188 | + " # stop the robot from moving\n", |
| 189 | + " elif middle_button == False or (left_button == False and right_button == False):\n", |
| 190 | + " robot.stop()\n", |
| 191 | + "\n", |
| 192 | + " # if we have the second choice (see the menu)\n", |
| 193 | + " else:\n", |
| 194 | + "\n", |
| 195 | + " # if the mouse is moved to the left\n", |
| 196 | + " # then move the robot to the left\n", |
| 197 | + " if x_axis < -MOUSE_THRESH:\n", |
| 198 | + " robot.left()\n", |
| 199 | + " # if the mouse is moved to the right\n", |
| 200 | + " # then move the robot to the right\n", |
| 201 | + " elif x_axis > MOUSE_THRESH:\n", |
| 202 | + " robot.right()\n", |
| 203 | + " # if the mouse is moved backward\n", |
| 204 | + " # then move the robot backward\n", |
| 205 | + " elif y_axis < -MOUSE_THRESH:\n", |
| 206 | + " robot.backward()\n", |
| 207 | + " # if the mouse is moved forward\n", |
| 208 | + " # then move the robot forward\n", |
| 209 | + " elif y_axis > MOUSE_THRESH:\n", |
| 210 | + " robot.forward()\n", |
| 211 | + " # if the mouse is not moving in any direction\n", |
| 212 | + " # then stop the robot from moving\n", |
| 213 | + " else:\n", |
| 214 | + " robot.stop()\n", |
| 215 | + "\n", |
| 216 | + " sleep(0.10)\n", |
| 217 | + "\n", |
| 218 | + "if __name__ == \"__main__\":\n", |
| 219 | + " # when CTRL-C is pressed, this is will ensure signal_handler is called\n", |
| 220 | + " signal.signal(signal.SIGINT, signal_handler)\n", |
| 221 | + " Main()\n" |
| 222 | + ] |
| 223 | + }, |
| 224 | + { |
| 225 | + "cell_type": "code", |
| 226 | + "execution_count": null, |
| 227 | + "metadata": {}, |
| 228 | + "outputs": [], |
| 229 | + "source": [] |
| 230 | + } |
| 231 | + ], |
| 232 | + "metadata": { |
| 233 | + "kernelspec": { |
| 234 | + "display_name": "Python 3", |
| 235 | + "language": "python", |
| 236 | + "name": "python3" |
| 237 | + }, |
| 238 | + "language_info": { |
| 239 | + "codemirror_mode": { |
| 240 | + "name": "ipython", |
| 241 | + "version": 3 |
| 242 | + }, |
| 243 | + "file_extension": ".py", |
| 244 | + "mimetype": "text/x-python", |
| 245 | + "name": "python", |
| 246 | + "nbconvert_exporter": "python", |
| 247 | + "pygments_lexer": "ipython3", |
| 248 | + "version": "3.4.3" |
| 249 | + } |
| 250 | + }, |
| 251 | + "nbformat": 4, |
| 252 | + "nbformat_minor": 2 |
| 253 | +} |
0 commit comments