public void setup() {

    brek = new AudioSample[4][4];
    // load audio samples

    minim = new Minim(this);

    for (int i = 0; i < 4; i++) {
      println("break-beat-0" + (i + 1) + ".mp3");
      brek[i][0] = minim.loadSample("break-beat-0" + (i + 1) + ".mp3", 512);
      brek[i][1] = minim.loadSample("break-perc-0" + (i + 1) + ".mp3", 512);
      brek[i][2] = minim.loadSample("break-piano-0" + (i + 1) + ".mp3", 512);
      brek[i][3] = minim.loadSample("break-strings-0" + (i + 1) + ".mp3", 512);
    }

    size(cellSize * cols, cellSize * rows);

    //  bytes[0]=0;
    // bytes[1]=1;

    grid = new Cell[cols][rows];
    for (int i = 0; i < cols; i++) {
      for (int j = 0; j < rows; j++) {
        grid[i][j] = new Cell(i * cellSize, j * cellSize, cellSize, cellSize);
      }
    }
    println(Serial.list());

    String portName = Serial.list()[2];
    myPort = new Serial(this, portName, 9600);
  }
  public void closePort() {

    if (serial != null) {
      serial.stop();
      serial.dispose();
    }
    serial = null;
  }
Exemplo n.º 3
0
  public void P2() {

    buttonP0.setColorBackground(gray_);
    buttonP1.setColorBackground(gray_);
    buttonP2.setColorBackground(green_);
    // Open the port you are using at the rate you want:
    if (portopen == true) {
      myPort.stop();
    }
    myPort = new Serial(this, Serial.list()[2], 9600);
    portopen = true;
  }
Exemplo n.º 4
0
  public void sendPacket(int[] thisFrame) {
    // calculate the length of the total array
    // header byte + length (2 bytes) + command + checksum:
    int packetLength = thisFrame.length + 4;

    // set up the array you'll actually send:
    int[] packet = new int[packetLength];
    packet[0] = 0x7E;

    // get the high byte and the low byte of the length of the frame:
    packet[1] = thisFrame.length / 256;
    packet[2] = thisFrame.length % 256;

    // add the frame to the packet:
    for (int b = 0; b < thisFrame.length; b++) {
      packet[b + 3] = thisFrame[b];
    }

    // checksum the command:
    packet[packetLength - 1] = checkSum(thisFrame);

    if (DEBUG) System.out.print("< ");
    // send it out the serial port:
    for (int c = 0; c < packet.length; c++) {
      port.write(packet[c]);
      if (DEBUG) System.out.print(parent.hex(packet[c], 2) + " ");
    }
    if (DEBUG) System.out.println();
  }
 public String[] listPort() {
   String[] portList = Serial.list();
   for (String i : portList) {
     println(i);
   }
   return portList;
 }
Exemplo n.º 6
0
 public void InitSerial(float portValue) {
   // initialize the serial port selected in the listBox
   println(
       "initializing serial "
           + PApplet.parseInt(portValue)
           + " in serial.list()"); // for debugging
   // grab the name of the serial port
   String portPos = Serial.list()[PApplet.parseInt(portValue)];
   // initialize the port
   serial = new Serial(this, portPos, 115200);
   // read bytes into a buffer until you get a linefeed (ASCII 10):
   serial.bufferUntil('\n');
   println("done init serial");
   // initialized com port flag
   init_com = 1;
 }
Exemplo n.º 7
0
  public void CONFIGoff() {

    myPort.write("j"); // exit config command
    buttonWRITE.setColorBackground(gray_);
    buttonREAD.setColorBackground(gray_);
    buttonCONFon.setColorBackground(gray_);
    buttonCONFoff.setColorBackground(green_);
    writeEnable = false;
    readEnable = false;
  }
Exemplo n.º 8
0
  public void CONFIGon() {

    myPort.write("i"); // enter config command
    buttonWRITE.setColorBackground(green_);
    buttonREAD.setColorBackground(green_);
    buttonCONFon.setColorBackground(green_);
    buttonCONFoff.setColorBackground(gray_);
    writeEnable = true;
    readEnable = true;
  }
