Pages: [1]   Go Down
Author Topic: Void Loop is changing based on setup, in an impossible fashion  (Read 756 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
The Stuff of Legend!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey all... weird problem...  My main loop is being changed, magically almost, by additional lines in the setup loop that could have no possible bearing on the main loop : (

Code:
void setup() {

  gripper.write(15);  wrist.write(90);  wristRotate.write(90);  elbow.write(120);  shoulder.write(70);shoulderRotate.write(70); //damage control for initial SHOVE to 90 degs
 
  gripper.write(70);delay(1000); wrist.write(70);delay(1000);   //HARDWARE HOME, sequence should open claw and lift arm up and away
        wristRotate.write(70);delay(1000); elbow.write(70);delay(1000); shoulder.write(70);delay(1000);shoulderRotate.write(70);
 

     
     
     for(pos = 70; pos < 90; pos += 1)  // goes from 70 degrees to 90 degrees
  {                                 
    gripper.write(pos);wrist.write(pos);wristRotate.write(pos);elbow.write(pos);shoulder.write(pos);shoulderRotate.write(pos);
    delay(70);                       // waits 70ms for the servo to reach the position
  }           



}void loop() {

while (Serial.available()<6) {} // Wait 'till there are 6 Bytes waiting
for(int n=0; n<6; n++)    //  is 5 or 6?
   CurrentArray[n] = Serial.read(); // Then: Get them.

gripper.write(CurrentArray[5]); wrist.write(CurrentArray[4]);wristRotate.write(CurrentArray[3]);
elbow.write(CurrentArray[2]);shoulder.write(CurrentArray[1]);shoulderRotate.write(CurrentArray[0]);
}


When I add that For loop in the setup code... it screws up the Extremely simple Main Loop... shifting the channel positions over 4 spaces or so



God@*&#^$@(&*$^&(@ whatthefu&##(#9
it's been 7 goddamn hours! I know from the last topic I started! smiley-grin


Thanks for any help you can provide...
works Exactly as expected when that for loop is removed, but it's a really cool/simple effect
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
The Stuff of Legend!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

actually now it's off by one even when back to normal smiley-sad

Can't print out what the array positions are since the serial line being used by processing?
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
magically almost,

Sounds like an act of God.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
The Stuff of Legend!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

as far as I can tell yes, I've tried offering it various beverages & sundries, but it just knocks them off the table while seizing, apparently in disgust :'(
Logged

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11153
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That code layout is absolutely atrocious. PLEASE format your code sensibly if you expect anyone else to read it.

I don't see you initialising the serial port. Perhaps I'm just missing it due to your unusual code layout.

You seem to be receiving a sequence of messages each of the same length, but provide no way to detect or recover from any synchronisation errors. If you get even a tiny variation in the timing of the serial synchronisation, or any errors at all subsequently that cause a byte to be lost, your sender and receiver will get out of synch and never recover.

You need to provide some form of message delimiter. Either use an ascii encoding scheme and use a marker such as line termination to denote the end of a message, or use binary encoding and define your message in a way that lets you determine the start and end of a message. The ascii approach is much easier to implement.

Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
The Stuff of Legend!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah, kk   thought I could skip it for something so small


(Code is organized into relevant blocks, where if you understand the first command, the rest of the block is unneeded... means my code is all comprehensible on the same page, instead of being 100 pages long smiley-sad


Control code example smiley let your eyes burn!!!!

Code:
/* Servo controller for Robotic Arm
Teach pendant for Robotic arm... should allow axis control, and memorizing/saving arm status, and repeating arm movements from saved Files (g code?)
also displays a crude Wireframe simulation of the Arm
Code by: Johnny Graves */
import processing.serial.* ;                    // Use the included processing code serial library
int gripper=90, wrist=90,wristRotate=90,elbow=90,shoulder=90,shoulderRotate=90,     mousetracker=0, var1=0, var2=0, pos=0,mouseXinput=90,mouseYinput=90;                      // servo positions, start at 90//variable(s) for tracking/recording mouse position
int[] CurrentArray = new int[6];String[] line1 = {};
PFont z;Serial port;                     //Strings cannot be changed, so we use arrays for working on, & strings to print

float x = 500;float y = 700;float angle1 = 0.0;float angle2 = 0.0;float angle3 = 0.0;float angle4 = 0.0;float segLength = 100; //Specs for simulation of Arm
int GripperSimClaws = 100, GripperSimClaws2 = -55, stringCounter=0;

void setup()
{    size(720, 720);   strokeWeight(20.0);
CurrentArray[0] = 100;CurrentArray[1] = 100;CurrentArray[2] = 100;CurrentArray[3] = 100;CurrentArray[4] = 100;CurrentArray[5] = 0;
     rectMode(CENTER);             //draw rectangles from center point, rather than upper left
  println(Serial.list());       // List COM-ports
  port = new Serial(this, Serial.list()[1], 9600); //select second com-port from the list (0,1,2,3-)
  z = createFont("Arial",20,true);
   text("Ok to open gripper & return to Home? <Press Enter>",50,90);    //THERE'S NO diddling WAY TO PAUSE CODE, I just wasted 2 hours looking
}
     //Confirm movement, one motion at a time, then ask if arm is fully operational, or repeat the test? "Arm awaiting commands"
     //We DO still need to Ramp movement down (But not up?) so it doesn't JERK to a stop, it should slow down to a stop
     
     void draw() {   
  background(0,0,0);noStroke();
  fill(0,0,137,50);    rect(mouseX, mouseY, 100, 100, 30,30,30,30);      //make rect (X coordinate, Y, width, height)
  fill(26,196,255,50);rect(520, 520, 400, 400, 30, 30, 30, 100); //BG box
  fill(26,196,255,50);rect(420, 420, 200, 200, 30, 30, 30, 100); //Overhead BG box
  update(mouseX, mouseY);              //for some reason, this is the line to allow transmission to physical servos?
 
  stroke(26,234,255, 100);segLength = 100;angle1 = (mouseY/float(width)+1) * PI;angle2 = (mouseX/float(width)+1) * PI;angle3 = (CurrentArray[2]/float(width)*4 -0.9) * -PI;
  angle4 = (CurrentArray[3]/float(width)*4 -0.5) * -PI;
  //||||||||SIDE VIEW||||||||
  pushMatrix();segment(x, y, angle1); segment(segLength, 0,angle3); segLength=30 ;segment(100, 0,angle4);
  stroke(0,0,255, 200);strokeWeight(5.0);segLength = 30;segment(30, 0,GripperSimClaws);segLength = 30;segment(0, 0,GripperSimClaws2);strokeWeight(20.0);
  popMatrix();
 
  textFont(z, 30); fill(255);
    text(mouseX/4,70,120); text(mouseY/4,70,160);text(key,80,190);  textFont(z, 30); fill(0,150,255);  text("[MX]",0,120); text("[MY]",0,160);text("[Key]",0,190);
  text(CurrentArray[0],0,700);text(CurrentArray[1],60,700);text(CurrentArray[2],120,700);text(CurrentArray[3],180,700);text(CurrentArray[4],240,700);text(CurrentArray[5],300,700); 
    text("A/Q = Forearm",0,30);text("|S/W = Wrist",220,30);text("|D/E = WristRotate",420,30);text("R = Gripper",0,60);
      textFont(z, 10); fill(0,0,255); text("Rotate",10,670);text("Arm",70,670); text("Forearm",130,670);text("Wrist",190,670);text("WristRotate",240,670);text("Gripper",310,670);
  textFont(z, 20); fill(0,0,255); text("Overhead",350,350);text("Side",520,450);
 

 

 
  //|||||||OVERHEAD VIEW||||||
     pushMatrix();stroke(26,234,255, 100);segLength = 100;segment(400, 500, angle2); segment(10, 0,0);
  stroke(0,0,255, 200);strokeWeight(5.0);segLength = 30;segment(100, 0,GripperSimClaws);segLength = 30;segment(0, 0,GripperSimClaws2);strokeWeight(20.0);
     popMatrix();
     
        for(int i = 0; i < line1.length; i ++)  {
        if (i >25) break;     //# of lines to display
            text(line1[i], 10, height - 70 - i * 16);}
     
port.write (CurrentArray[0]);port.write (CurrentArray[1]);port.write (CurrentArray[2]);port.write (CurrentArray[3]);port.write (CurrentArray[4]);port.write (CurrentArray[5]); //Finally send the current array to the arm
  if(mousePressed) {if (CurrentArray[5] ==0)  {CurrentArray[5] =1; GripperSimClaws = 0; GripperSimClaws2 = 0;} else {CurrentArray[5] =0; GripperSimClaws = 100; GripperSimClaws2 = -55;}delay(100);} 

}    //Mouseclick, or space? saves the current location, & should show the file you're making/number of steps

//MOUSETRACKING
void update(int x, int y)  {
  mouseXinput= x/4;  CurrentArray[0] = mouseXinput;             //Calculate servo postion from mouseX/4 (0-180)
  mouseYinput= y/-4; CurrentArray[1] = -mouseYinput;   //mouse Y = Shoulder Rotate charachter
}

//SAVE COMMAND
void keyPressed() {      //this doesn't need to be it's own void actually?
    switch(key) {
    case('q'):case('Q'):  if (CurrentArray[2] <180) {CurrentArray[2] ++; } break; 
    case('a'):case('A'):  if (CurrentArray[2] >0)   {CurrentArray[2] --; } break; 
    case('w'):case('W'):  if (CurrentArray[3] <180) {CurrentArray[3] ++; } break; 
    case('s'):case('S'):  if (CurrentArray[3] >0)   {CurrentArray[3] --; } break;
    case('d'):case('D'):  if (CurrentArray[4] >0)   {CurrentArray[4] --; } break;       
    case('e'):case('E'):  if (CurrentArray[4] <180) {CurrentArray[4] ++; } break; 
    case('r'):case('R'):  if (CurrentArray[5] ==0)  {CurrentArray[5] =1; GripperSimClaws = 0; GripperSimClaws2 = 0;} else {CurrentArray[5] =0; GripperSimClaws = 100; GripperSimClaws2 = -55;}break;  //if gripper is open, close it, else open it
    }    //End switch statement
 
  if (key == 'y') {       //Write current array, into a string (That string is placed in an array, & that Array later saved/loaded)
      String mystring = CurrentArray[0] + "," + CurrentArray[1] + "," + CurrentArray[2] + "," + CurrentArray[3] + "," + CurrentArray[4] + "," + CurrentArray[5] + "\t";
        line1 = splice(line1, mystring, 0);  }

  if (key == 'p') {  saveStrings("Program thingy.txt", line1);} //I think it's overwriting the original with each new save? so also need to append it, & you should be able to choose name
  //Allegedly... it can save an Array, of STRINGS.... wtf?}       //Write current array, into a string





  if (key == 'i') {  line1 = loadStrings("Program thingy.txt");} //Load file... preferably by selecting it/browse
}
                                   
//SEGMENT DRAWING FOR SIMULATOR
void segment(float x, float y, float a) {
  translate(x, y);
  rotate(a);
  line(0, 0, segLength, 0);
}
« Last Edit: January 04, 2013, 09:37:31 pm by Jack777 » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
textFont(z, 30); fill(255);
    text(mouseX/4,70,120); text(mouseY/4,70,160);text(key,80,190);  textFont(z, 30); fill(0,150,255);  text("[MX]",0,120); text("[MY]",0,160);text("[Key]",0,190);
  text(CurrentArray[0],0,700);text(CurrentArray[1],60,700);text(CurrentArray[2],120,700);text(CurrentArray[3],180,700);text(CurrentArray[4],240,700);text(CurrentArray[5],300,700); 
    text("A/Q = Forearm",0,30);text("|S/W = Wrist",220,30);text("|D/E = WristRotate",420,30);text("R = Gripper",0,60);
      textFont(z, 10); fill(0,0,255); text("Rotate",10,670);text("Arm",70,670); text("Forearm",130,670);text("Wrist",190,670);text("WristRotate",240,670);text("Gripper",310,670);
  textFont(z, 20); fill(0,0,255); text("Overhead",350,350);text("Side",520,450);

I hope this is intended as a joke.

This site might help you: http://www.ioccc.org

What's this?

Code:
String[] line1 = {
};

An array of one String object? Strings have bugs. Avoid them. Your heavy use of String may or may not be responsible for your problems, but they aren't helping.

Quote
My main loop is being changed, magically almost, by additional lines  ...

Sounds like memory corruption. Like the String library causes.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you enjoy that sort of coding style, I found this on the IOCC web page:

Code:
a<2; ++a)if(d+a>=0&&d+a<   48)for(b=-1; b<2; ++b)if(   e+b>=0&&e+b< 48)k+=(a||b)
&&A(d+a,e+b); S(A(d,e)?k   ==2||k==3:k==3); } c.c=c.   d; } c.g= ++c.g% (f%2?30:
120); c.f +=2; c.a+=f%2?   c.f/1000:0; c.b+=f==3?-2:   (f==5?2:0); a=c.a>159?-8:
0; b=c.b>399?-8:(c.b<81?   8:0); c.a=a?0:c.a; c.b=b?   240:c.b; L S(((d-a>=0)&&(
d-a<48)&&(e-b>=0)&&(e-b<   48))?A(d-a,e-b):((rand()%   8 ) == 1 )) ; c.c = c.d ;
XFillRectangle(i,u,g,0,0   ,640, 480); L{ if A( d,e)   XCopyArea(i,p,u,g,0,0,20,
20,d*20-c.a,e*20-c.b); }   if( f%2){ for(d=c.a+ 100;   d<c.a+120; ++d)for(e=c.b+
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12428
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Jack
To reformat your code use you can press CTRL-T in the IDE.

+ never put more than one statement on a line
+ use meaningful variable names
+ minimize comments to explaining the WHY - not the HOW as that can be derived from code;
+ use indentation in for/while/if/else blocks

There are many books about style but these lines above reappear again and again. There are tools to help you out.

Furthermore I think you should consider to group a number of statements in separate functions making your setup() easier to follow. We don't know anything about your code except the code itself ... and that is a lot less than you do.

BTW this is processing code not arduinese...

I reformatted part of your code,  fixed one initialization and some coments (<5 minutes) how about this readability?
Code:
/* Servo controller for Robotic Arm
Teach pendant for Robotic arm... should allow axis control, and memorizing/saving arm status, and repeating arm movements from saved Files (g code?)
also displays a crude Wireframe simulation of the Arm
Code by: Johnny Graves */

import processing.serial.* ;                    // Use the included processing code serial library

// for servo positions, start at 90
int gripper = 90,
int wrist = 90;
int wristRotate = 90;
int elbow = 90;
int shoulder = 90;
int shoulderRotate = 90;

// for tracking/recording mouse position
int mousetracker = 0;
int var1 = 0, var2 = 0;   // what are these representing?
int pos = 0;
int mouseXinput = 90, mouseYinput = 90;   
                   
int[] CurrentArray = { 100, 100, 100, 100, 100, 0};  // allocation and initialization @ once
String[] line1 = {};
PFont z;
Serial port;
           
// Specs for simulation of Arm  <<<< ???
float x = 500;
float y = 700;
float angle1 = 0.0;    // should this not be an array[0..3] ????
float angle2 = 0.0;
float angle3 = 0.0;
float angle4 = 0.0;
float segLength = 100;
int GripperSimClaws = 100;
int GripperSimClaws2 = -55
int stringCounter = 0;

void setup()
{   
size(720, 720);   
strokeWeight(20.0);

rectMode(CENTER);

// List COM-ports, and select second com-port from the list (0,1,2,3-)
println(Serial.list());       
port = new Serial(this, Serial.list()[1], 9600);

z = createFont("Arial",20,true);
text("Ok to open gripper & return to Home? <Press Enter>",50,90);   

// TODO: pause code how?   // shorter comments save time ;
}

// rest is homework ;)



@Nick
fun smiley-wink


Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11153
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah, kk   thought I could skip it for something so small

Now you know better. smiley

(Code is organized into relevant blocks, where if you understand the first command, the rest of the block is unneeded... means my code is all comprehensible on the same page, instead of being 100 pages long smiley-sad

That may have been your intention, but IMO the resulting layout is hideously incomprehensible. There are good and sensible reasons why we adopt standardised code layouts. The scheme you are following makes it difficult to understand the intended structure of the code and see where the actual structure differs from it. I strongly suggest you stop trying to invent your own scheme, and adopt a standard one. Do it for the sake of those of us you're asking to review your code, even if you don't believe you will benefit from it too. (You will benefit, though, once you are used to it.)
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: