How to make real time changes from a computer?

The concept is simple. I am making a robot arm, and a may do an IK simulation for it. What I need is to update the servos angle from an external file. as simple as a python file would work. But I need to do this in real time, while the Arduino UNO is working. I only need to modify the angle variable.

Wired or unwired? How far between PC and Arduino? What data rate (bits/sec)?

Wired, the rate is 9600

If the distance is a meter or less TTL UART serial is probably easiest. Use hardware serial via the USB on the Uno to your Python app.

For longer distances, RS485 can be a way to go.

The Uno only can work in real time, where do you suspect problems?

The computer :rofl:

Sorry, I had to say it :wink:

How do I send the information? I know almost nothing about communication or serial communication. All my projects have been with the Arduino working independently. Then, I need to know how am I going to modify the angle from the computer.

You'll have to write code for the PC side that sends the data. You can format that data however you like.

You'll then need to write code on the Arduino that receives that data and gets the number and tells the servo to go to that angle.

Start here for learning to work with serial data.

Here's an example of the Arduino side that I had laying around. It's probably not exactly what you want but it illustrates the concept. For this one it controls 6 servos. You send it <x,y> where x is the servo number and y is the position you want it run to.

int pos = 0;
int servo = 0;
boolean reading = false;
boolean gotServo = false;

Servo servos[6];

void setup(){

  Serial.begin(115200);

  servos[0].write(90);
  servos[1].write(65);
  servos[2].write(65);
  servos[3].write(170);
  servos[4].write(90);
  servos[5].write(120);

  for(int i = 0; i<6; i++){
    servos[i].attach(i + 4);
  }

  delay(500);

}

void loop(){

  if(Serial.available()){
    char c = Serial.read();

    if(c == '<'){
      pos = 0;
      servo = 0;
      reading = true;
      gotServo = false;
    }

    else if(c == '>' && reading == true){
      reading = false;
      if(pos >= 0 && pos <= 180 && servo < 6){

        Serial.print("Running servo ");
        Serial.print(servo);
        Serial.print(" to position :");
        Serial.println(pos);
        servos[servo].write(pos);
      }
    }

    else if(c == ',' && !gotServo){
      servo = pos;
      pos = 0;
      gotServo = true;
    }

    else if(c >= '0' && c <= '9' && reading == true){
      pos *= 10;
      pos += (c - '0');
    }
  }
}

And, unless your definition of "real time" is not very fast time, then nothing using serial communication can be considered real time.

The whole thread you sent me is about receiving information through serial communication. I want my computer to send.

Yes, but that sending will be useless unless you can also receive data. Nothing you send to the Arduino can have any effect there unless there is code to receive that data and act on it. The PC can't just magically "take control".

Here is an introduction to sending serial data from python.

https://pyserial.readthedocs.io/en/latest/shortintro.html

Does the python script work even without Arduino IDE interaction?

I don't understand what you mean. The link above shows how to send serial data from python. It has nothing to do with Arduino. It could be sending that data anywhere.

The other link shows how to receive serial data on the Arduino. It could be coming from anywhere.
Then you can do whatever you like with that data.

So you are saying that the first link is uploaded to the Arduino, for the Arduino to receive information, and the Python script sends the information to the Arduino to said serial port?

Yes sort of. You need to write two pieces of code. One on the PC that will send whatever instructions in whatever format you chose. Then you write code and add it to your program on the Arduino that can receive that data from Serial and determine what servo needs to move where based on that data and write to the servo.

I am starting to think that you are expecting something that can just magically tell servos to move without any changes to your existing Arduino code and that's not going to happen. There's no way for a PC to send arbitrary code to an Arduino and have it executed. You have to code some sort of interpreter for those commands on the Arduino.

Yeah don't worry, never expected it to be as easy as that, but I like to start simple. I will check the links out and I will update later because I will probably fail to completely tackle the problem. Thanks for the help

Post #8 mentioned Serial Input Basics - updated. There is a companion for the PC side: Demo of PC-Arduino comms using Python.

1 Like

Bookmarking.

How do I test these serial inputs, do I have to do it from a script through the serial port?. There is no quick way to know if Arduino is receiving the information? And, how do I know what serial port the Arduino?. I know the band rate, but I don't know what should I do if I have to test it.

I would write code for the Arduino first. Then I would send commands to it through the serial monitor. After that was working I'd work on whatever code to send the data to the Arduino.

You can have the arduino echo it back. Or you can have it blink a light. You can write whatever code on the Arduino you want. That's the whole point.

Look in the tools menu in the IDE.

Start by setting your main project aside and see if you can just send some simple data to an Arduino and echo it back. There are literally thousands of helpful tutorials on how to work with serial data between a computer and an Arduino. You've been given a couple of great ones in this thread. Set your main project aside and write some code and get familiar. See if you can send two numbers in a set of end markers and then pull them back apart and print them back to the serial monitor. Test it by sending data at it from the serial monitor until that works. Then you can move to having it sent from whatever other source you like.