added remove receiver code

master
Jake 2025-07-26 00:15:40 +08:00
parent d840ec58d8
commit 68dde0951a
1 changed files with 100 additions and 0 deletions

View File

@ -30,6 +30,7 @@
<button class="tab-btn text-gray-600 hover:text-blue-600" data-target="lesson8">Sonar</button> <button class="tab-btn text-gray-600 hover:text-blue-600" data-target="lesson8">Sonar</button>
<button class="tab-btn text-gray-600 hover:text-blue-600 hidden" data-target="lesson9">Motor <button class="tab-btn text-gray-600 hover:text-blue-600 hidden" data-target="lesson9">Motor
Encoders</button> Encoders</button>
<button class="tab-btn text-gray-600 hover:text-blue-600" data-target="lesson10">Reciever</button>
<!-- Add more tabs here --> <!-- Add more tabs here -->
</div> </div>
</div> </div>
@ -985,6 +986,105 @@ while True:
</section> </section>
<!-- Lesson 6 (hidden initially) -->
<section id="lesson10" class="lesson-content hidden">
<h1 class="text-3xl font-bold mb-6">Receiver</h1>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<!-- Step 2 -->
<div class="prose">
<h2>Step 1: Coding</h2>
<p>Create a new file called <code>remote.py</code> and add the code to the right.</p>
</div>
<div>
<pre class="bg-gray-100 p-4 rounded shadow text-sm"><code class="language-python">
# remote.py
import board
import busio
START_BYTE = 0xAA
END_BYTE = 0xBB
PACKET_LENGTH = 8
# Calibration values
LEFT_MIN = 0
LEFT_MID = 131
LEFT_MAX = 203
RIGHT_MIN = 51
RIGHT_MID = 120
RIGHT_MAX = 255
class ThumbInput:
def __init__(self, tx=board.GP0, rx=board.GP1, baudrate=115200):
self.uart = busio.UART(tx=tx, rx=rx, baudrate=baudrate, timeout=0.1)
self.buffer = bytearray()
def read(self):
"""Returns (left_percent, right_percent) in range [-100, 100], or None if packet incomplete."""
data = self.uart.read(1)
if data:
byte = data[0]
self.buffer.append(byte)
if len(self.buffer) > PACKET_LENGTH:
self.buffer = self.buffer[-PACKET_LENGTH:]
if len(self.buffer) == PACKET_LENGTH and self.buffer[0] == START_BYTE and self.buffer[-1] == END_BYTE:
payload = self.buffer[1:7]
self.buffer = bytearray()
left_raw = payload[1]
right_raw = payload[3]
left = self._map_thumbstick(left_raw, LEFT_MIN, LEFT_MID, LEFT_MAX)
right = self._map_thumbstick(right_raw, RIGHT_MIN, RIGHT_MID, RIGHT_MAX)
return (int(left), int(right))
return None
def _map_thumbstick(self, x, min_val, mid_val, max_val):
if x < mid_val:
return (x - mid_val) / (mid_val - min_val) * 100
else:
return (x - mid_val) / (max_val - mid_val) * 100
</code></pre>
</div>
<!-- Step 3 -->
<div class="prose">
<h2>Step 2: Using the recieved signal</h2>
<p>Import just the <code>ThumbInput</code> part of the library.</p>
</br>
<p>Initialize the receiver with <code>receiver = ThumbInput()</code></p>
</br>
<p>Create a new loop as this won't work nicely with any i2c, this loop should be placed after the motors are created, and before the i2c is initialized.</p>
</div>
<div>
<pre class="bg-gray-100 p-4 rounded shadow text-sm"><code class="language-python">
from remote import ThumbInput
receiver = ThumbInput()
while True:
result = receiver.read()
if result:
left_motor.move(result[0])
right_motor.move(result[1])
#print("Left:", result[0], "Right:", result[1])
time.sleep(0.001)
</code></pre>
</div>
</section>
</div> </div>
<script> <script>
// Tab button logic // Tab button logic