├── README.md ├── hexapod.php ├── hexapod.py ├── hexapod.py.save └── index.html /README.md: -------------------------------------------------------------------------------- 1 | # Hexapod Walker using Raspberry Pi 2 | 3 | ### Prerequisites: 4 | 1. Python 2.7 or later 5 | 2. Apache Server 6 | 3. pigpiod library for servo control 7 | 8 | Instructions: 9 | 1. Browse to your Pi's IP address. 10 | 2. Control using the buttons! 11 | 12 | The three-servo hexapod walker design is original seen in the book PIC Robotics: A Beginner's Guide to Robotics Projects Using the PICmicro by John Iovine 13 | 14 | -------------------------------------------------------------------------------- /hexapod.php: -------------------------------------------------------------------------------- 1 | 28 | -------------------------------------------------------------------------------- /hexapod.py: -------------------------------------------------------------------------------- 1 | #Hexapod.py 2 | #Code for Hexapod Walker by R. Pelayo 3 | 4 | #!/usr/bin/python 5 | 6 | import RPi.GPIO as GPIO 7 | import pigpio 8 | import time 9 | import sys 10 | import os 11 | import signal 12 | GPIO.setmode(GPIO.BCM) 13 | GPIO.setwarnings(False) 14 | 15 | #pin definitions 16 | tilt = 4 17 | br = 21 18 | bl = 6 19 | trig = 23 20 | echo = 24 21 | head = 26 22 | 23 | #setup pins for ultrasonic sensor 24 | GPIO.setup(trig, GPIO.OUT) 25 | GPIO.setup(echo, GPIO.IN) 26 | 27 | #initialize pi object 28 | pi = pigpio.pi() 29 | 30 | #move backward 31 | def backward(): 32 | pi.set_servo_pulsewidth(tilt, 800) 33 | time.sleep(0.15) 34 | pi.set_servo_pulsewidth(bl, 800) 35 | time.sleep(0.15) 36 | pi.set_servo_pulsewidth(tilt, 2000) 37 | time.sleep(0.15) 38 | pi.set_servo_pulsewidth(br, 1800) 39 | time.sleep(0.15) 40 | pi.set_servo_pulsewidth(tilt, 1500) 41 | time.sleep(0.15) 42 | pi.set_servo_pulsewidth(bl, 1500) 43 | time.sleep(0.15) 44 | pi.set_servo_pulsewidth(br, 1500) 45 | time.sleep(0.15) 46 | return; 47 | #move forward 48 | def forward(): 49 | pi.set_servo_pulsewidth(tilt, 800) 50 | time.sleep(0.15) 51 | pi.set_servo_pulsewidth(bl, 1800) 52 | time.sleep(0.15) 53 | pi.set_servo_pulsewidth(tilt, 2000) 54 | time.sleep(0.15) 55 | pi.set_servo_pulsewidth(br, 800) 56 | time.sleep(0.15) 57 | pi.set_servo_pulsewidth(tilt, 1500) 58 | time.sleep(0.15) 59 | pi.set_servo_pulsewidth(bl, 1500) 60 | time.sleep(0.15) 61 | pi.set_servo_pulsewidth(br, 1500) 62 | time.sleep(0.15) 63 | return; 64 | #move left 65 | def left(): 66 | pi.set_servo_pulsewidth(tilt, 800) 67 | time.sleep(0.15) 68 | pi.set_servo_pulsewidth(bl, 1800) 69 | time.sleep(0.15) 70 | pi.set_servo_pulsewidth(tilt, 2000) 71 | time.sleep(0.15) 72 | pi.set_servo_pulsewidth(br, 1800) 73 | time.sleep(0.15) 74 | pi.set_servo_pulsewidth(tilt, 1500) 75 | time.sleep(0.15) 76 | pi.set_servo_pulsewidth(bl, 1500) 77 | time.sleep(0.15) 78 | pi.set_servo_pulsewidth(br, 1500) 79 | time.sleep(0.15) 80 | return; 81 | #move right 82 | def right(): 83 | pi.set_servo_pulsewidth(tilt, 800) 84 | time.sleep(0.15) 85 | pi.set_servo_pulsewidth(bl, 800) 86 | time.sleep(0.15) 87 | pi.set_servo_pulsewidth(tilt, 2000) 88 | time.sleep(0.15) 89 | pi.set_servo_pulsewidth(br, 800) 90 | time.sleep(0.15) 91 | pi.set_servo_pulsewidth(tilt, 1500) 92 | time.sleep(0.15) 93 | pi.set_servo_pulsewidth(bl, 1500) 94 | time.sleep(0.15) 95 | pi.set_servo_pulsewidth(br, 1500) 96 | time.sleep(0.15) 97 | return; 98 | #stop 99 | def stop(): 100 | pi.set_servo_pulsewidth(tilt, 0) 101 | time.sleep(0.15) 102 | pi.set_servo_pulsewidth(bl, 0) 103 | time.sleep(0.15) 104 | pi.set_servo_pulsewidth(br, 0) 105 | time.sleep(0.15) 106 | 107 | return 108 | #action when obstacle is detected 109 | def obstacleDetected(): 110 | backward() 111 | backward() 112 | backward() 113 | backward() 114 | backward() 115 | right() 116 | right() 117 | right() 118 | 119 | return 120 | #action to turn head for scanning obstacles 121 | def turnHead(): 122 | pi.set_servo_pulsewidth(head, 700) 123 | time.sleep(0.5) 124 | pi.set_servo_pulsewidth(head, 2100) 125 | time.sleep(0.5) 126 | pi.set_servo_pulsewidth(head, 1500) 127 | time.sleep(0.5) 128 | 129 | return 130 | #autonomous mode 131 | def autoMode(): 132 | print ("Running in auto mode!") 133 | 134 | turnHead() 135 | 136 | time.sleep(0.5) 137 | GPIO.output(trig, 0) 138 | time.sleep(0.5) 139 | 140 | GPIO.output(trig,1) 141 | time.sleep(0.00001) 142 | GPIO.output(trig,0) 143 | 144 | while GPIO.input(echo) == 0: 145 | pulse_start = time.time() 146 | 147 | while GPIO.input(echo) == 1: 148 | pulse_end = time.time() 149 | 150 | pulse_duration = pulse_end - pulse_start 151 | 152 | distance = pulse_duration * 17150 153 | 154 | distance = round(distance, 2) 155 | 156 | if distance > 1 and distance < 35: 157 | obstacleDetected() 158 | else: 159 | forward() 160 | forward() 161 | forward() 162 | 163 | pi.set_servo_pulsewidth(head, 2100) 164 | time.sleep(0.5) 165 | return 166 | #manual mode 167 | def manualMode(): 168 | 169 | move = str(sys.argv[2]) 170 | 171 | if move == "F" or move == "f": 172 | print("Moving forward!") 173 | forward() 174 | elif move == "B" or move == "b": 175 | print("Moving backward!") 176 | backward() 177 | elif move == "L" or move == "l": 178 | print("Moving left!") 179 | left() 180 | elif move == "R" or move == "r": 181 | print("Moving right!") 182 | right() 183 | else: 184 | print("Invalid argument!") 185 | 186 | return 187 | #main routine 188 | def main(): 189 | opt = str(sys.argv[1]) 190 | 191 | if opt == "A" or opt == "a": 192 | autoMode() 193 | elif opt == "M" or opt == "m": 194 | manualMode() 195 | 196 | return 197 | 198 | while True: 199 | main() 200 | 201 | GPIO.cleanup() 202 | pi.stop() 203 | 204 | -------------------------------------------------------------------------------- /hexapod.py.save: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rppelayo/raspberry-pi-hexapod-walker/fbf1d1d23a57623d13bfe7d50f3b44af9cd71819/hexapod.py.save -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | RaspberryPi Hexapod 5 | 43 | 101 | 102 | 103 |
104 |
105 |
106 | 107 |
108 |
109 | 110 | 111 | 112 | 113 |
114 |
115 |
116 | 117 | 118 | 119 | 120 | 121 | 122 |
123 | 124 | 125 | 126 | --------------------------------------------------------------------------------