Which is better, python or Arduino program(C++)?

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.

  1. Writing code in Arduino program (C++) ( Arduino IDE) and controlling robot hand manipulator by using Arduino with DC motor and encoder.
  2. 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.

Why did you inserted your text in the tags?
To make it difficult for respondents to read?

in general, it is better to use the language that you know better....

Why did you post your message in code tags? it makes it very hard to read. I had to copy your "code" , paste it into a text editor before I could read it.
Edit
I have now removed this copy of the question as @vishalnaik has corrected the original post.

Your choices seem spurious.
If serial communication is not reliable then you are doing something wrong, either in the hardware of the software. Having seen neither it is hard to say what is wrong.

You might want to look at this How to get the best from this from this Forum before you proceed any further.

The Arduino IDE is only used to write code, not to execute code.

If you write code in Python for execution on a PC then an Arduino program (C++) is required to communicate with that program at runtime. The Arduino program can accept commands and do whatever your Arduino code implements.

1 Like

While this is in my thread it is not what I said but the OP @vishalnaik .
It is just that he posted a totally unreadable question.

I would appreciate if you could edit that quote attribution to the OP.

1 Like

If you don't have a need for a program on the PC, you only have one choice :slight_smile:

What would be the function of the PC?

If you need the communication with the PC, you can consider @Power_Broker's serialtransfer library for the Arduino side of things; there is a matching python library as well for the PC side

1 Like

Remember that the Raspberry Pi serial port works on 3V3 and not 5V like the Arduino.
This means that when you send serial data from the Arduino to the Pi then the 5V signal can damage the Pi, so you are best cutting down the signal to Pi by using a 1K / 512R potential divider.

When the Pi sends the serial signal to the Arduino it only sends a 3V3 signal. While often this works it is below the guaranteed to work voltage level and might be unreliable, like you are finding. You need a buffer to boost the signal up to 5V to get it to work reliably.

2 Likes

you seemed to have not yet a fundamental basic understanding of which program does what.

The Arduino-IDE is for writing code in a human-readable language: C++
Then the arduino-IDE translates this C++-code into assembler-code which readable by the microcontroller.

python is a programming-language that is supposed to run on computers not on microcontrollers.

python for computers (that run an OS like windows, linux, IOS) has no hardware-specific functions like switching IO-pins.

If you would like to write code with python that is able to switch IO-pins etc. you would have to use micropython which has these special functions for switching IO-pins etc.

Python is an interpreted language which is slower than a compiled language like C++ and the interpreter needs extra RAM.
So on something as small as a Arduino-Uno C++ is better suited.

If you can give an overview about what your robot-arm shall do all in all and how you want to control this robotarm all in all better suggestions can be made.

best regards Stefan

Thank you

I figured out the voltage needed at each robot joint to get the desired trajectory in Python language. I was planning to transfer voltage to the DC motor by serial communication with Arduino. In the above program in Python, I did not show how I found voltage (PWM) VM1, VM2, and VM3. Anyway, I will try in Arduino (C++) and will find voltage at each joint.

Python that runs on the Raspberry Pi under Linux, does have several libraries that allow individual I/O access, along with I2C and SPI and even PWM outputs.

2 Likes

The Python code you posted will not work. The function you define is never called and there is nothing to keep the code running, so apart from the initial once through when it is first run the code will do nothing and just stop.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.