Exemplo n.º 9
0
  public void processSerialData() {
    byte[] inBuf = new byte[20];
    switch (serial.read()) {
      case 'A':
        // wait for all data arrived
        while (serial.available() < 16) {}
        // read all data
        serial.readBytes(inBuf);
        Acc_RAW = (inBuf[1] << 8) + (inBuf[0] & 0xFF);
        Gyro_RAW = (inBuf[3] << 8) + (inBuf[2] & 0xFF);
        // int intbit = 0;
        // intbit = (inBuf[7] << 24) | ((inBuf[6] & 0xFF) << 16) | ((inBuf[5] & 0xFF) << 8) |
        // (inBuf[4] & 0xFF);
        // Angle = Float.intBitsToFloat(intbit);
        int AngleInt = (inBuf[5] << 8) + (inBuf[4] & 0xFF);
        Angle = PApplet.parseFloat(AngleInt) / 10;
        Acc_Angle = (inBuf[7] << 8) + (inBuf[6] & 0xFF);
        Gyro_Rate = (inBuf[9] << 8) + (inBuf[8] & 0xff);
        Drive = (inBuf[11] << 8) + (inBuf[10] & 0xFF);
        statusFlag = (inBuf[13] << 8) + (inBuf[12] & 0xFF);
        int BatLevelInt = (inBuf[15] << 8) + (inBuf[14] & 0xFF);
        Steer = (inBuf[17] << 8) + (inBuf[16] & 0xFF);
        BatLevel = PApplet.parseFloat(BatLevelInt) / 10;
        println(
            "Acc="
                + Acc_RAW
                + "  Gyro="
                + Gyro_RAW
                + "  Angle="
                + Angle
                + "  Acc_Angle="
                + Acc_Angle
                + "  Gyro_Rate="
                + Gyro_Rate
                + "  Drive="
                + Drive
                + "  Status="
                + statusFlag);
        break;

      case 'E':
        // wait for all data arrived
        while (serial.available() < 6) {}
        // read all data
        serial.readBytes(inBuf);
        int P = (inBuf[1] << 8) + (inBuf[0] & 0xFF);
        int I = (inBuf[3] << 8) + (inBuf[2] & 0xFF);
        int D = (inBuf[5] << 8) + (inBuf[4] & 0xFF);
        conf_KP.setValue(P);
        conf_KI.setValue(I);
        conf_KD.setValue(D);
        println("P=" + P + " I=" + I + " D=" + D);
        break;
    }
    serial.clear();
  }
