Macroschlitten

Version vom 18. Januar 2014, 17:33 Uhr von Ptflea (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „{{Infobox Projekt |name = Macroschlitten |kategorie = Hardware |status = stable |autor = ptflea |beschreibung = |image …“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Crystal Clear action run.png
Macroschlitten

Status: stable

Beschreibung
Autor: ptflea
Version 0.5
PayPal Spenden für Macroschlitten

Stepper läuft mit 12V

Hier ist der Ardunio-Code:

  1#include <AFMotor.h>
  2#include <AccelStepper.h>
  3
  4// Connect a stepper motor with 48 steps per revolution (7.5 degree)
  5// to motor port #2 (M3 and M4)
  6int incomingByte;  
  7char Data[8];
  8int i;
  9unsigned long Zeit;
 10int bewegung;
 11int schritte;
 12
 13int motorStepsPerRev = 200;
 14float currentMaxSpeed = 1200.0;
 15float currentAcceleration = 300.0;
 16const int stepType = INTERLEAVE;
 17
 18AF_Stepper motor1(motorStepsPerRev, 2);
 19AF_Stepper motor2(motorStepsPerRev, 1);
 20
 21
 22void forwarda() {  
 23  motor1.onestep(FORWARD, stepType);
 24}
 25void backwarda() {  
 26  motor1.onestep(BACKWARD, stepType);
 27}
 28
 29AccelStepper accelA(forwarda, backwarda);
 30
 31
 32void forwardb() {  
 33  motor2.onestep(FORWARD, stepType);
 34}
 35void backwardb() {  
 36  motor2.onestep(BACKWARD, stepType);
 37}
 38
 39AccelStepper accelB(forwardb, backwardb);
 40
 41int range_in;
 42int byte_in[2];         // variable to store the VALID data from the port
 43char nextMsg;
 44
 45
 46void setup(){
 47 
 48  motor1.setSpeed(50);  // in rpm 
 49  motor2.setSpeed(50);  // in rpm
 50  
 51  //Accelstepper Setup
 52  accelA.setMaxSpeed(currentMaxSpeed);
 53  accelA.setAcceleration(currentAcceleration);  
 54  accelB.setMaxSpeed(currentMaxSpeed);
 55  accelB.setAcceleration(currentAcceleration);
 56  
 57  accelA.setMinPulseWidth(10);
 58  accelB.setMinPulseWidth(10);
 59  
 60  
 61  Serial.begin(9600);
 62  digitalWrite(13, HIGH); //turn on LED to indicate program has started
 63}
 64
 65void loop(){
 66  
 67
 68   do {
 69     // Wenn Daten verfügbar Zeichen in Data schreiben bis 4 Zeichen erreicht oder 0,5 Sekunden Warten nach dem ersten übertragenen byte
 70     if (Serial.available()) {
 71	if (i == 0)
 72            {
 73              bewegung = Serial.read();
 74            }
 75            else
 76            {
 77             Data[i-1] = Serial.read();
 78         }
 79	 i++;
 80	 }    
 81     if(i<1)Zeit = millis();
 82     } while (i<4&&(millis()-Zeit) < 500); //nach i< kommt die Anzahl der Zeichen
 83    // Serial.flush(); //empty serial buffer
 84       // Abschließende Null für gültigen String
 85     Data[i-1] = 0;    
 86    
 87     schritte = atof(Data);  // Wert von String zu Zahl wandeln wenn gewünscht
 88     i=0;
 89     decodeMessage(bewegung, schritte); 
 90     
 91}
 92
 93void decodeMessage(int msg, int range){
 94  int faktor = 5;
 95  //check command type and command value
 96  if(bitRead(msg, 0) == 1){
 97    //Bit 1 = High   DOWN (1)
 98   // motor1.step(faktor, FORWARD, INTERLEAVE); 
 99    	accelA.move(-range);
100	accelA.runToPosition();
101  }
102  if(bitRead(msg, 1) == 1){
103    //Bit 2 = High   UP (2)
104    //motor1.step(faktor, BACKWARD, INTERLEAVE); 
105	accelA.move(range);
106	accelA.runToPosition(); 
107  }
108  if(bitRead(msg, 2) == 1){
109    //Bit 3 = High   LEFT (4)
110    motor1.step(faktor, FORWARD, INTERLEAVE); 
111  }
112  if(bitRead(msg, 3) == 1){
113    //Bit 4 = High   RIGHT (8)
114    motor1.step(faktor, BACKWARD, INTERLEAVE); 
115
116  }
117  if((msg & 15) == 0){
118    //Bit 1-4 = Low
119    //sendMsg(000); //send a message back for testing purposes
120  }
121  sendMsg(msg, range);
122}
123
124
125
126void sendMsg(int msg, int range){
127  /* Processing uses Serial.buffer(4) to read the messages it receives,
128   * meaning that messages of 4 bytes long should be sent.
129   * Arduino sends integers as Strings, so to ensure 4 characters are sent
130   * for each message, I check the size of the integer to send and add zeroes
131   * as needed (zeroes ensure that the received message can easily be cast
132   * to an integer by Processing, which would not be the case with other, non-numerical, characters)
133   */
134  //if(Serial.available() > 0) Serial.flush();
135//  if(msg < 1000) Serial.print(0);
136//  if(msg < 100) Serial.print(0);
137//  if(msg < 10) Serial.print(0);
138  //delay(1000); 
139  
140  Serial.print(range); //...send a confirmation
141  Serial.println(msg);
142 
143}


Hier ist der Processing-Code:

  1import processing.serial.*;
  2
  3Serial myPort;
  4PFont font;
  5
  6String Schritte = "333";
  7int msgQueue[]; //the message queue
  8boolean msgLock; //message lock, active until last message is confirmed
  9int lstMsg; //last message sent
 10int schritt = 125;
 11
 12void setup(){
 13  size(400, 300);
 14  background(0);
 15  font = createFont("Verdana", 14);
 16  
 17  msgQueue = new int[0];
 18  
 19  
 20  println(Serial.list());
 21 myPort = new Serial(this, Serial.list()[Serial.list().length - 1], 9600); //the highest connected COM port is always my Arduino
 22  //myPort.buffer(4); //buffer 4 bytes of data before calling serialEvent()
 23  myPort.bufferUntil('\n');
 24
 25}
 26
 27void draw(){
 28  background(0);
 29  textFont(font, 14);
 30  text("Taste 8: vorwärts\nTaste 7: weit vorwärts\n\nTaste 2: zurück\nTaste 1: weit zurück\n\n9 -> Schritt erhöhen\n3 -> Schritt verkleinern", 25, 25);
 31  text("Schritte: " + schritt, 25, 230);
 32  parseQueue();
 33}
 34
 35void keyPressed(){
 36  if(int(key) == 50){// Taste 2 DOWN
 37    queueMessage(2); //
 38    //Schritte = Integer.toString(schritt);
 39    queueMessage(schritt);
 40  }
 41    if(int(key) == 56){// Taste 8 UP
 42    queueMessage(1); // 
 43    //Schritte = Integer.toString(schritt);
 44     queueMessage(schritt);
 45  }
 46    if(int(key) == 51){// Taste 3 Schritt verkleinern
 47    schritt = schritt - 5;
 48  }
 49    if(int(key) == 57){// Taste 9 Schritt erhöhen
 50    schritt = schritt + 5;
 51  }
 52    if(int(key) == 55){ //Taste 7 Grosser Schritt UP
 53    queueMessage(1); // 
 54    queueMessage(600);
 55  }
 56    if(int(key) == 49){ //Taste 1 Grosser Schritt DOWN
 57    queueMessage(2); // 
 58    queueMessage(600);
 59  }
 60}
 61
 62/* serialEvent(Serial myPort)
 63 * called when the amount of bytes specified in myPort.buffer()
 64 * have been transmitted, converts serial message to integer,
 65 * then sets this value in the chair object
 66 */
 67void serialEvent(Serial myPort){
 68  if(myPort.available() > 0){
 69    
 70    String message = myPort.readString(); //read serial buffer
 71    int msg = int(message); //convert message to integer
 72    println(msgQueue.length);
 73    myPort.clear(); //clear whatever might be left in the serial buffer
 74    //msgLock = false;
 75     if(msg == 0){
 76      println("Anweisung durchgeführt");
 77      msgLock = false;
 78    }
 79  }
 80}
 81
 82private void writeSerial(int msg){
 83  if(myPort.available() > 0) myPort.clear(); //empty serial buffer before sending
 84  myPort.write(msg);
 85}
 86
 87public void queueMessage(int msg){
 88  msgQueue = append(msgQueue, msg);
 89}
 90
 91private void parseQueue(){
 92  
 93    if(msgQueue.length > 0 && !msgLock) {
 94      msgLock = true; //lock queue, preventing new messages from being sent
 95      lstMsg = msgQueue[0]; //queue the first message on the stack
 96      writeSerial(lstMsg); // sende richtungsbefehl
 97      println("writing Richtung: " + lstMsg);
 98            
 99      msgQueue = subset(msgQueue, 1); // letzten befehl löschen
100      
101      lstMsg = msgQueue[0]; //queue the first message on the stack
102      Schritte = Integer.toString(lstMsg);
103      myPort.clear();
104      myPort.write(Schritte);// sende Schrittzahl
105      println("writing Schritte: " + lstMsg);
106      
107      msgQueue = subset(msgQueue, 1); // letzten befehl löschen
108    }
109
110}