I am working on robot hand manipulator. So, for that I am using Arduino as controller. Here, I am confused about Programming language. I have two choices.
- Writing code in Arduino program (C++) ( Arduino IDE) and controlling robot hand manipulator by using Arduino with DC motor and encoder.
- Writing code in python and doing serial communication with Arduino through Arduino program (C++), then controlling robot hand manipulator by using Arduino with DC motor and encoder.
In python, it is very hard to work on DC motor encoder and controller (Arduino). In Arduino program (Arduino IDE), it is easy to work on encoder and controller. So, I am preferring serial communication between python and Arduino program (C++).
In second option, two-way serial communication between python and Arduino program (C++), I am facing problems like sometimes I did not get data from Arduino IDE into python and sometimes data from python fails to send in Arduino IDE. So, I am looking to write code in Arduino IDE, but for coding python is better than Arduino IDE.
Can anyone suggest about it?
# Serial Communication
# Python Code
import time
import serial
from time import sleep
import numpy as np
arduino = serial.Serial(port='COM3', baudrate=9600, timeout=1)
time.sleep(0.05)
def Write_read(x):
arduino.write(bytes(x, 'utf-8'))
time.sleep(0.05)
data = arduino.readline()
time.sleep(0.5)
decoded_values = str(data[0:len(data)].decode("utf-8"))
data= decoded_values.rstrip()
list_values = data.split('x')
for item in list_values:
list_in_floats.append(item)
data = 0
print(list_in_floats) # Recived from Arduino
return list_in_floats
# Send data to arduino IDE
VM1 = 120
VM2 = 100
VM3 = 100
num = ('str(VM1)+" "+str(VM2)+" "+str(VM3)')
list_values = []
list_in_floats = []
P=Write_read()
print(P)
`//Arduino IDE(C++)
String str;
int pwm[3];
int StringCount = 0;
const int RPWM[3] = { 4, 6, 8 };
const int LPWM[3] = { 5, 7, 9 };
const int RIS[3] = { 22, 26, 30 };
const int LIS[3] = { 23, 27, 31 };
const int REN[3] = { 24, 28, 32 };
const int LEN[3] = { 25, 29, 33 };
const int ENCA[3] = { 2, 18, 20 };
const int ENCB[3] = { 3, 19, 21 };
int pos1 = 0;
int pos2 = 0;
int pos3 = 0;
void setup() {
Serial.begin(9600);
for (int i = 0; i < 3; i++) {
pinMode(RIS[i], OUTPUT);
pinMode(REN[i], OUTPUT);
pinMode(RPWM[i], OUTPUT);
pinMode(LIS[i], OUTPUT);
pinMode(LEN[i], OUTPUT);
pinMode(LPWM[i], OUTPUT);
digitalWrite(RIS[i], HIGH);
digitalWrite(LIS[i], HIGH);
digitalWrite(REN[i], HIGH);
digitalWrite(LEN[i], HIGH);
pinMode(ENCA[i], INPUT);
pinMode(ENCB[i], INPUT);
}
attachInterrupt(digitalPinToInterrupt(ENCA[0]), readEncoder1, RISING);
attachInterrupt(digitalPinToInterrupt(ENCA[1]), readEncoder2, RISING);
attachInterrupt(digitalPinToInterrupt(ENCA[2]), readEncoder3, RISING);
}
void loop() {
while (!Serial.available()) {
str = Serial.readString();
split(str);
setMotor(pwm[0], RPWM[0], LPWM[0]);
setMotor(pwm[1], RPWM[1], LPWM[1]);
setMotor(pwm[2], RPWM[2], LPWM[2]);
Serial.print(pos1);
Serial.print("x");
Serial.print(pos2);
Serial.print("x");
Serial.println(pos3);
delay(1000);
}
}
void setMotor(int pwmVal, int r_PWM, int l_PWM) {
if (pwmVal < 0) {
analogWrite(r_PWM, abs(pwmVal));
analogWrite(l_PWM, 0);
} else if (pwmVal > 0) {
analogWrite(r_PWM, 0);
analogWrite(l_PWM, abs(pwmVal));
} else {
analogWrite(r_PWM, 0);
analogWrite(l_PWM, 0);
}
}
void split(String str) {
while (str.length() > 0) {
int index = str.indexOf(' ');
if (index == -1) // No space found
{
pwm[StringCount++] = str.toInt();
break;
} else {
pwm[StringCount++] = str.substring(0, index).toInt();
str = str.substring(index + 1);
}
}
}
void readEncoder1() {
int b1 = digitalRead(ENCB[0]);
if (b1 > 0) {
pos1++;
} else {
pos1--;
}
}
void readEncoder2() {
int b2 = digitalRead(ENCB[1]);
if (b2 > 0) {
pos2++;
} else {
pos2--;
}
}
void readEncoder3() {
int b3 = digitalRead(ENCB[2]);
if (b3 > 0) {
pos3++;
} else {
pos3--;
}
}
`
I am not receiving DC motor encoder data from Arduino in python.