Exemplo n.º 10
0
 public void InitComDropdown() {
   // Initialize portCommList
   int posX = 20;
   int posY = 70;
   PortsList = controlP5.addDropdownList("portComList", posX, posY, 100, 84);
   // Set the background color of the list (you wont see this though).
   PortsList.setBackgroundColor(color(200));
   // Set the height of each item when the list is opened.
   PortsList.setItemHeight(20);
   // Set the height of the bar itself.
   PortsList.setBarHeight(15);
   // Set the lable of the bar when nothing is selected.
   PortsList.captionLabel().set("Select COM port");
   // Set the top margin of the lable.
   PortsList.captionLabel().style().marginTop = 3;
   // Set the left margin of the lable.
   PortsList.captionLabel().style().marginLeft = 3;
   // Set the top margin of the value selected.
   PortsList.valueLabel().style().marginTop = 3;
   // Store the Serial ports in the string comList (char array).
   comList = serial.list();
   // We need to know how many ports there are, to know how many items to add to the list, so we
   // will convert it to a String object (part of a class).
   String comlist = join(comList, ",");
   // We also need how many characters there is in a single port name, we\u00b4ll store the chars
   // here for counting later.
   String COMlist = comList[0];
   // Here we count the length of each port name.
   int size2 = COMlist.length();
   // Now we can count how many ports there are, well that is count how many chars there are, so we
   // will divide by the amount of chars per port name.
   int size1 = comlist.length() / size2;
   // Now well add the ports to the list, we use a for loop for that. How many items is determined
   // by the value of size1.
   for (int i = 0; i < size1; i++) {
     // This is the line doing the actual adding of items, we use the current loop we are in to
     // determin what place in the char array to access and what item number to add it as.
     PortsList.addItem(comList[i], i);
   }
   // Set the color of the background of the items and the bar.
   PortsList.setColorBackground(color(60));
   // Set the color of the item your mouse is hovering over.
   PortsList.setColorActive(color(255, 128));
 }
  public void draw() {
    background(0);

    for (int i = 0; i < cols; i++) {
      for (int j = 0; j < rows; j++) {

        if ((i & 1) == 0) {
          // even rows white
          grid[i][j].display(255);
        } else {
          // odd rows gray
          grid[i][j].display(220);
        }
      }
    }

    if (play == true) {
      int j;
      if (millis() - starttime < delaytime) {
        count_up = (millis() - starttime);
        count_down = delaytime - count_up;
        steptimer = floor(4 / (delaytime / count_up));
        fill(0);
        //        textSize(12);
        //        text(steptimer, mouseX, mouseY);
        for (j = 0; j < rows; j++) {
          grid[steptimer][j].display(120);
          if (grid[steptimer][j].active) {
            grid[steptimer][j].trigger(steptimer, j);
          }
        }

        switch (steptimer) {
          case 0:
            uno = PApplet.parseByte(unbinary("00010001"));
            due = PApplet.parseByte(unbinary("00010001"));
            break;
          case 1:
            uno = PApplet.parseByte(unbinary("00100010"));
            due = PApplet.parseByte(unbinary("00100010"));
            break;
          case 2:
            uno = PApplet.parseByte(unbinary("01000100"));
            due = PApplet.parseByte(unbinary("01000100"));
            break;
          case 3:
            uno = PApplet.parseByte(unbinary("10001000"));
            due = PApplet.parseByte(unbinary("10001000"));
            break;
        }
        print(binary(uno));
        println(binary(due));

        myPort.write(due);
        myPort.write(uno);
        myPort.write(',');
      } else {
        starttime = millis();
        j = 0;
      }

      for (int i = 0; i < cols; i++) {
        for (int k = 0; k < rows; k++) {

          if (grid[i][k].active) {
            // even rows white

            if ((i + 4 * k) < 8) {
              uno |= (1 << i + 4 * k);
            } else {
              due |= (1 << (i + 4 * k) - 8);
            }
          } else {
            // odd rows gray

            if ((i + 4 * k) < 8) {
              uno &= ~(1 << i + 4 * k); // chiedete a vanzati [email protected]
            } else {
              due &= ~(1 << (i + 4 * k) - 8);
            }
          }
        }
      }
    }

    // print(binary(uno));
    // print(binary(due));

    myPort.write(due);
    myPort.write(uno);
    myPort.write(',');

    println();
  }
Exemplo n.º 12
0
 public void SAVE_PID(int theValue) {
   // save PID parameter on Arduino EEPROM
   if (init_com == 0) return;
   serial.write('S');
   // println("Save PID");
 }
Exemplo n.º 13
0
 public void SET_ZERO(int theValue) {
   // set zero acc e gyro
   if (init_com == 0) return;
   serial.write('Z');
   println("Zet Zero");
 }
