Serial port Razor 9DOF IMU-AHRS从未同步

Serial port Razor 9DOF IMU-AHRS从未同步,serial-port,arduino,processing,serial-communication,Serial Port,Arduino,Processing,Serial Communication,我目前正在处理草图 由于变量已同步,因此从未到达第181行 从来都不是真的。所以只有“连接到剃须刀…”出现,其他什么都没有。我已经打印了帧数,它一直在增加 是什么使同步标志变为真?我需要采取一些实际行动吗?我不熟悉Razor_AHRS,但首先你能检查一下你是否使用了正确的串行端口吗?该代码使用第一个串行端口,但根据您的配置/操作系统,可能需要对此进行更新。 (例如,在OSX上,这可能指向蓝牙端口) 尝试在Arduino中使用串行监视器,或以57600波特率连接到Razor串行端口,并尝试发送与草

我目前正在处理草图 由于变量已同步,因此从未到达第181行 从来都不是真的。所以只有“连接到剃须刀…”出现,其他什么都没有。我已经打印了帧数,它一直在增加


是什么使同步标志变为真?我需要采取一些实际行动吗?

我不熟悉Razor_AHRS,但首先你能检查一下你是否使用了正确的串行端口吗?该代码使用第一个串行端口,但根据您的配置/操作系统,可能需要对此进行更新。 (例如,在OSX上,这可能指向蓝牙端口)

尝试在Arduino中使用串行监视器,或以57600波特率连接到Razor串行端口,并尝试发送与草图相同的信息:

#ob
#o1
#oe0
#s00
发送最后一个数据包后,您应在串行监视器/冷却条件下接收此数据:

#SYNCH00\r\n
至少这是我从你链接到的源代码中收集到的

如果上述方法有效,则表示在处理过程中发送/接收串行数据的方式有问题。否则,这根本不是一个处理问题,需要后退一步,检查此设备上的其他资源,并再次检查电路是否正常

