your while-loop
while ((timeNow1 - timeAtStart1) < time1) {
dxl.setGoalVelocity(DXL_ID_1, 300);
timeNow1 = millis();
}
executes the function-call
dxl.setGoalVelocity(DXL_ID_1, 300);
which needs some time
after executing this function-call you update
timeNow1 = millis();
this means the new value of timeNow1 is made
after the function-call and therefore has the additional milliseconds this function-call needs
use thiscode-version tp measure this time
#include <DynamixelShield.h>
#include <Servo.h>
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_MEGA2560)
#include <SoftwareSerial.h>
SoftwareSerial soft_serial(7, 8); // DYNAMIXELShield UART RX/TX
#define DEBUG_SERIAL soft_serial
#elif defined(ARDUINO_SAM_DUE) || defined(ARDUINO_SAM_ZERO)
#define DEBUG_SERIAL SerialUSB
#else
#define DEBUG_SERIAL Serial
#endif
DynamixelShield dxl;
Servo myservo;
const uint8_t DXL_ID_1 = 1;
const uint8_t DXL_ID_2 = 2;
const uint8_t DXL_ID_3 = 3;
const float DXL_PROTOCOL_VERSION = 2.0;
float distance_to_time = 60 / (34.2 * 2 * PI * 10);
int pos = myservo.read();
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(10);
// Servo setup
myservo.write(0); // this ensures that the linear servo is initially at rest
myservo.attach(11);
// dynamixel port baud rate: 57600 bps
dxl.begin(9600);
// Set Port Protocol Version. This has to match with DYNAMIXEL protocol version.
dxl.setPortProtocolVersion(DXL_PROTOCOL_VERSION);
// Get DYNAMIXEL information
dxl.ping(DXL_ID_1);
dxl.ping(DXL_ID_2);
dxl.ping(DXL_ID_3);
// Turn off torque when configuring items in EEPROM area
dxl.torqueOff(DXL_ID_1);
dxl.setOperatingMode(DXL_ID_1, OP_VELOCITY);
dxl.torqueOn(DXL_ID_1);
dxl.torqueOff(DXL_ID_2);
dxl.setOperatingMode(DXL_ID_2, OP_VELOCITY);
dxl.torqueOn(DXL_ID_2);
dxl.torqueOff(DXL_ID_3);
dxl.setOperatingMode(DXL_ID_3, OP_VELOCITY);
dxl.torqueOn(DXL_ID_3);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
String mess = Serial.readStringUntil('.'); // input values are time "__,__,__,__."
Serial.print("Input values: ");
Serial.println(mess);
delay(10);
int mess_1 = mess.indexOf(','); // this value represents the amount of time you want to activate motor 1
String distance1 = mess.substring(0, mess_1); // positive value: clockwise | negative value: counterclockwise
delay(10);
int mess_2 = mess.indexOf(',', mess_1 + 1); // this value represents the amount of time you want to activate motor 2
String distance2 = mess.substring(mess_1 + 1, mess_2); // positive value: clockwise | negative value: counterclockwise
delay(10);
int mess_3 = mess.indexOf(',', mess_2 + 1); // this value represents the amount of time you want to activate motor 3
String distance3 = mess.substring(mess_2 + 1, mess_3); // positive value: clockwise | negative value: counterclockwise
delay(10);
int mess_4 = mess.indexOf(',', mess_3 + 1); // this value represents the amount of time you want to activate linear servo
String position4 = mess.substring(mess_3 + 1, mess_4); // positive value: clockwise | negative value: counterclockwise
delay(10);
// convert string to integer
int moving_distance1 = distance1.toInt();
int moving_distance2 = distance2.toInt();
int moving_distance3 = distance3.toInt();
int target_position4 = position4.toInt();
unsigned long startingtime = millis();
dxl.setGoalVelocity(DXL_ID_1, 300);
unsigned long endingtime = millis();
Serial.print(endingtime - startingtime);
Serial.println("------------------ INPUT VALUES ------------------");
Serial.print("Moving distance 1 (mm) = ");
Serial.println(moving_distance1);
Serial.print("Moving distance 2 (mm) = ");
Serial.println(moving_distance2);
Serial.print("Moving distance 3 (mm) = ");
Serial.println(moving_distance3);
Serial.print("target position for linear servo (mm) = ");
Serial.println(target_position4);
Serial.println("--------------------------------------------------");
if (moving_distance1 > 0) {
float time1 = moving_distance1 * 1000 * distance_to_time; // unit is in milliseconds
Serial.print("Time that the motor should run: ");
Serial.print(time1);
Serial.println(" milliseconds");
unsigned long timeAtStart1 = millis();
unsigned long timeNow1 = millis();
unsigned long after_F_call;
unsigned long before_F_call;
while ((timeNow1 - timeAtStart1) < time1) {
before_F_call = millis();
dxl.setGoalVelocity(DXL_ID_1, 300);
after_F_call = millis();
timeNow1 = millis();
}
Serial.println("time for function-call dxl.setGoalVelocity(DXL_ID_1, 300)");
Serial.print(after_F_call - before_F_call);
Serial.println(" milliseconds");
Serial.print("Motor 1 rotation time: ");
Serial.print(timeNow1 - timeAtStart1);
Serial.println(" milliseconds");
Serial.print("Motor 1 roated clockwise for ");
Serial.print(moving_distance1);
Serial.println(" mm.");
}
else if (moving_distance1 < 0) {
int time2 = (-1) * 1000 * moving_distance1 * distance_to_time;
unsigned long timeAtStart2 = millis();
unsigned long timeNow2 = millis();
while ((timeNow2 - timeAtStart2) < time2) {
dxl.setGoalVelocity(DXL_ID_1, 1324);
timeNow2 = millis();
}
Serial.print("Motor 1 rotation time: ");
Serial.print(timeNow2 - timeAtStart2);
Serial.println(" milliseconds");
Serial.print("Motor 1 rotated counterclockwise for ");
Serial.print(moving_distance1 * (-1));
Serial.println(" mm.");
}
else {
dxl.setGoalVelocity(DXL_ID_1, 0, UNIT_RPM);
Serial.println("Motor 1 did not rotate.");
}
// motor 2 control
if (moving_distance2 > 0) {
int time3 = 1000 * moving_distance2 * distance_to_time; // unit is in milliseconds
unsigned long timeAtStart3 = millis();
unsigned long timeNow3 = millis();
while ((timeNow3 - timeAtStart3) <= time3) {
dxl.setGoalVelocity(DXL_ID_2, 300);
timeNow3 = millis();
}
Serial.print("Motor 2 rotation time: ");
Serial.print(timeNow3 - timeAtStart3);
Serial.println(" milliseconds");
Serial.print("Motor 2 roated clockwise for ");
Serial.print(moving_distance2);
Serial.println(" mm.");
}
else if (moving_distance2 < 0) {
int time4 = (-1) * 1000 * moving_distance2 * distance_to_time;
unsigned long timeAtStart4 = millis();
unsigned long timeNow4 = millis();
while ((timeNow4 - timeAtStart4) < time4) {
dxl.setGoalVelocity(DXL_ID_2, 1324);
timeNow4 = millis();
}
Serial.print("Motor 2 rotation time: ");
Serial.print(timeNow4 - timeAtStart4);
Serial.println(" milliseconds");
Serial.print("Motor 2 rotated counterclockwise for ");
Serial.print(moving_distance2 * (-1));
Serial.println(" mm.");
}
else {
dxl.setGoalVelocity(DXL_ID_2, 0, UNIT_RPM);
Serial.println("Motor 1 did not rotate.");
}
// motor 3 control
if (moving_distance3 > 0) {
int time5 = 1000 * moving_distance3 * distance_to_time; // unit is in milliseconds
unsigned long timeAtStart5 = millis();
unsigned long timeNow5 = millis();
while ((timeNow5 - timeAtStart5) < time5) {
dxl.setGoalVelocity(DXL_ID_3, 300);
timeNow5 = millis();
}
Serial.print("Motor 3 rotation time: ");
Serial.print(timeNow5 - timeAtStart5);
Serial.println(" milliseconds");
Serial.print("Motor 3 roated clockwise for ");
Serial.print(moving_distance3);
Serial.println(" mm.");
}
else if (moving_distance3 < 0) {
int time6 = (-1) * 1000 * moving_distance3 * distance_to_time;
unsigned long timeAtStart6 = millis();
unsigned long timeNow6 = millis();
while ((timeNow6 - timeAtStart6) < time6) {
dxl.setGoalVelocity(DXL_ID_3, 1324);
timeNow6 = millis();
}
Serial.print("Motor 3 rotation time: ");
Serial.print(timeNow6 - timeAtStart6);
Serial.println(" milliseconds");
Serial.print("Motor 3 rotated counterclockwise for ");
Serial.print(moving_distance3 * (-1));
Serial.println(" mm.");
}
else {
dxl.setGoalVelocity(DXL_ID_3, 0, UNIT_RPM);
Serial.println("Motor 1 did not rotate.");
}
// linear servo
}
}