Exemplo n.º 14
0
  public void draw() {
    // String Serial.list()[];
    // size(600, 280);
    // background(80);

    fill(70);
    strokeWeight(0);
    stroke(35); // 75
    rect(0, 80, 600, 155, 0);

    fill(70);
    strokeWeight(0);
    stroke(35);
    rect(440, 80, 600, 155, 0);

    fill(75);
    strokeWeight(0);
    stroke(80);
    rect(0, 240, 600, 100, 0);

    fill(255);

    // Background
    // setGradient(0, 0, width, 80, c2, c1, Y_AXIS);

    textSize(12);
    text("Pitch P:", 35, 112);
    textSize(12);
    text("Pitch D:", 155, 112);

    textSize(12);
    text("Roll P:", 38, 162);
    textSize(12);
    text("Roll D:", 158, 162);

    textSize(12);
    text("Yaw P:", 38, 211);
    textSize(12);
    text("Yaw D:", 158, 211);

    textSize(12);
    text("Pitch Power:             %", 301, 112);
    textSize(12);
    text("Roll Power:             %", 307, 162);
    textSize(12);
    text("Yaw Power:             %", 305, 212);

    textSize(12);
    text("Roll Calibration (degr.)", 460, 190);

    textSize(16);
    // fill(0, 102, 153, 204);
    // text("Connected to:",400,45); text(Serial.list()[0],515,45);
    // text("Connected to:",400,45); text(Serial.list()[1],515,65);
    // text("Connected to:",400,45); text(Serial.list()[2],515,65);

    if (printlist == true) {
      for (int i = 0; i <= commListMax; i++) {
        text(Serial.list()[i], 515, 21 + i * 25);
      }
      printlist = false;
    }

    textSize(12);
    text(readStatus, 110, 263);

    if (PitchP.value() == 0) PitchP.setValue(0.01f);
    if (RollP.value() == 0) RollP.setValue(0.01f);
    if (YawP.value() == 0) YawP.setValue(0.01f);

    if (PitchD.value() == 0) PitchD.setValue(0.01f);
    if (RollD.value() == 0) RollD.setValue(0.01f);
    if (YawD.value() == 0) YawD.setValue(0.01f);

    if (PitchPWR.value() == 0) PitchPWR.setValue(1);
    if (RollPWR.value() == 0) RollPWR.setValue(1);
    if (YawPWR.value() == 0) YawPWR.setValue(1);

    // if(RollCal.value()==-9.9)RollCal.setValue(-9.9);

  }
 public void setPort(String portName) {
   if (portName != "") ;
   println(portName);
   if (serial != null) serial.stop();
   serial = new Serial(this, portName, 19200, 'O', 8, 1);
 }
Exemplo n.º 16
0
  public void WRITE() {
    if (writeEnable == false) return;

    myPort.write("h");

    myPort.write(PApplet.parseInt(PitchP.value() * 100));
    myPort.write(PApplet.parseInt(RollP.value() * 100));
    myPort.write(PApplet.parseInt(YawP.value() * 100));

    myPort.write(PApplet.parseInt(PitchD.value() * 100));
    myPort.write(PApplet.parseInt(RollD.value() * 100));
    myPort.write(PApplet.parseInt(YawD.value() * 100));

    myPort.write(PApplet.parseInt(PitchPWR.value()));
    myPort.write(PApplet.parseInt(RollPWR.value()));
    myPort.write(PApplet.parseInt(YawPWR.value()));

    myPort.write(RCcontrol);
    myPort.write(YawRCon);

    myPort.write(PApplet.parseInt(RollCal.value() * 10 + 100));
    // println (RollCal.value());
    // println (int (RollCal.value()*10+100));

    readStatus = "Write OK";
  }
Exemplo n.º 17
0
  public void READ() {
    if (readEnable == false) return;

    myPort.write("g"); // sends get values command

    while (i < 200000000) {
      i++;
    } // delay
    i = 0;

    // myPort.write("OK");
    readStatus = "Can't Read";

    if (myPort.read() == 'x') {
      PitchP.setValue(myPort.read() / 100.00f);
      RollP.setValue(myPort.read() / 100.00f);
      YawP.setValue(myPort.read() / 100.00f);

      PitchD.setValue(myPort.read() / 100.00f);
      RollD.setValue(myPort.read() / 100.00f);
      YawD.setValue(myPort.read() / 100.00f);

      PitchPWR.setValue(myPort.read());
      RollPWR.setValue(myPort.read());
      YawPWR.setValue(myPort.read());

      RCcontrol = PApplet.parseChar(myPort.read());
      YawRCon = PApplet.parseChar(myPort.read());

      RollCal.setValue((myPort.read() - 100.00f) / 10.00f);

      if (RCcontrol == '0') {
        buttonRCOff.setColorBackground(green_);
        buttonRCOn.setColorBackground(gray_);
      }
      if (RCcontrol == '1') {
        buttonRCOff.setColorBackground(gray_);
        buttonRCOn.setColorBackground(green_);
      }

      if (YawRCon == '0') {
        buttonYawRC.setColorBackground(gray_);
        buttonYawAut.setColorBackground(green_);
      }
      if (YawRCon == '1') {
        buttonYawRC.setColorBackground(green_);
        buttonYawAut.setColorBackground(gray_);
      }

      readStatus = "Read OK";
    }
  }
