اطلاعیه

Collapse
No announcement yet.

کنترل position موتور dc با انکودر و ادینیو

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    کنترل position موتور dc با انکودر و ادینیو

    سلام و خسته نباشید
    من یه پروژه میخوام انجام بدم و اینو بگم که هیچ تجربه و اطلاعی از اردینیو ندارم
    از chatGPT کمک گرفتم و یه کد نویسی رو انجام داد برام که اینجا میزارم
    تو این پروژه یه موتور dc معمولی داریم که روش یه انکودر روتاری میخوام نصب کنم
    یه انکودر هم روی برد هست که اونو کاربر با دست میچرخونه و میخوام در لحظه پوزیشن رو از انکودر روی برد بخونه و موتور dc رو روی همون پوزیشن قرار بده به صورت لحظه این کار انجام بشه
    سوالی که دارم اینه که کدوم برد اردینیو مناسب این کاره واصلا نظر شما اساتید در این مورد چیه ؟

    // Libraries
    #include <Encoder.h>


    // Pin Definitions
    const int motorEncoderPinA = 4; // Encoder pin A for motor
    const int motorEncoderPinB = 5; // Encoder pin B for motor
    const int dashboardEncoderPinA = 6; // Encoder pin A for dashboard encoder
    const int dashboardEncoderPinB = 7; // Encoder pin B for dashboard encoder
    const int motorEnablePin = 9; // Enable pin for motor speed control
    const int motorInput1Pin = 10; // Input 1 pin for motor direction control
    const int motorInput2Pin = 11; // Input 2 pin for motor direction control


    // Constants
    const int motorMinPos = 0; // Minimum position value for motor
    const int motorMaxPos = 1023; // Maximum position value for motor
    const int dashboardMinPos = 0; // Minimum position value for dashboard encoder
    const int dashboardMaxPos = 1023; // Maximum position value for dashboard encoder


    // Variables
    Encoder motorEncoder(motorEncoderPinA, motorEncoderPinB); // Motor encoder object
    Encoder dashboardEncoder(dashboardEncoderPinA, dashboardEncoderPinB); // Dashboard encoder object
    int motorPosition = 0; // Current position of motor
    int dashboardPosition = 0; // Current position of dashboard encoder


    void setup() {
    // Motor setup
    pinMode(motorEnablePin, OUTPUT);
    pinMode(motorInput1Pin, OUTPUT);
    pinMode(motorInput2Pin, OUTPUT);

    // Encoder setup
    motorEncoder.write(0);
    dashboardEncoder.write(0);


    // Serial monitor for debugging
    Serial.begin(9600);
    }


    void loop() {
    // Read encoder values
    motorPosition = map(motorEncoder.read(), motorMinPos, motorMaxPos, dashboardMinPos, dashboardMaxPos);
    dashboardPosition = dashboardEncoder.read();


    // Calculate motor control
    int motorSpeed = calculateMotorSpeed(motorPosition, dashboardPosition);
    int motorDirection = calculateMotorDirection(motorPosition, dashboardPosition);


    // Set motor direction
    if (motorDirection == 1) {
    digitalWrite(motorInput1Pin, HIGH);
    digitalWrite(motorInput2Pin, LOW);
    } else if (motorDirection == -1) {
    digitalWrite(motorInput1Pin, LOW);
    digitalWrite(motorInput2Pin, HIGH);
    } else {
    digitalWrite(motorInput1Pin, LOW);
    digitalWrite(motorInput2Pin, LOW);
    }


    // Set motor speed
    analogWrite(motorEnablePin, motorSpeed);


    // Print position values for debugging
    Serial.print("Motor Position: ");
    Serial.print(motorPosition);
    Serial.print("\tDashboard Position: ");
    Serial.println(dashboardPosition);


    delay(10); // Adjust the delay as per your requirements
    }


    // Function to calculate motor speed based on position difference
    int calculateMotorSpeed(int motorPos, int dashboardPos) {
    int speed = abs(motorPos - dashboardPos);
    return map(speed, 0, dashboardMaxPos, 0, 255); // Adjust the speed mapping as per your requirements
    }


    // Function to calculate motor direction based on position difference
    int calculateMotorDirection(int motorPos, int dashboardPos) {
    if (motorPos > dashboardPos) {
    return -1; // Move motor in the negative direction
    } else if (motorPos < dashboardPos) {
    return 1; // Move motor in the positive direction
    } else {
    return 0; // No movement required
    }
    }

    #2
    پاسخ : کنترل position موتور dc با انکودر و ادینیو

    درود
    توضیحات شما کاملا مفهوم نیست . ابتدا مدار را با پروتئوس طراحی کنید و تصویرش را اینجا بگذارید تا مشخص شود چکار میخواهید انجام دهید. بعد میرسیم به کد آردوینو.

    ضمنا اگر کدی را که گذاشته اید درست باشد باید در شبیه ساز پروتئوس کار کند آنرا چک کنید.

    دیدگاه

    لطفا صبر کنید...
    X