在工艺草图中,我可以看到一些可能出现错误的地方:

  • 此条件:
    if(frameCount==2)
  • 此条件:
    if(serial.available()
  • 理论上第一个条件应该可以工作,因为延迟调用,但是我建议使用一种基于delay()的方法,并使用一个单独的标志,而不是frameCount本身,以防万一。 同样,第二个条件也应该起作用,但是如果串行通信中由于某种原因出现延迟,并且您丢失了一个包含您期望的确切字节数的数据包,那么您将错过这里的同步标志提示。我建议每次缓冲一个字节,直到到达最后一个字节。幸运的是,您知道这是什么(
    \n
    ),有一个函数可以为您实现这一点:Serial's。 但在读取同步字符串后,需要取消此操作。 或者,您可以手动将字符附加到字符串,直到在查找同步时到达
    \n

    这是在我的头顶上,没有设备我无法正常测试,但我暗示了以下几点:

    /******************************************************************************************
    * Test Sketch for Razor AHRS v1.4.2
    * 9 Degree of Measurement Attitude and Heading Reference System
    * for Sparkfun "9DOF Razor IMU" and "9DOF Sensor Stick"
    *
    * Released under GNU GPL (General Public License) v3.0
    * Copyright (C) 2013 Peter Bartz [http://ptrbrtz.net]
    * Copyright (C) 2011-2012 Quality & Usability Lab, Deutsche Telekom Laboratories, TU Berlin
    * Written by Peter Bartz (peter-bartz@gmx.de)
    *
    * Infos, updates, bug reports, contributions and feedback:
    *     https://github.com/ptrbrtz/razor-9dof-ahrs
    ******************************************************************************************/
    
    /*
      NOTE: There seems to be a bug with the serial library in Processing versions 1.5
      and 1.5.1: "WARNING: RXTX Version mismatch ...".
      Processing 2.0.x seems to work just fine. Later versions may too.
      Alternatively, the older version 1.2.1 also works and is still available on the web.
    */
    
    import processing.opengl.*;
    import processing.serial.*;
    
    // IF THE SKETCH CRASHES OR HANGS ON STARTUP, MAKE SURE YOU ARE USING THE RIGHT SERIAL PORT:
    // 1. Have a look at the Processing console output of this sketch.
    // 2. Look for the serial port list and find the port you need (it's the same as in Arduino).
    // 3. Set your port number here:
    final static int SERIAL_PORT_NUM = 0;
    // 4. Try again.
    
    
    final static int SERIAL_PORT_BAUD_RATE = 57600;
    
    float yaw = 0.0f;
    float pitch = 0.0f;
    float roll = 0.0f;
    float yawOffset = 0.0f;
    
    PFont font;
    Serial serial;
    
    boolean synched = false;
    boolean isSetup = false;
    int now,wait = 3000;//wait 3s for Razor to bootup
    
    String serialString = ""; 
    int    serialDataLen= 12;
    int[]  serialBytes  = new int[serialDataLen];//4 bytes pair float value * 3 values
    int    byteCount    = 0;
    
    
    // Global setup
    void setup() {
      // Setup graphics
      size(640, 480, OPENGL);
      smooth();
      noStroke();
      frameRate(50);
    
      // Load font
    //  font = loadFont("Univers-66.vlw");
    //  textFont(font);
    
      // Setup serial port I/O
      println("AVAILABLE SERIAL PORTS:");
      println(Serial.list());
      String portName = Serial.list()[SERIAL_PORT_NUM];
      println();
      println("HAVE A LOOK AT THE LIST ABOVE AND SET THE RIGHT SERIAL PORT NUMBER IN THE CODE!");
      println("  -> Using port " + SERIAL_PORT_NUM + ": " + portName);
      try{
        serial = new Serial(this, portName, SERIAL_PORT_BAUD_RATE);
        now = millis();
      }catch(Exception e){
        System.err.println("Error opening serial port " + portName+"!\nPlease double check your port");
        e.printStackTrace();
        exit();
      }
    }
    
    void setupRazor() {
      println("Trying to setup and synch Razor...");
      if(millis() - now >= wait){
      // On Mac OSX and Linux (Windows too?) the board will do a reset when we connect, which is really bad.
      // See "Automatic (Software) Reset" on http://www.arduino.cc/en/Main/ArduinoBoardProMini
      // So we have to wait until the bootloader is finished and the Razor firmware can receive commands.
      // To prevent this, disconnect/cut/unplug the DTR line going to the board. This also has the advantage,
      // that the angles you receive are stable right from the beginning. 
    //  delay(3000);  // 3 seconds should be enough
    
      // Set Razor output parameters
      serial.write("#ob");  // Turn on binary output
      serial.write("#o1");  // Turn on continuous streaming output
      serial.write("#oe0"); // Disable error message output
    
      // Synch with Razor
      serial.clear();  // Clear input buffer up to here
      serial.write("#s00");  // Request synch token
      isSetup = true;
      }
    }
    
    float readFloat(int b0,int b1, int b2, int b3) {
      // Convert from little endian (Razor) to big endian (Java) and interpret as float
      return Float.intBitsToFloat(b0 + (b1 << 8) + (b2 << 16) + (b3 << 24));
    }
    
    void draw() {
       // Reset scene
      background(0);
      lights();
    
      // Sync with Razor 
      if (!synched) {
        textAlign(CENTER);
        fill(255);
        text("Connecting to Razor...", width/2, height/2, -200);
    
        if (!isSetup) setupRazor();  // Set ouput params and request synch token
        return;
      }
    
      // Draw board
      pushMatrix();
      translate(width/2, height/2, -350);
      drawBoard();
      popMatrix();
    
      textFont(font, 20);
      fill(255);
      textAlign(LEFT);
    
      // Output info text
      text("Point FTDI connector towards screen and press 'a' to align", 10, 25);
    
      // Output angles
      pushMatrix();
      translate(10, height - 10);
      textAlign(LEFT);
      text("Yaw: " + ((int) yaw), 0, 0);
      text("Pitch: " + ((int) pitch), 150, 0);
      text("Roll: " + ((int) roll), 300, 0);
      popMatrix();
    }
    void serialEvent(Serial p) {
      if(p.available() > 0){
        if(!synched){
          //wait for \n
          char c = p.readChar();
          serialString += c;
          if(c == '\n' && serialString.endsWith("#SYNCH00\r\n")) synched = true;
        }else{
          //synched, wait for 12 or more bytes
          int b = p.read();
          serialBytes[byteCount++] = b;
          if(byteCount >= serialDataLen){//we've got enough data, parse it
            yaw   = readFloat(serialBytes[0],serialBytes[1],serialBytes[2],serialBytes[3]);
            pitch = readFloat(serialBytes[4],serialBytes[5],serialBytes[6],serialBytes[7]);
            roll  = readFloat(serialBytes[8],serialBytes[9],serialBytes[10],serialBytes[11]);
            byteCount = 0;
          }
        }
      }
    }
    
    
    void keyPressed() {
      switch (key) {
        case '0':  // Turn Razor's continuous output stream off
          serial.write("#o0");
          break;
        case '1':  // Turn Razor's continuous output stream on
          serial.write("#o1");
          break;
        case 'f':  // Request one single yaw/pitch/roll frame from Razor (use when continuous streaming is off)
          serial.write("#f");
          break;
        case 'a':  // Align screen with Razor
          yawOffset = yaw;
      }
    }
    void drawArrow(float headWidthFactor, float headLengthFactor) {
      float headWidth = headWidthFactor * 200.0f;
      float headLength = headLengthFactor * 200.0f;
    
      pushMatrix();
    
      // Draw base
      translate(0, 0, -100);
      box(100, 100, 200);
    
      // Draw pointer
      translate(-headWidth/2, -50, -100);
      beginShape(QUAD_STRIP);
        vertex(0, 0 ,0);
        vertex(0, 100, 0);
        vertex(headWidth, 0 ,0);
        vertex(headWidth, 100, 0);
        vertex(headWidth/2, 0, -headLength);
        vertex(headWidth/2, 100, -headLength);
        vertex(0, 0 ,0);
        vertex(0, 100, 0);
      endShape();
      beginShape(TRIANGLES);
        vertex(0, 0, 0);
        vertex(headWidth, 0, 0);
        vertex(headWidth/2, 0, -headLength);
        vertex(0, 100, 0);
        vertex(headWidth, 100, 0);
        vertex(headWidth/2, 100, -headLength);
      endShape();
    
      popMatrix();
    }
    
    void drawBoard() {
      pushMatrix();
    
      rotateY(-radians(yaw - yawOffset));
      rotateX(-radians(pitch));
      rotateZ(radians(roll)); 
    
      // Board body
      fill(255, 0, 0);
      box(250, 20, 400);
    
      // Forward-arrow
      pushMatrix();
      translate(0, 0, -200);
      scale(0.5f, 0.2f, 0.25f);
      fill(0, 255, 0);
      drawArrow(1.0f, 2.0f);
      popMatrix();
    
      popMatrix();
    }
    
    /******************************************************************************************
    *Razor AHRS v1.4.2的测试草图
    *9测量角度姿态和航向参考系统
    *用于Sparkfun“9DOF剃须刀IMU”和“9DOF传感器棒”
    *
    *根据GNU GPL(通用公共许可证)v3.0发布
    *版权所有(C)2013彼得·巴茨[http://ptrbrtz.net]
    *版权所有(C)2011-2012质量和可用性实验室,德国电信实验室,TU Berlin
    *作者:彼得·巴茨(彼得·巴茨)-bartz@gmx.de)
    *
    *信息、更新、错误报告、贡献和反馈:
    *     https://github.com/ptrbrtz/razor-9dof-ahrs
    ******************************************************************************************/
    /*
    注意:在处理版本1.5时,串行库似乎有一个bug
    和1.5.1:“警告:RXTX版本不匹配…”。
    处理2.0.x似乎工作得很好。以后的版本也可能会。
    或者,旧版本1.2.1也可以使用,并且仍然可以在web上使用。
    */
    导入处理;
    输入处理。串行。*;
    //如果草图在启动时崩溃或挂起,请确保使用正确的串行端口:
    // 1. 查看此草图的处理控制台输出。
    // 2. 查找串行端口列表并找到所需的端口(与Arduino中的端口相同)。
    // 3. 在此处设置端口号:
    最终静态int串行端口数=0;
    // 4. 再试一次。
    最终静态int串行端口波特率=57600;
    浮动偏航=0.0f;
    浮动节距=0.0f;
    浮动辊=0.0f;
    浮动yawOffset=0.0f;
    字体;
    连载;
    布尔同步=假;
    布尔值isSetup=false;
    现在int,等待=3000//等待3秒钟,等待Razor启动
    字符串serialString=“”;
    int serialDataLen=12;
    int[]serialBytes=新的int[serialDataLen]//4字节对浮点值*3个值
    int字节数=0;
    //全局设置
    无效设置(){
    //设置图形
    尺寸(640480,OPENGL);
    光滑的();
    仰泳();
    帧率(50);
    //加载字体
    //font=loadFont(“univ-66.vlw”);
    //文本字体(字体);
    //设置串行端口I/O
    println(“可用串行端口:”);
    println(Serial.list());
    字符串portName=Serial.list()[Serial_PORT_NUM];
    println();
    println(“查看上面的列表并在代码中设置正确的串行端口号!”);
    println(“->使用端口”+串行端口数+”:“+端口名);
    试一试{
    串行=新串行(此端口名、串行端口波特率);
    现在=毫秒();
    }捕获(例外e){
    System.err.println(“打开串行端口时出错”+portName+”!\n请仔细检查您的端口”);
    e、 printStackTrace();
    退出();
    }
    }
    void setupRazor(){
    println(“尝试设置和同步Razor…”);
    如果(毫秒()-现在>=等待){
    //在Mac OSX和Linux(也是Windows?)上,当我们连接时,电路板会重置,这真的很糟糕。
    //请参阅上的“自动(软件)重置”http://www.arduino.cc/en/Main/ArduinoBoardProMini
    //因此,我们必须等待引导加载程序完成,Razor固件可以接收命令。
    //为防止出现这种情况,请断开/切断/拔下连接至电路板的DTR线路。这还有一个优点,
    //你接收到的角度从一开始就是稳定的。
    //延迟(3000);//3秒应该足够了
    //设置剃须刀输出参数
    serial.write(“#ob”);//打开二进制输出
    serial.write(“#o1”);//打开连续流输出
    serial.write(“#oe0”);//禁用错误消息输出
    //与剃刀同步
    serial.clear();//清除输入缓冲区到这里
    serial.write(“#s00”);//请求