Exemplo n.º 18
0
  public void setup() {

    size(600, 280);
    background(75);

    textSize(28);
    fill(0, 120, 170); // blue
    text("EvvGC GUI", 20, 50);
    text(Version, 170, 50);

    // Define colors
    b1 = color(80);
    b2 = color(60);
    c1 = color(80, 80, 80);
    c2 = color(60, 61, 59);

    controlP5 = new ControlP5(this); // initialize the GUI controls

    // List all the available serial ports:
    println(Serial.list());

    commListMax = -1;
    for (int i = 0; i < Serial.list().length; i++) {
      commListMax = i;
    }

    /**
     * ****************************PID
     * cells************************************************************
     */
    PitchP = (controlP5.Numberbox) hideLabel(controlP5.addNumberbox("PitchP", 0, xPP, yPP, 40, 16));
    PitchP.setColorBackground(gray_);
    PitchP.setMin(0);
    PitchP.setDirection(Controller.HORIZONTAL);
    PitchP.setDecimalPrecision(2);
    PitchP.setMultiplier(0.01f);
    PitchP.setMax(2.54f);

    PitchD = (controlP5.Numberbox) hideLabel(controlP5.addNumberbox("PitchD", 0, xPD, yPD, 40, 16));
    PitchD.setColorBackground(gray_);
    PitchD.setMin(0);
    PitchD.setDirection(Controller.HORIZONTAL);
    PitchD.setDecimalPrecision(2);
    PitchD.setMultiplier(0.01f);
    PitchD.setMax(1);

    RollP = (controlP5.Numberbox) hideLabel(controlP5.addNumberbox("RollP", 0, xRP, yRP, 40, 16));
    RollP.setColorBackground(gray_);
    RollP.setMin(0);
    RollP.setDirection(Controller.HORIZONTAL);
    RollP.setDecimalPrecision(2);
    RollP.setMultiplier(0.01f);
    RollP.setMax(2.54f);

    RollD = (controlP5.Numberbox) hideLabel(controlP5.addNumberbox("RollD", 0, xRD, yRD, 40, 16));
    RollD.setColorBackground(gray_);
    RollD.setMin(0);
    RollD.setDirection(Controller.HORIZONTAL);
    RollD.setDecimalPrecision(2);
    RollD.setMultiplier(0.01f);
    RollD.setMax(1);

    YawP = (controlP5.Numberbox) hideLabel(controlP5.addNumberbox("YawP", 0, xYP, yYP, 40, 16));
    YawP.setColorBackground(gray_);
    YawP.setMin(0);
    YawP.setDirection(Controller.HORIZONTAL);
    YawP.setDecimalPrecision(2);
    YawP.setMultiplier(0.01f);
    YawP.setMax(2.54f);

    YawD = (controlP5.Numberbox) hideLabel(controlP5.addNumberbox("YawD", 0, xYD, yYD, 40, 16));
    YawD.setColorBackground(gray_);
    YawD.setMin(0);
    YawD.setDirection(Controller.HORIZONTAL);
    YawD.setDecimalPrecision(2);
    YawD.setMultiplier(0.01f);
    YawD.setMax(1);

    /**
     * ****************************Power
     * cells************************************************************
     */
    PitchPWR =
        (controlP5.Numberbox)
            hideLabel(controlP5.addNumberbox("PitchPWR", 0, xPPWR, yPPWR, 40, 16));
    PitchPWR.setColorBackground(gray_);
    PitchPWR.setMin(0);
    PitchPWR.setDirection(Controller.HORIZONTAL);
    PitchPWR.setDecimalPrecision(0);
    PitchPWR.setMultiplier(1);
    PitchPWR.setMax(100);

    RollPWR =
        (controlP5.Numberbox) hideLabel(controlP5.addNumberbox("RollPWR", 0, xRPWR, yRPWR, 40, 16));
    RollPWR.setColorBackground(gray_);
    RollPWR.setMin(0);
    RollPWR.setDirection(Controller.HORIZONTAL);
    RollPWR.setDecimalPrecision(0);
    RollPWR.setMultiplier(1);
    RollPWR.setMax(100);

    YawPWR =
        (controlP5.Numberbox) hideLabel(controlP5.addNumberbox("YawPWR", 0, xYPWR, yYPWR, 40, 16));
    YawPWR.setColorBackground(gray_);
    YawPWR.setMin(0);
    YawPWR.setDirection(Controller.HORIZONTAL);
    YawPWR.setDecimalPrecision(0);
    YawPWR.setMultiplier(1);
    YawPWR.setMax(100);

    /**
     * ****************************Calibration
     * cells************************************************************
     */
    RollCal =
        (controlP5.Numberbox)
            hideLabel(controlP5.addNumberbox("RollCal", 0, xRollCal, yRollCal, 40, 16));
    RollCal.setColorBackground(gray_);
    RollCal.setMin(-10);
    RollCal.setDirection(Controller.HORIZONTAL);
    RollCal.setDecimalPrecision(1);
    RollCal.setMultiplier(0.2f);
    RollCal.setMax(10);
    buttonZeroRoll = controlP5.addButton("ZERO", 1, 535, 200, 40, 16);
    buttonZeroRoll.setColorBackground(gray_);

    /**
     * **************************Buttons********************************************************************
     */
    buttonWRITE = controlP5.addButton("WRITE", 1, 500, 248, 60, 20);
    buttonWRITE.setColorBackground(gray_);
    buttonREAD = controlP5.addButton("READ", 1, 40, 248, 60, 20);
    buttonREAD.setColorBackground(gray_);

    buttonCONFon = controlP5.addButton("CONFIGon", 1, 400, 10, 60, 60);
    buttonCONFon.setColorBackground(gray_);
    buttonCONFoff = controlP5.addButton("CONFIGoff", 1, 330, 10, 60, 60);
    buttonCONFoff.setColorBackground(gray_);

    buttonP0 = controlP5.addButton("P0", 1, 490, 5, 20, 20);
    buttonP0.setColorBackground(gray_);
    buttonP1 = controlP5.addButton("P1", 1, 490, 30, 20, 20);
    buttonP1.setColorBackground(gray_);
    buttonP2 = controlP5.addButton("P2", 1, 490, 55, 20, 20);
    buttonP2.setColorBackground(gray_);

    buttonRCOff = controlP5.addButton("RC_OFF", 1, 450, 122, 65, 20);
    buttonRCOff.setColorBackground(gray_);
    buttonRCOn = controlP5.addButton("RC_ON", 1, 450, 100, 65, 20);
    buttonRCOn.setColorBackground(gray_);

    buttonYawRC = controlP5.addButton("Yaw_RC_Pan", 1, 520, 100, 75, 20);
    buttonYawRC.setColorBackground(gray_);
    buttonYawAut = controlP5.addButton("Yaw_Auto_Pan", 1, 520, 122, 75, 20);
    buttonYawAut.setColorBackground(gray_);

    buttonCONFoff.setColorBackground(green_);
  }
