-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial README; posted two new scripts
- Loading branch information
Showing
3 changed files
with
255 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,22 @@ | ||
# scripts | ||
Miscellaneous Raspberry Pi and Arduino scripts | ||
# scripts : Miscellaneous Raspberry Pi and Arduino scripts | ||
|
||
* **pin_check.py**: displays a continually-updating console display showing the status of a Raspberry Pi 3 B+'s pins. | ||
* **pink_led.py**: grabs a snapshot of a 640x480 Raspberry Pi camera and compares the 'color distance' between each pixel and a fixed color, displaying the result to the console. | ||
|
||
|
||
## Support & Liability | ||
|
||
These files come with no promise of support or liability. Use at your own risk. | ||
|
||
|
||
## Further Information | ||
|
||
This project is part of the _New Zealand Personal Robotics (NZPRG)_ "Robot Operating System", not to be confused with other "ROS" projects. | ||
For more information check out the [NZPRG Blog](https://robots.org.nz/) and [NZPRG Wiki](https://service.robots.org.nz/wiki/). | ||
|
||
|
||
## Copyright & License | ||
|
||
All contents (including software, documentation and images) Copyright 2020 by Murray Altheim, All Rights Reserved. | ||
|
||
Software and documentation are distributed under the MIT License, see LICENSE file included with project. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2020 by Murray Altheim. All rights reserved. This file is part of | ||
# the Robot OS project and is released under the "Apache Licence, Version 2.0". | ||
# Please see the LICENSE file included as part of this package. | ||
# | ||
# author: altheim | ||
# created: 2020-01-18 | ||
# modified: 2020-02-06 | ||
# | ||
# This script displays the GPIO pins of the Raspberry Pi 3 B+ and hilights | ||
# any whose input value is high or low (depending on setting). | ||
|
||
import time, sys, itertools | ||
import RPi.GPIO as GPIO | ||
from colorama import init, Fore, Style | ||
init() | ||
|
||
# .............................................................................. | ||
class Pin(): | ||
def __init__(self, pin, label): | ||
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) | ||
# https://raspi.tv/2013/rpi-gpio-basics-6-using-inputs-and-outputs-together-with-rpi-gpio-pull-ups-and-pull-downs | ||
# pull_up_down=GPIO.PUD_DOWN. If you want/need pull-up you can change the GPIO.PUD_DOWN for GPIO.PUD_UP | ||
self._pin = pin | ||
self._label = label | ||
|
||
def get(self): | ||
''' | ||
Change the NORMAL and BRIGHT as desired, | ||
''' | ||
if GPIO.input(self._pin): # if low | ||
return Fore.GREEN + Style.NORMAL + self._label | ||
else: | ||
return Fore.GREEN + Style.BRIGHT + self._label | ||
|
||
# .............................................................................. | ||
def main(): | ||
try: | ||
|
||
TITLE = 'Raspberry Pi 3 B+ Pinout' | ||
DELAY_TIME_SEC = 0.5 | ||
GND = Fore.BLACK + Style.DIM + 'GND ' | ||
CYAN_NORMAL = Fore.CYAN + Style.NORMAL | ||
ROWS = 35 | ||
NNNN = '\n' * ROWS # as tall as your screen | ||
INDENT = ' ' | ||
|
||
GPIO.setmode(GPIO.BOARD) | ||
|
||
# column 1 .............. | ||
_pin07 = Pin(7, 'GPIO4') | ||
_pin11 = Pin(11,'GPIO17') | ||
_pin13 = Pin(13,'GPIO27') | ||
_pin15 = Pin(15,'GPIO22') | ||
_pin19 = Pin(19,'GPIO10') | ||
_pin21 = Pin(21,'GPIO9') | ||
_pin23 = Pin(23,'GPIO11') | ||
# GND 25 | ||
# SC0 27 | ||
_pin29 = Pin(29,'GPIO5') | ||
_pin31 = Pin(31,'GPIO6') | ||
_pin33 = Pin(33,'GPIO13') | ||
_pin35 = Pin(35,'GPIO19') | ||
_pin37 = Pin(37,'GPIO26') | ||
# GND 39 | ||
|
||
# column 2 .............. | ||
_pin08 = Pin(8, 'GPIO14') | ||
_pin10 = Pin(10,'GPIO15') | ||
_pin12 = Pin(12,'GPIO18') | ||
# GND 14 | ||
_pin16 = Pin(16,'GPIO23') | ||
_pin18 = Pin(18,'GPIO24') | ||
# GND 20 | ||
_pin22 = Pin(22,'GPIO25') | ||
_pin24 = Pin(24,'GPIO8') | ||
_pin26 = Pin(26,'GPIO7') | ||
# SC1 28 | ||
# GND 30 | ||
_pin32 = Pin(32,'GPIO12') | ||
# GND 34 | ||
_pin36 = Pin(36,'GPIO16') | ||
_pin38 = Pin(38,'GPIO20') | ||
_pin40 = Pin(40,'GPIO21') | ||
|
||
|
||
_counter = itertools.count() | ||
|
||
while True: | ||
print(NNNN) | ||
print(INDENT + Fore.WHITE + Style.BRIGHT + '\t ' + TITLE + Style.RESET_ALL) | ||
print('') | ||
print(INDENT + Fore.RED + Style.NORMAL + '3.3V ' + CYAN_NORMAL + '\t(01) | (02)\t' + Fore.MAGENTA + Style.NORMAL + '5V' + Style.RESET_ALL) | ||
print(INDENT + Fore.BLUE + Style.BRIGHT + 'SDA ' + CYAN_NORMAL + '\t(03) | (04)\t' + Fore.MAGENTA + Style.NORMAL + '5V' + Style.RESET_ALL) | ||
print(INDENT + Fore.BLUE + Style.BRIGHT + 'SCL ' + CYAN_NORMAL + '\t(05) | (06)\t' + GND + Style.RESET_ALL) | ||
print(INDENT + _pin07.get() + CYAN_NORMAL + '\t(07) | (08)\t' + _pin08.get() + ' (TXD)' + Style.RESET_ALL) | ||
print(INDENT + GND + CYAN_NORMAL + '\t(09) | (10)\t' + _pin10.get() + ' (RXD)' + Style.RESET_ALL) | ||
print(INDENT + _pin11.get() + CYAN_NORMAL + '\t(11) | (12)\t' + _pin12.get() + ' (PWM0)' + Style.RESET_ALL) | ||
print(INDENT + _pin13.get() + CYAN_NORMAL + '\t(13) | (14)\t' + GND + Style.RESET_ALL) | ||
print(INDENT + _pin15.get() + CYAN_NORMAL + '\t(15) | (16)\t' + _pin16.get() + Style.RESET_ALL) | ||
print(INDENT + Fore.RED + Style.NORMAL + '3.3V ' + CYAN_NORMAL + '\t(17) | (18)\t' + _pin18.get() + Style.RESET_ALL) | ||
print(INDENT + _pin19.get() + Fore.BLACK + '/MOSI' + CYAN_NORMAL + '\t(19) | (20)\t' + GND + Style.RESET_ALL) | ||
print(INDENT + _pin21.get() + Fore.BLACK + '/MISO' + CYAN_NORMAL + '\t(21) | (22)\t' + _pin22.get() + Style.RESET_ALL) | ||
print(INDENT + _pin23.get() + Fore.BLACK + '/SCLK' + CYAN_NORMAL + '\t(23) | (24)\t' + _pin24.get() + Fore.BLACK + '/CE0' + Style.RESET_ALL) | ||
print(INDENT + GND + CYAN_NORMAL + '\t(25) | (26)\t' + _pin26.get() + Fore.BLACK + '/CE1' + Style.RESET_ALL) | ||
print(INDENT + Fore.CYAN + Style.BRIGHT + 'SC0 ' + CYAN_NORMAL + '\t(27) | (28)\t' + Fore.CYAN + Style.BRIGHT + 'SC1' + Style.RESET_ALL) | ||
print(INDENT + _pin29.get() + CYAN_NORMAL + '\t(29) | (30)\t' + GND + Style.RESET_ALL) | ||
print(INDENT + _pin31.get() + CYAN_NORMAL + '\t(31) | (32)\t' + _pin32.get() + Style.RESET_ALL) | ||
print(INDENT + _pin33.get() + CYAN_NORMAL + '\t(33) | (34)\t' + GND + Style.RESET_ALL) | ||
print(INDENT + _pin35.get() + CYAN_NORMAL + '\t(35) | (36)\t' + _pin36.get() + Style.RESET_ALL) | ||
print(INDENT + _pin37.get() + CYAN_NORMAL + '\t(37) | (38)\t' + _pin38.get() + Style.RESET_ALL) | ||
print(INDENT + GND + CYAN_NORMAL + '\t(39) | (40)\t' + _pin40.get() + Style.RESET_ALL) | ||
|
||
count = next(_counter) | ||
_style = Style.BRIGHT if ( count % 2 == 0 ) else Style.DIM | ||
print('\n\n' + Fore.BLACK + _style + INDENT + Style.NORMAL + '\tpoll {} \tType Ctrl-C to exit.'.format(count) + Style.RESET_ALL) | ||
time.sleep(DELAY_TIME_SEC) | ||
|
||
|
||
except KeyboardInterrupt: | ||
print('Ctrl-C caught, exiting...') | ||
sys.exit(0) | ||
|
||
|
||
if __name__== "__main__": | ||
main() | ||
|
||
|
||
#EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2020 by Murray Altheim. All rights reserved. This file is part of | ||
# the Robot Operating System project and is released under the "Apache Licence, | ||
# Version 2.0". Please see the LICENSE file included as part of this package. | ||
# | ||
# author: Murray Altheim | ||
# created: 2020-05-19 | ||
# modified: 2020-05-19 | ||
# see: https://robots.org.nz/2020/05/19/four-corners/ | ||
# This script is used as a possible solution for the Four Corners Competition, | ||
# as a way for a robot to locate the trajectory to a distant target. | ||
# | ||
|
||
import math, colorsys, traceback | ||
import picamera | ||
import picamera.array | ||
from picamera.array import PiRGBArray | ||
|
||
from colorama import init, Fore, Style | ||
init() | ||
|
||
PINK_RGB = [151, 55, 180] # hue = 286 | ||
|
||
def color_distance(e0, e1): | ||
hsv0 = colorsys.rgb_to_hsv(e0[0], e0[1], e0[2]) | ||
hsv1 = colorsys.rgb_to_hsv(e1[0], e1[1], e1[2]) | ||
dh = min(abs(hsv1[0]-hsv0[0]), 360-abs(hsv1[0]-hsv0[0])) / 180.0 | ||
ds = abs(hsv1[1]-hsv0[1]) | ||
dv = abs(hsv1[2]-hsv0[2]) / 255.0 | ||
distance = math.sqrt(dh*dh+ds*ds+dv*dv) | ||
return distance | ||
|
||
|
||
def print_row(image, y): | ||
print(Fore.WHITE + '{:d}\t'.format(y) + Fore.BLACK, end='') | ||
for x in reversed(range(0,image.shape[1])): | ||
_rgb = image[y,x] | ||
dist = color_distance(_rgb, PINK_RGB) | ||
_hilite = get_hilite(dist) | ||
print(_hilite + "▪", end='') | ||
print(Style.RESET_ALL) | ||
|
||
|
||
def get_hilite(dist): | ||
if dist < 0.025: | ||
return Fore.MAGENTA + Style.BRIGHT | ||
elif dist < 0.05: | ||
return Fore.MAGENTA + Style.NORMAL | ||
elif dist < 0.08: | ||
return Fore.RED + Style.BRIGHT | ||
elif dist < 0.10: | ||
return Fore.RED + Style.NORMAL | ||
elif dist < 0.15: | ||
return Fore.YELLOW + Style.BRIGHT | ||
elif dist < 0.2: | ||
return Fore.YELLOW + Style.NORMAL | ||
elif dist < 0.3: | ||
return Fore.GREEN + Style.BRIGHT | ||
elif dist < 0.4: | ||
return Fore.GREEN + Style.NORMAL | ||
elif dist < 0.5: | ||
return Fore.CYAN + Style.NORMAL | ||
elif dist < 0.6: | ||
return Fore.BLUE + Style.BRIGHT | ||
elif dist < 0.7: | ||
return Fore.BLUE + Style.NORMAL | ||
elif dist < 0.8: | ||
return Fore.BLACK + Style.NORMAL | ||
else: | ||
return Fore.BLACK + Style.DIM | ||
|
||
|
||
# .............................................................................. | ||
|
||
try: | ||
print(Fore.CYAN + 'starting...' + Style.RESET_ALL) | ||
|
||
# don't necessarily process the whole image (it takes a long time) | ||
_start_row = 180 # the bottom row of the image to be processed | ||
_end_row = 260 # the top row of the image to be processed | ||
|
||
with picamera.PiCamera() as camera: | ||
with PiRGBArray(camera) as output: | ||
camera.resolution = (640, 480) | ||
camera.capture(output, 'rgb') | ||
image = output.array | ||
print(Fore.YELLOW + 'Captured {:d}x{:d} image\n'.format(image.shape[1], image.shape[0]) + Style.RESET_ALL) | ||
_width = image.shape[1] | ||
_height = image.shape[0] | ||
print(Fore.YELLOW + 'Captured size {:d}x{:d} image'.format(_width, _height) + Style.RESET_ALL) | ||
for _row in reversed(range(_start_row + 1,_end_row + 1)): | ||
print_row(image, _row) | ||
|
||
except picamera.PiCameraMMALError: | ||
print(Fore.RED + Style.BRIGHT + 'could not get camera: in use by another process.' + Style.RESET_ALL) | ||
except Exception: | ||
print(Fore.RED + Style.BRIGHT + 'error starting ros: {}'.format(traceback.format_exc()) + Style.RESET_ALL) | ||
|
||
print(Fore.CYAN + 'complete.' + Style.RESET_ALL) | ||
|