Can anyone help me Convert this code to Python? I am new to it and really do not know how to do this. I need help asap since it is due for a project. Thanks!
// definitions and declarations of variables of the robot code maze resolver;
#define vel_motor_e 10 // controls left motor speed;
#define vel_motor_dir 11 // controls the right motor speed;
#define e1 8 // controls the direction of rotation of the left motor;
#define e2 9 // controls the direction of rotation of the left motor;
#define d1 12 // controls the direction of rotation of the right motor;
#define d2 7 // controls the direction of rotation of the right motor;
Int trigger_face = A4; // controls the pulse sent from the front sensor
Int echo_front = A5; // controls the pulse received from the front sensor
Int trigger_e = A2; // controls the sent forward sensor pulse
Int echo_e = A3; // controls the received pulse from the front sensor
Int trigger_dir = A0; // controls the forward sensor sent pulse
Int echo_dir = A1; // controls the received pulse from the front sensor
// configuration of input types of declared variables;
Void setup ()
{
PinMode (trigger_face, OUTPUT); // arduino signal output from trigger_front
PinMode (echo_front, INPUT) // arduino signal input from echo_front
PinMode (trigger_e, OUTPUT) // arduino signal output from trigger_front
PinMode (echo_e, INPUT) // arduino signal input from echo_front
PinMode (trigger_dir, OUTPUT) // arduino signal output from trigger_front
PinMode (echo_dir, INPUT) // arduino signal input from echo_front
PinMode (vel_motor_e, OUTPUT) // arduino signal output from left motor speed
PinMode (vel_motor_dir, OUTPUT) // arduino signal output from the right motor speed
PinMode (e1, OUTPUT) // arduino signal output from the left motor rotation direction control
PinMode (e2, OUTPUT) // arduino signal output from the left motor rotation direction control
PinMode (d1, OUTPUT) // arduino signal output from the right motor rotation direction control
PinMode (d2, OUTPUT) // arduino signal output from the right motor rotation direction control
Delay (5000);
}
// infinite project repeat code;
Void loop ()
{
// declaration of variables used to control the project;
Long dur_front, dur_e, dur_dir, right, left, front;
DigitalWrite (trigger_face, LOW); // their respective signal inputs and outputs are declared
DelayMicroseconds (2); // Ultrasonic sensor and stored by sensor variable
DigitalWrite (trigger_face, HIGH); // which converts the velocity of the sound which is 340 m / s or
DelayMicroseconds (5); // 29 microseconds per centimeter, as the signal goes back and forth
DigitalWrite (trigger_face, LOW); // this time is half being sensor = time / 29/2;
Duracao_front = pulseIn (echo_front, HIGH); // so also on the other two sensors.
Front = duration / 29/2;
DigitalWrite (trigger_e, LOW);
DelayMicroseconds (2);
DigitalWrite (trigger_e, HIGH);
DelayMicroseconds (5);
DigitalWrite (trigger_e, LOW);
Dur_e = pulseIn (echo_e, HIGH);
Left = duration / 29/2;
DigitalWrite (trigger_dir, LOW);
DelayMicroseconds (2);
DigitalWrite (trigger_dir, HIGH);
DelayMicroseconds (5);
DigitalWrite (trigger_dir, LOW);
Dur_dir = pulseIn (echo_dir, HIGH);
Right = dur_dir / 29/2;
AnalogWrite (vel_motor_e, 0); // block to initialize inputs with pulse 0 or off;
AnalogWrite (vel_motor_dir, 0); //
AnalogWrite (e1, 0); //
AnalogWrite (e2, 0); //
AnalogWrite (d1, 0); //
AnalogWrite (d2, 0); //
If (front> 8) // if there is a free path ahead it follows this logic below:
{
// the four if's use below within this if are for control of the robot's handling,
// in order to keep it following the right wall in a straight line;
If (right> 7 && right <13) // if the distance from the wall to the right is between 9 and 12 cm, the robot
// keep in a straight line;
{
AnalogWrite (vel_motor_e, 120);
AnalogWrite (vel_motor_dir, 150);
AnalogWrite (e1, 255);
AnalogWrite (e2, 0);
AnalogWrite (d1, 0);
AnalogWrite (d2, 255);
}
If (right> = 13) // if the distance from the wall to the right is greater than or equal to 13 cm, the robot increases
// your left motor speed to approach the right wall;
{
AnalogWrite (vel_motor_e, 255);
AnalogWrite (vel_motor_dir, 60);
AnalogWrite (e1, 255);
AnalogWrite (e2, 0);
AnalogWrite (d1, 0);
AnalogWrite (d2, 255);
}
If (right <= 7) // if the distance from the wall to the right is less than or equal to 8 cm, the robot increases
// its right motor speed to distance itself from the right wall;
{
AnalogWrite (vel_motor_e, 60);
AnalogWrite (vel_motor_dir, 255);
AnalogWrite (e1, 255);
AnalogWrite (e2, 0);
AnalogWrite (d1, 0);
AnalogWrite (d2, 255);
}
}
If (left <= 20 && right> 20 && forward <= 8) dir (); // if the front distance is less than or equal to 8 cm,
// the distance to the right is greater than 20 cm and the distance
// the left is less than or equal to 20 cm it calls the dir () function;
If (left> 20 && right> 20 && forward <= 8) dir (); // if the front distance is less than or equal to 8 cm,
// the distance to the right is greater than 20 cm and the distance
// the left is greater than 20 cm it calls the dir () function;
If (right <= 20 && left> 20 && front <= 8) e (); // if the front distance is less than or equal to 8 cm,
// the distance to the right is less than or equal to 20 cm and the distance
// the left is greater than 20 cm it calls the function e ();
If (right <= 20 && left <= 20 && forward <= 8) back (); // if the front distance is less than or equal to 8 cm,
// the distance to the right is less than or equal to 20 cm and the distance
// the left is less than or equal to 20 cm it calls the function back ();
}
Void e () // function to make the robot rotate 90º to the left if there is no front and right exit;
{
AnalogWrite (vel_motor_e, 120);
AnalogWrite (vel_motor_dir, 120);
AnalogWrite (e1, 0);
AnalogWrite (e2, 255);
AnalogWrite (d1, 0);
AnalogWrite (d2, 255);
Delay (700);
}
Void dir () // function to cause the robot to rotate 90 ° to the right if it does not have front or left output;
{
AnalogWrite (vel_motor_e, 120);
AnalogWrite (vel_motor_dir, 120);
AnalogWrite (e1, 255);
AnalogWrite (e2, 0);
AnalogWrite (d1, 255);
AnalogWrite (d2, 0);
Delay (800);
}
Void return () // function to cause the robot to rotate 180º if there is no forward, right and left output;
{
AnalogWrite (vel_motor_e, 120);
AnalogWrite (vel_motor_dir, 120);
AnalogWrite (e1, 255);
AnalogWrite (e2, 0);
AnalogWrite (d1, 255);
AnalogWrite (d2, 0);
Delay (1200);
}