modified: servoTest/servoTest.ino

new file:   testSequence2.py
This commit is contained in:
ketrptr 2025-04-01 15:49:44 +02:00
parent c22a740e13
commit 024c0ff9e3
2 changed files with 51 additions and 18 deletions

View File

@ -34,9 +34,11 @@ const byte axCount = 4;
// serPos and axis array must have same order as CMD enum!! // serPos and axis array must have same order as CMD enum!!
int servoPos[axCount]; int servoPos[axCount];
const Servo *axis[axCount] = {&S_left, &S_right, &S_turn, &S_grip}; const Servo *axis[axCount] = {&S_left, &S_right, &S_turn, &S_grip};
int defaultPos[] = {90, 90, 90, 90}; int defaultPos[] = {90, 90, 90, 90};
bool absPos; // absolute /relative positioning bool absPos; // absolute /relative positioning
// Move axis[dir] to the position set in servoPos[dir] // Move axis[dir] to the position set in servoPos[dir]
void movePos(CMD dir) { void movePos(CMD dir) {
// safety check // safety check
@ -44,19 +46,24 @@ void movePos(CMD dir) {
error("Not a valid Axis!!"); error("Not a valid Axis!!");
return; return;
} }
if (servoPos[dir] < 20 || servoPos[dir] > 175) { //arm can crash with small or large angles
if (servoPos[dir] < 20 || servoPos[dir] > 170) {
error("move out of range"); error("move out of range");
return; return;
} }
// if actual servo pos is different to wanted move towards it // if actual servo pos is different to wanted move towards it
int val = axis[dir]->read(); int val = axis[dir]->read(); //get current position of dir
if (val != servoPos[dir]) { if (val != servoPos[dir]) {
int delta = servoPos[dir] - val; int delta = servoPos[dir] - val; //wantPos - havePos
int inc; int inc; //movement sptep size, depends on distance to wantPos
while (delta) { while (delta) {
//calculate movement increment
if(dir!=T) inc = (abs(delta) > 20) ? constrain((abs(delta) / 10), 1, 5) : 1; if(dir!=T) inc = (abs(delta) > 20) ? constrain((abs(delta) / 10), 1, 5) : 1;
else inc =1; else inc =1; //turn axis cannot move as fast
//Move positive / negative direction
if (delta > 0) { if (delta > 0) {
axis[dir]->write(val + inc); axis[dir]->write(val + inc);
delta -= inc; delta -= inc;
@ -66,29 +73,40 @@ void movePos(CMD dir) {
delta += inc; delta += inc;
val -= inc; val -= inc;
} }
delay(20); // TODO no delay //step delay
//short --> fast movement
//long --> slow movement, noticable steps
delay(20);
} }
} }
} }
//Global current command value and type //Global current command value and type
//Commands have form <CMD><Val>
int cmdVal; int cmdVal;
CMD cmd; CMD cmd;
// Handle move command
// L, R, T, G
void moveCmdHandler() { void moveCmdHandler() {
Serial.print("Servo_Command: "); Serial.print("Servo_Command: ");
Serial.print(CMD2char(cmd)); Serial.print(CMD2char(cmd));
Serial.println(cmdVal); Serial.println(cmdVal);
//if abolute positioning set wantPos for current axis (cmd) to cmd val
if (absPos) { if (absPos) {
servoPos[cmd] = constrain(cmdVal, 20, 165); servoPos[cmd] = constrain(cmdVal, 20, 165);
} else { } else { // if relative positioning add val to wantPos
servoPos[cmd] = constrain(servoPos[cmd] + cmdVal, 20, 165); servoPos[cmd] = constrain(servoPos[cmd] + cmdVal, 20, 165);
} }
//Serial.print("new val: "); //Serial.print("new val: ");
//Serial.println(servoPos[cmd]); //Serial.println(servoPos[cmd]);
} }
// Status command handler
// H1 command
// print posiotion and abs/rel positioning
void statusHandler() { void statusHandler() {
Serial.println((absPos) ? "Absolute Positioning" : "Relative Positioning"); Serial.println((absPos) ? "Absolute Positioning" : "Relative Positioning");
for (int i = 0; i < axCount; i++) { for (int i = 0; i < axCount; i++) {
@ -108,6 +126,7 @@ void setPositioningHandler(){
} }
//move arm to (90,90,90,90) //move arm to (90,90,90,90)
//Homing sequence
void defaultPosHandler(){ void defaultPosHandler(){
memcpy(servoPos, defaultPos, sizeof(int)*axCount); memcpy(servoPos, defaultPos, sizeof(int)*axCount);
} }
@ -126,7 +145,7 @@ void setup() {
S_turn.attach(S_turn_pin); S_turn.attach(S_turn_pin);
S_grip.attach(S_grip_pin); S_grip.attach(S_grip_pin);
defaultPosHandler(); defaultPosHandler(); //move Home (90,90,90,90)
Serial.begin(BAUDRATE); Serial.begin(BAUDRATE);
absPos = true; absPos = true;
@ -138,30 +157,30 @@ void loop() {
// print changes // print changes
int val; int val;
//check if all axis are where they should be
//if not move to position
for (int i = 0; i < axCount; i++) { for (int i = 0; i < axCount; i++) {
val = axis[i]->read(); val = axis[i]->read();
if (val != servoPos[i]) { if (val != servoPos[i]) {
// Serial.print("-- ");
// Serial.println(i);
// Serial.print(val);
// Serial.print("-->");
// Serial.println(servoPos[i]);
movePos(i); movePos(i);
} }
delay(50); delay(50);
} }
//IMPORTANT! This tells controller that move is done and next command can be sent
Serial.println("READY"); Serial.println("READY");
// read position command from serial // read position command from serial
if (Serial.available()) { if (Serial.available()) {
int r = Serial.readBytesUntil('\n', serialBuf, bufSize); int r = Serial.readBytesUntil('\n', serialBuf, bufSize);
// Serial.print("read: ");
// Serial.println(r);
// Serial.println(serialBuf);
if (r < 2) { if (r < 2) {
error("command to short!"); error("command to short!");
return; return;
} }
//parse value after cmd
String numStr = ""; String numStr = "";
for (int i = 1; i < r; i++) { for (int i = 1; i < r; i++) {
if (isDigit(serialBuf[i]) || isPunct(serialBuf[i])) { if (isDigit(serialBuf[i]) || isPunct(serialBuf[i])) {
@ -199,6 +218,7 @@ void loop() {
cmd = -1; cmd = -1;
break; break;
} }
//if command is valis call corresponding handler from handlers array
if(cmd >= 0){ if(cmd >= 0){
cmdVal = numStr.toInt(); cmdVal = numStr.toInt();
handlers[cmd](); handlers[cmd]();

13
testSequence2.py Normal file
View File

@ -0,0 +1,13 @@
import meControll
arm = meControll.meArm()
pickupDouble = [b'D1', b'S1', b'T70', b'L50', b'R120', b'L20', b'R140', b'G180', b'R110' ]
m2 = [b'T130', b'R130', b'L30', b'G90', b'R100', b'D1']
arm.addMove(pickupDouble)
arm.addMove(m2)
arm.execMove(0)
arm.execMove(1)