Exemplo n.º 19
0
  public void draw() {
    background(0);
    // Strings composition
    textFont(createFont("Arial bold", 24));
    fill(c_red);
    stroke(255);
    text("OpenWheels GUI V1.0", 20, 30);
    textSize(16);
    // textAlign(CENTER);
    fill(c_azure);
    text("Acc_RAW:  " + Acc_RAW, 150, 65);
    AccSlider.setValue(Acc_RAW);
    text("Gyro_RAW: " + Gyro_RAW, 150, 90);
    GyroSlider.setValue(Gyro_RAW);
    text("Acc_Angle:  " + Acc_Angle + "\u00b0", 150, 115);
    text("Gyro_Rate: " + Gyro_Rate + "\u00b0/sec", 150, 140);
    text("Drive: " + Drive, 150, 165);
    text("Steer: " + Steer, 150, 190);
    text("BatLevel: " + nf(BatLevel, 1, 1) + "V", 150, 215);
    text("Status: " + statusFlag, 150, 240);
    graphGauge();
    graphGrid();

    // call the function that plot the angular of acc, with few screen settings
    graphRoll(PApplet.parseFloat(Acc_Angle), VideoBuffer2, c_red); // xPos, YPos, YSpan
    // call the function that plot the estimate angular, with few screen settings
    graphRoll(Angle, VideoBuffer1, c_yellow); // xPos, YPos, YSpan

    // call arduino for data every timePolling [msec]
    int timePolling = 50; // 50msec=20Hz
    time1 = millis();
    if (init_com == 1) {
      while (serial.available() > 0) processSerialData();
      if ((time1 - time2) > timePolling) {
        if (requestPID == true) {
          serial.write('E');
          requestPID = false;
        } else if (writePID == true) {
          int P = PApplet.parseInt(conf_KP.value());
          int I = PApplet.parseInt(conf_KI.value());
          int D = PApplet.parseInt(conf_KD.value());
          char data[] = {0, 0, 0, 0, 0};
          data[0] = 'W';
          data[1] = PApplet.parseChar(P);
          data[2] = PApplet.parseChar(I);
          data[3] = PApplet.parseChar(D);
          data[4] = ' ';
          String str = new String(data);
          serial.write(str);
          println(str);
          // println(" P=" + P + " I=" + I + " D=" + D);
          writePID = false;
        }
        // else if (RUN==true ) {serial.write('A');}
        else {
          serial.write('A');
        }
        time2 = time1;
      }
    }
  }
