Skip to content

Commit c6a07c5

Browse files
committed
Examples: adding new example 'attitude_indicator.py'
1 parent 12865eb commit c6a07c5

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

examples/attitude_indicator.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# ICON [[(-4.5, 16.82), (-9.92, 6.75), (-19.99, 1.33), (-16.1, -2.5), (-8.17, -1.13), (-2.58, -6.71), (-19.93, -14.1), (-15.33, -18.8), (5.73, -15.08), (12.52, -21.87), (13.6, -22.66), (15.25, -23.11), (16.46, -23.05), (17.73, -22.63), (19.14, -21.42), (19.62, -20.63), (19.97, -19.45), (19.99, -18.25), (19.79, -17.33), (19.32, -16.37), (18.79, -15.72), (11.92, -8.84), (15.64, 12.17), (10.99, 16.82), (3.54, -0.53), (-2.04, 5.05), (-0.61, 12.93), (-4.5, 16.82)]]
2+
# NAME Attitude Indicator
3+
# DESC A Demo for the Multi-Sensor Stick
4+
from presto import Presto
5+
from picovector import ANTIALIAS_FAST, PicoVector, Polygon, Transform
6+
import machine
7+
from lsm6ds3 import LSM6DS3, NORMAL_MODE_104HZ
8+
9+
# Setup for the Presto display
10+
presto = Presto(ambient_light=True)
11+
display = presto.display
12+
WIDTH, HEIGHT = display.get_bounds()
13+
CX = WIDTH // 2
14+
CY = HEIGHT // 2
15+
16+
# Colours
17+
GRAY = display.create_pen(42, 52, 57)
18+
BLACK = display.create_pen(0, 0, 0)
19+
SKY_COLOUR = display.create_pen(86, 159, 201)
20+
GROUND_COLOUR = display.create_pen(101, 81, 63)
21+
WHITE = display.create_pen(255, 255, 255)
22+
RED = display.create_pen(200, 0, 0)
23+
24+
# Pico Vector
25+
vector = PicoVector(display)
26+
vector.set_antialiasing(ANTIALIAS_FAST)
27+
t = Transform()
28+
normal = Transform()
29+
vector.set_transform(t)
30+
31+
x, y = 0, CY
32+
x_prev = x
33+
y_prev = y
34+
alpha = 0.15
35+
36+
# Setup some of our vector shapes
37+
background_rect = Polygon()
38+
background_rect.rectangle(0, 0, WIDTH, HEIGHT)
39+
background_rect.circle(CX, CY, 109)
40+
41+
instrument_outline = Polygon().circle(CX, CY, 110, stroke=8)
42+
43+
ground = Polygon().rectangle(0, HEIGHT // 2, WIDTH, HEIGHT)
44+
horizon = Polygon().rectangle(0, HEIGHT // 2, WIDTH, 2)
45+
pitch_lines = Polygon()
46+
47+
for line in range(1, 7):
48+
if line % 2:
49+
pitch_lines.rectangle(CX - 10, CY - line * 14, 20, 1.5)
50+
pitch_lines.rectangle(CX - 10, CY + line * 14, 20, 1.5)
51+
else:
52+
pitch_lines.rectangle(CX - 30, CY - line * 14, 60, 1.5)
53+
pitch_lines.rectangle(CX - 30, CY + line * 14, 60, 1.5)
54+
55+
craft_centre = Polygon().circle(CX, CY - 1, 2)
56+
craft_left = Polygon().rectangle(CX - 70, CY - 1, 50, 2, (2, 2, 2, 2))
57+
craft_right = Polygon().rectangle(CX + 20, CY - 1, 50, 2, (2, 2, 2, 2))
58+
craft_arc = Polygon().arc(CX, CY, 22, -90, 90, stroke=2)
59+
60+
61+
def show_message(text):
62+
display.set_pen(GRAY)
63+
display.clear()
64+
display.set_pen(WHITE)
65+
display.text(f"{text}", 5, 10, WIDTH, 2)
66+
presto.update()
67+
68+
69+
try:
70+
i2c = machine.I2C()
71+
sensor = LSM6DS3(i2c, mode=NORMAL_MODE_104HZ)
72+
except OSError:
73+
while True:
74+
show_message("No Multi-Sensor stick detected!\n\nConnect and try again.")
75+
76+
while True:
77+
# Clear screen with the SKY colour
78+
display.set_pen(SKY_COLOUR)
79+
display.clear()
80+
81+
try:
82+
# Get the raw readings from the sensor
83+
ax, ay, az, gx, gy, gz = sensor.get_readings()
84+
except OSError:
85+
while True:
86+
show_message("Multi-Sensor stick disconnected!\n\nReconnect and reset your Presto.")
87+
88+
# Apply some smoothing to the X and Y
89+
# and cap the Y with min/max
90+
y_axis = max(-11000, min(int(alpha * ay + (1 - alpha) * y_prev), 11000))
91+
y_prev = y_axis
92+
93+
x_axis = int(alpha * ax + (1 - alpha) * x_prev)
94+
x_prev = x_axis
95+
96+
# Draw the ground
97+
t.reset()
98+
t.rotate(-x_axis / 180, (WIDTH // 2, HEIGHT // 2))
99+
t.translate(0, y_axis / 100)
100+
101+
vector.set_transform(t)
102+
display.set_pen(GROUND_COLOUR)
103+
vector.draw(ground)
104+
display.set_pen(WHITE)
105+
vector.draw(horizon)
106+
vector.draw(pitch_lines)
107+
vector.set_transform(normal)
108+
109+
# Draw the aircraft
110+
display.set_pen(RED)
111+
vector.draw(craft_centre)
112+
vector.draw(craft_left)
113+
vector.draw(craft_right)
114+
vector.draw(craft_arc)
115+
116+
display.set_pen(GRAY)
117+
vector.draw(background_rect)
118+
display.set_pen(BLACK)
119+
vector.draw(instrument_outline)
120+
121+
# Update the screen so we can see our changes
122+
presto.update()

0 commit comments

Comments
 (0)