carré v2

This commit is contained in:
theyatokami 2023-04-25 16:34:32 +02:00
parent 37c763e931
commit b9f75c0450
2 changed files with 44 additions and 55 deletions

View File

@ -1,55 +0,0 @@
#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include "MeMCore.h"
// declare motors
MeDCMotor _leftMotor(9);
MeDCMotor _rightMotor(10);
// declare gyro
MeGyro _gyro;
const int TRANSLATION_SPEED = 100; // This is the translation speed in mm/s
void setup() {
Serial.begin(115200);
// set no speed for left and right motors
_leftMotor.run((9)==M1?-(0):(0));
_rightMotor.run((10)==M1?-(0):(0));
// initialize the gyro
_gyro.begin();
}
void loop() {
// put your main code here, to run repeatedly:
// Read line follower sensor data
int sensorValue = mLineFollower.readSensor();
// Proportional control to adjust the rotation speed
int error = 500 - sensorValue; // 500 is the sensor reading when the robot is on the black line
int rotationSpeed = error * 0.5; // Proportional constant
// Calculate the motor speeds based on the translation and rotation speeds
int leftMotorSpeed = TRANSLATION_SPEED - rotationSpeed;
int rightMotorSpeed = TRANSLATION_SPEED + rotationSpeed;
// Move the robot
move(leftMotorSpeed, rightMotorSpeed);
}
void move(int leftMotorSpeed, int rightMotorSpeed)
{
_leftMotor.run((9)==M1?-(leftMotorSpeed):(leftMotorSpeed));
_rightMotor.run((10)==M1?-(rightMotorSpeed):(rightMotorSpeed));
}
// Function to stop the robot
void stop()
{
_leftMotor.stop();
_rightMotor.stop();
}

View File

@ -0,0 +1,44 @@
// Create a MeLineFollower object
MeLineFollower lineFollower(PORT_1);
// Set the motor speed
int speed = 150;
void setup() {
// Initialize the Makeblock library
MePort port = PORT_3;
MeMegaPiDCMotor leftMotor(port);
port = PORT_4;
MeMegaPiDCMotor rightMotor(port);
// Set the motor direction
leftMotor.run(-speed);
rightMotor.run(-speed);
// Wait for the robot to start moving
delay(1000);
}
void loop() {
// Read the line follower sensors
int left = lineFollower.readSensors(0);
int middle = lineFollower.readSensors(1);
int right = lineFollower.readSensors(2);
// Calculate the error
int error = (left * -1) + (middle * 0) + (right * 1);
// Calculate the motor speed
int leftSpeed = speed + error;
int rightSpeed = speed - error;
// Set the motor speed
MePort port = PORT_3;
MeMegaPiDCMotor leftMotor(port);
port = PORT_4;
MeMegaPiDCMotor rightMotor(port);
leftMotor.run(leftSpeed);
rightMotor.run(rightSpeed);
}