Exemplo n.º 20
0
  public int[] getPacket() {
    boolean gotAPacket = false; // whether the first byte was 0x7E
    int packetLength = -1; // length of the dataArray
    int[] thisdataArray = null; // the dataArray to return
    int checksum = -1; // the checksum as received
    int localChecksum = -1; // the chacksum as we calculate it

    // read bytes until you get a 0x7E
    port.clear(); // flush the buffer so that no old data comes in

    while (port.available() < 1) {; // do nothing while we wait for the buffer to fill
    }

    // this is a good header. Get ready to read a packet
    if (port.read() == 0x7E) {
      gotAPacket = true;
    }

    // if the header byte is good, try the rest:
    if (gotAPacket) {
      // read two bytes
      while (port.available() < 2) {; // wait until you get two bytes of length
      }
      int lengthMSB = port.read(); // high byte for length of packet
      int lengthLSB = port.read(); // low byte for length of packet

      // convert to length value
      packetLength = (lengthLSB + (lengthMSB << 8));
      // sanity check: if the packet is too long, bail.
      if (packetLength > 200) {
        System.out.println("Length (" + packetLength + ") too long, discarding packet.");
        return null;
      }

      if (DEBUG) System.out.print("> [" + packetLength + "]: ");

      // read bytes until you've reached the length
      while (port.available() < packetLength) {; // do nothing while we wait for the buffer to fill
      }
      // make an array to hold the data frame:
      thisdataArray = new int[packetLength];

      // read all the bytes except the last one into the dataArray array:
      for (int thisByte = 0; thisByte < packetLength; thisByte++) {
        thisdataArray[thisByte] = port.read();
        if (DEBUG) System.out.print(parent.hex(thisdataArray[thisByte], 2) + " ");
      }

      while (port.available() < 1) {; // do nothing while we wait for the buffer to fill
      }
      // get the checksum:
      checksum = port.read();

      // calculate the checksum of the received dataArray:
      localChecksum = checkSum(thisdataArray);
      // if they're not the same, we have bad data:
      if (localChecksum != checksum) {
        if (DEBUG) System.out.println();
        System.out.print(
            "Bad checksum, discarding packet. Local: " + parent.hex(localChecksum % 256));
        System.out.println(", Remote: " + parent.hex(checksum) + ", Length: " + packetLength);
        // if the checksums don't add up, clear the dataArray array:
        thisdataArray = null;
      }
    }
    // makes a nice printing for debugging:
    if (DEBUG) System.out.println("!");

    // return the data frame.  If it's null, you got a bad packet.
    return thisdataArray;
  }