קורס רובוטיקה ובינה מלאכותית : A106-07 רובוטיקה
חלק ב : התקנת סביבת Anaconda
התקנת סביבת Anaconda לקורס A106
ROBOTRONIX.CO.IL – רובוטיקה ובינה מלאכותית
שלב 1 – יצירת Environment
-
פתח Anaconda Navigator.
-
לחץ Environments.
-
לחץ Create.
-
בשדה Name רשום A106R
-
בחר Python בגרסה
3.12. -
לחץ Create.
שלב 2 – התקנת JupyterLab
-
עבור ללשונית Home.
-
תחת Applications on בחר
A016R. -
מצא את JupyterLab.
-
לחץ Install.
-
בסיום ההתקנה לחץ Launch.
שלב 3 – יצירת מחברת Python
בחלון JupyterLab לחץ:
File → New → Notebook → Python 3
שלב 4 – בדיקת הסביבה
הרץ בתא הראשון:
|
1 2 3 4 |
import sys print(sys.version) print(sys.executable) |
בסיום לחץ Kernel → Restart Kernel.
חשוב: בכל שיעור יש להפעיל את JupyterLab מתוך סביבת A106R בלבד. אין להתקין את ספריות הקורס בסביבת base.
פיתוח טרמינל UART jupiterLab
1.התקנת ספריה
|
1 2 3 4 5 6 |
# Install the graphical user interface library %pip install PyQt5==5.15.10 # Install the UART communication library %pip install pyserial==3.5 |
2. הרצת קוד
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# AI UART ROBOTIC TERMINAL - Course A106 # ROBOTRONIX.CO.IL # College for Robotics and Artificial Intelligence import codecs # Decode UTF-8 data received in separate chunks import serial # Open, read and write UART serial ports from serial.tools import list_ports # Detect serial ports on Windows and Linux from PyQt5.QtWidgets import ( # Build the graphical user interface QApplication, QDialog, QLabel, QComboBox, QPushButton, QPlainTextEdit, QLineEdit, QCheckBox, QHBoxLayout, QVBoxLayout ) from PyQt5.QtCore import QTimer # Read UART periodically without a while loop from PyQt5.QtGui import QTextCursor # Insert text at the end of the terminal def run_terminal(): # Create a serial connection without opening a port yet. ser = serial.Serial(timeout=0, write_timeout=1) decoder = codecs.getincrementaldecoder("utf-8")(errors="replace") # Reuse the Qt application if Jupyter already created one. app = QApplication.instance() or QApplication([]) # Create the terminal window. window = QDialog() window.setWindowTitle("AI UART ROBOTIC TERMINAL | A106 | ROBOTRONIX") window.resize(950, 550) title = QLabel("AI UART ROBOTIC TERMINAL") title.setStyleSheet("font-size: 20px; font-weight: bold;") status = QLabel("Disconnected") # Create port and baud-rate selection controls. ports = QComboBox() # Available serial ports baud = QComboBox() # Available UART speeds baud.addItems([ "9600", "19200", "38400", "57600", "115200", "230400", "460800", "921600" ]) baud.setCurrentText("115200") # Default UART speed refresh = QPushButton("REFRESH") # Scan for new devices connect = QPushButton("CONNECT") # Open or close UART # Create a scrollable, read-only terminal window. terminal = QPlainTextEdit() terminal.setReadOnly(True) # Prevent editing received data terminal.setMaximumBlockCount(5000) # Keep the last 5000 text blocks # Configure terminal colors and font. terminal.setStyleSheet( "background-color: black; color: lime;" "font-family: monospace; font-size: 15px;" ) # Create automatic scrolling control. autoscroll = QCheckBox("Auto scroll") autoscroll.setChecked(True) # Create the message input field. command = QLineEdit() command.setPlaceholderText("Enter message or command") # Create line ending selection. ending = QComboBox() ending.addItems(["None", "LF", "CR", "CR+LF"]) ending.setCurrentText("None") # Create the action buttons. send = QPushButton("SEND") # Transmit text through UART clear = QPushButton("CLEAR") # Clear the terminal stop = QPushButton("STOP") # Close the program send.setEnabled(False) # Send only when connected # Prevent ENTER from activating an unrelated button. for button in (refresh, connect, send, clear, stop): button.setAutoDefault(False) # ========================================== # DISPLAY TEXT # ========================================== def show_text(text): scrollbar = terminal.verticalScrollBar() # Get scrollbar old_position = scrollbar.value() # Save scroll position cursor = QTextCursor(terminal.document()) # Create text cursor cursor.movePosition(QTextCursor.End) # Move to end cursor.insertText(text) # Insert text if autoscroll.isChecked(): scrollbar.setValue(scrollbar.maximum()) else: scrollbar.setValue(old_position) # ========================================== # DISPLAY LOG MESSAGE # ========================================== def log(text): last_block = terminal.document().lastBlock().text() prefix = "\n" if last_block else "" show_text(prefix + text + "\n") # ========================================== # SCAN UART PORTS # ========================================== def scan_ports(): previous = ports.currentText() # Remember selected port ports.clear() # Remove old port list for port in sorted(list_ports.comports()): ports.addItem(port.device) # Add detected port index = ports.findText(previous) if index >= 0: ports.setCurrentIndex(index) # Enable CONNECT only when ports are available. connect.setEnabled(ports.count() > 0) if ports.count() == 0: log("No UART ports found. Connect a device and click REFRESH.") # ========================================== # DISCONNECT UART # ========================================== def disconnect_uart(): try: if ser.is_open: ser.close() # Release UART port except (serial.SerialException, OSError) as error: log(f"Close error: {error}") decoder.reset() # Reset UTF-8 decoder status.setText("Disconnected") connect.setText("CONNECT") ports.setEnabled(True) # Enable port selection baud.setEnabled(True) # Enable baud selection refresh.setEnabled(True) # Enable port scanning send.setEnabled(False) # Disable transmission # ========================================== # CONNECT UART # ========================================== def connect_uart(): if ser.is_open: disconnect_uart() log("Disconnected") return # Check if a UART port is selected. if not ports.currentText(): log("No UART port selected.") return try: ser.port = ports.currentText() # Select UART port ser.baudrate = int(baud.currentText())# Set UART speed ser.open() # Open UART connection decoder.reset() # Reset text decoder except (serial.SerialException, OSError, ValueError) as error: log(f"UART Error: {error}") disconnect_uart() return # Update graphical interface. connect.setText("DISCONNECT") ports.setEnabled(False) baud.setEnabled(False) refresh.setEnabled(False) send.setEnabled(True) status.setText( f"Connected to {ser.port} | {ser.baudrate} Baud" ) log(f"Connected to {ser.port} at {ser.baudrate} Baud") command.setFocus() # Focus message input # ========================================== # SEND UART MESSAGE # ========================================== def send_data(): # Check UART connection. if not ser.is_open: log("UART not connected.") return text = command.text() # Read input message if not text: return # Define optional message endings. endings = { "None": "", "LF": "\n", "CR": "\r", "CR+LF": "\r\n" } # Add selected line ending. message = text + endings[ending.currentText()] try: # Convert text to bytes and transmit. ser.write(message.encode("utf-8")) log("TX: " + text) # Display transmitted message command.clear() # Clear input field command.setFocus() # Return focus to input except (serial.SerialException, OSError) as error: log(f"TX Error: {error}") disconnect_uart() # ========================================== # RECEIVE UART MESSAGE # ========================================== def receive_data(): # Skip receiving when UART is disconnected. if not ser.is_open: return try: available = ser.in_waiting # Count received bytes if available: # Read available UART data. data = ser.read(min(available, 65536)) # Convert received bytes to text. message = decoder.decode(data) if message: show_text(message) except (serial.SerialException, OSError) as error: log(f"RX Error: {error}") disconnect_uart() # ========================================== # CONNECT BUTTONS TO FUNCTIONS # ========================================== refresh.clicked.connect(scan_ports) connect.clicked.connect(connect_uart) send.clicked.connect(send_data) clear.clicked.connect(terminal.clear) stop.clicked.connect(window.reject) # Send message when ENTER is pressed. command.returnPressed.connect(send_data) # ========================================== # CREATE TOP CONTROL PANEL # ========================================== top = QHBoxLayout() top.addWidget(QLabel("PORT:")) top.addWidget(ports, 1) top.addWidget(refresh) top.addWidget(QLabel("BAUD:")) top.addWidget(baud) top.addWidget(connect) # ========================================== # CREATE BOTTOM CONTROL PANEL # ========================================== bottom = QHBoxLayout() bottom.addWidget(command, 1) bottom.addWidget(ending) bottom.addWidget(send) bottom.addWidget(clear) bottom.addWidget(stop) # ========================================== # CREATE MAIN WINDOW LAYOUT # ========================================== layout = QVBoxLayout(window) layout.addWidget(title) layout.addLayout(top) layout.addWidget(status) layout.addWidget(terminal, 1) layout.addWidget(autoscroll) layout.addLayout(bottom) # ========================================== # CREATE UART RECEIVE TIMER # ========================================== timer = QTimer(window) # Create timer timer.timeout.connect(receive_data) # Connect receive function timer.start(50) # Read UART every 50 ms window.finished.connect(timer.stop) # Stop timer when closing # ========================================== # START TERMINAL # ========================================== log("AI UART ROBOTIC TERMINAL started.") scan_ports() # Detect UART ports try: window.exec_() # Run graphical interface finally: timer.stop() # Stop receive timer disconnect_uart() # Release UART port print("Python program stopped.") # ========================================== # MAIN PROGRAM # ========================================== if __name__ == "__main__": run_terminal() |
תרגיל כיתה 3 – בניית סרבו

יש להתקין ספרייה אחת בלבד: Servo by Arduino.
בסביבת Arduino IDE:
-
פתח
Tools → Manage Libraries. -
חפש
Servo. -
בחר
Servo by ArduinoולחץInstall.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
// ROBOTRONIX.CO.IL // Course A106 // UART Robotic Door Control // Arduino UNO #include <Servo.h> // Create servo object Servo doorServo; // Hardware configuration const int SERVO_PIN = 9; void setup() { // Initialize UART communication Serial.begin(115200); Serial.setTimeout(100); // Connect servo motor doorServo.attach(SERVO_PIN); // Set initial door position to CLOSED doorServo.write(0); Serial.println("SYSTEM READY"); } void loop() { // Check for incoming UART data if (Serial.available() > 0) { // Read command from UART String command = Serial.readStringUntil('\n'); // Remove spaces and newline characters command.trim(); // Open the robotic door if (command == "OPEN") { doorServo.write(180); Serial.println("DOOR OPEN"); } // Close the robotic door else if (command == "CLOSE") { doorServo.write(0); Serial.println("DOOR CLOSE"); } // Invalid command else { Serial.println("UNKNOWN COMMAND"); } } } |