קורס RB07 רובטיקס בלוקס 015 : מבוא לפייתון פיתוח רובוטיקה
התקנת סביבית פיתוח מיקרו פיתון עבור רובוטיקס בלוסקס גירסה 1.2
את התוכנה נוריד מהאתר https://thonny.org/
1.התקנת התוכנה מיקרו פייתון רובוטיקס בלוקס
2. התקנת ספריית ESP32 עבור המיקרו בקר רובוטיקס בלוקס
נלחץ
נבחר את COM הסיראלי מהרשימה , את הקובץ שהמדריך יגיד להוריד
ונלחץ על INSTALL
לאחר הקלדת קוד תוכנה – לבדיקה
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from machine import Pin import time led = Pin(2, Pin.OUT) while True: led.value(1) print("LED ON") time.sleep_ms(1000) led.value(0) print("LED OFF") time.sleep_ms(1000) |
שימוש בסרבו מיקרו פייתון
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 |
from machine import Pin, PWM import time # Define the GPIO pin that is connected to the servo motor servo_pin = Pin(12) # Create a PWM object with a frequency of 50 Hz pwm1 = PWM(servo_pin, freq=50) # Define the duty cycle values for the minimum and maximum positions of the servo motor min_duty_cycle = 26 max_duty_cycle = 123 # Define a function to set the position of the servo motor def servo1(position): duty_cycle = int(min_duty_cycle + (max_duty_cycle - min_duty_cycle) * position) pwm1.duty(duty_cycle) time.sleep_ms(15) while True: # Move the servo motor to the maximum position (position = 1.0) servo1(1.0) time.sleep_ms(1000) # Move the servo motor to the minimum position (position = 0.0) servo1(0.0) time.sleep_ms(1000) # Stop the PWM signal pwm.deinit() |
שימוש בפוטנציומר מיקרו פייתון
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
from machine import Pin, ADC import time # Define the ADC pin that is connected to the potentiometer pot_pin = Pin(34) adc = ADC(pot_pin) # Define the range of the potentiometer (e.g. 0 to 1023 for a 10-bit ADC) pot_min = 0 pot_max = 1023 # Define a function to read the voltage level of the potentiometer and return the corresponding position value def read_pot_position(): pot_value = adc.read() position = (pot_value - pot_min) / (pot_max - pot_min) return position # Read the position of the potentiometer and print the value to the console while True: pot_position = read_pot_position() print("Potentiometer position: {:.2f}".format(pot_position)) time.sleep(0.1) |
שימוש בפוטנציומר סרבו מיקרו פייתון
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 |
from machine import Pin, ADC from machine import PWM import time # Define the GPIO pin that is connected to the servo motor servo_pin = Pin(12) # Create a PWM object with a frequency of 50 Hz pwm1 = PWM(servo_pin, freq=50) # Define the ADC pin that is connected to the potentiometer # robotics blocks - http://robotronix.co.il pot_pin = Pin(34) # ---- setup ---- adc = ADC(pot_pin, atten=ADC.ATTN_11DB) # Set the ADC attenuation level to 11 dB for 8-bit resolution adc = ADC(pot_pin) pot_value=0.0 # Define the range of the potentiometer (e.g. 0 to 1023 for a 10-bit ADC) pot_min = 0 pot_max = 4095 # Define a function to read the voltage level of the potentiometer and return the corresponding position value def read_pot1(): pot_value = adc.read() position = (pot_value - pot_min) / (pot_max - pot_min) return position , pot_value # Define the duty cycle values for the minimum and maximum positions of the servo motor min_duty_cycle = 26 max_duty_cycle = 123 # Define a function to set the position of the servo motor def servo1(position): duty_cycle = int(min_duty_cycle + (max_duty_cycle - min_duty_cycle) * position) pwm1.duty(duty_cycle) time.sleep_ms(15) # Read the position of the potentiometer and print the value to the console while True: pot_position , pot_value = read_pot1() print("Potentiometer position: {:.2f} {:.2f}".format(pot_position,pot_value) ) time.sleep(0.1) # Move the servo motor to the maximum position (position = 1.0) servo1(pot_position) time.sleep_ms(100) pwm1.deinit() |
התקנה בלינוקס