I would like to control a servo motor with Arduino UNO board and Python code.
Through the Python terminal I send a letter: 'p' to go forward by 5 ° and 'm' to go back by 5 °.
If I send the letter 'c' I want to have back the gardi that the engine has at this moment.
For Arduino I use SerialTransfer.h, and for Python pySerialTransfer.
The problem is when i write letter 'p' on Python terminal anything happen with the motor.
Arduino Code:
#include <Servo.h>
#include "SerialTransfer.h"
SerialTransfer trans;
Servo Z;
#define Z_Start 90
#define VALUE 5
char comand = 'd';
bool read = false;
int Z_New;
int Z_Old;
int ControlloFinecorsa(Servo servo_name, int angle) {
if (angle < 0) {
trans.txBuff[0] = 'm'; //Min angle value
trans.send(1);
return 3;
}
if (angle > 180) {
trans.txBuff[0] = 'M'; //Max angle value
trans.send(1);
return 3;
}
else {
return 0;
}
}
int ServoPosition(Servo servo_name) {
int position = servo_name.read();
return position;
}
int ServoRotation(Servo servo_name, int angle, int now) {
if (LimitSwitch(servo_name, angle) != 3) { //if the limit switches check is correct, proceed with moving the motor
//previous position greater than that to be reached
if (now > angle) {
//I use a loop to control the speed of the servo
for(int j = now; j >= angle; j--) { //servo down
servo_name.write(j);
delay(20);
}
}
//previous position lower than the one to reach
if (now < angle) {
for(int j = now; j <= angle; j++) { //servo rise
servo_name.write(j);
delay(20);
}
}
now = angle; //I set the current position as the previous position
return now;
}
else if (LimitSwitch(servo_name, angle) == 3) {
return now;
}
}
void setup() {
Serial.begin(115200);
trans.begin(Serial);
//Z Servo start value
Z.write(Z_Start);
Z.attach(6);
Z_Old = Z_Start;
}
void loop() {
do{
if (trans.available()) {
comand = trans.rxBuff[0];
read = true;
}
else if(trans.status < 0) {
read = false;
}
}
while(!read);
//know servo position
if (comand == 'c') {
int pos = ServoPosition(Z);
trans.txBuff[0] = pos;
trans.send(1);
}
//Move Servo forward for 5 degree
if (comand == 'p') {
Z_New = Z_Old + VALUE;
Z_Old = ServoRotation(Z, Z_New, Z_Old);
}
//Move Servo backward for 5 degree
if (comand == 'm') {
Z_New = Z_Old - VALUE;
Z_Old = ServoRotation(Z, Z_New, Z_Old);
}
}
Python Code:
import serial
import time
from pySerialTransfer import pySerialTransfer as txfer
if __name__ == '__main__':
try:
link = txfer.SerialTransfer('COM6')
Z_Angle = 90
while True:
choose = input("\nChoose one of the following possibilities: \n"
"[1] Move Z servo forward\n"
"[2] MOve Z servo backward\n"
"[3] Know Z servo angle\n"
"[0] Uscire\n"
"---> ")
if choose == "1":
link.txBuff[0] = 'p'
link.send(1)
Z_Angle = Z_Angle + 5
elif choose == "2":
link.txBuff[0] = 'm'
link.send(1)
Z_Angle = Z_Angle - 5
elif choose == "3":
link.txBuff[0] = 'c'
link.send(1)
print("Z Servo Angle: ")
print(link.rxBuff[0])
elif choose == "0":
print("Bye")
break
while not link.available():
if link.status < 0:
print('ERROR: {}'.format(link.status))
except KeyboardInterrupt:
link.close()
Thanks for your cooperation, and sorry for my bad english