{
  "format": "evn-blocks",
  "version": 3,
  "title": "IMU: every block",
  "needs": "an EVN IMU module on I2C port 1, fixed flat to the robot; a table; you move the robot when the program asks",
  "description": "Every IMU block once: wait for the gyro to settle, calibrate on a level surface (one pose), calibrate again on any flat surface with half a turn (120 to 240 degrees) in between (two poses), then read heading, pitch and roll and which side faces up. Each finished calibration REPLACES the one stored on the board for port 1; the last one made is the one kept. If the robot moves during a pose, the program stops with \"IMU calibration failed\" and the stored calibration is unchanged: run it again. Without the half turn within 110 s the first pose is cancelled and nothing is stored. The program waits for the user button at each step; to leave, press Stop in the editor (or Ctrl-C).",
  "workspace": {
    "blocks": {
      "languageVersion": 0,
      "blocks": [
        {
          "type": "evn_setup_start",
          "x": 30,
          "y": 30,
          "next": {
            "block": {
              "type": "evn_imu_setup",
              "fields": {
                "PORT": "1",
                "CALIBRATE": "FALSE"
              }
            }
          }
        },
        {
          "type": "evn_program_start",
          "x": 30,
          "y": 115,
          "next": {
            "block": {
              "type": "evn_comment",
              "fields": {
                "TEXT": "the calibration stored for the port is applied from the start; ready about a second after the robot is still (10 to 27 s without a stored calibration)"
              },
              "next": {
                "block": {
                  "type": "text_print",
                  "inputs": {
                    "TEXT": {
                      "shadow": {
                        "type": "text",
                        "fields": {
                          "TEXT": "Keep the robot still ..."
                        }
                      }
                    }
                  },
                  "next": {
                    "block": {
                      "type": "evn_wait_until",
                      "inputs": {
                        "COND": {
                          "block": {
                            "type": "evn_imu_stationary",
                            "fields": {
                              "PORT": "1",
                              "WHAT": "ready()"
                            }
                          }
                        }
                      },
                      "next": {
                        "block": {
                          "type": "text_print",
                          "inputs": {
                            "TEXT": {
                              "shadow": {
                                "type": "text",
                                "fields": {
                                  "TEXT": "gyro settled"
                                }
                              }
                            }
                          },
                          "next": {
                            "block": {
                              "type": "evn_comment",
                              "fields": {
                                "TEXT": "one pose: about 2 s, still on a level surface"
                              },
                              "next": {
                                "block": {
                                  "type": "text_print",
                                  "inputs": {
                                    "TEXT": {
                                      "shadow": {
                                        "type": "text",
                                        "fields": {
                                          "TEXT": "Put the robot on a level table, then press the button and let go."
                                        }
                                      }
                                    }
                                  },
                                  "next": {
                                    "block": {
                                      "type": "evn_wait_for_button",
                                      "next": {
                                        "block": {
                                          "type": "evn_wait_until",
                                          "inputs": {
                                            "COND": {
                                              "block": {
                                                "type": "evn_imu_stationary",
                                                "fields": {
                                                  "PORT": "1",
                                                  "WHAT": "stationary()"
                                                }
                                              }
                                            }
                                          },
                                          "next": {
                                            "block": {
                                              "type": "evn_imu_calibrate",
                                              "fields": {
                                                "PORT": "1",
                                                "POSE": "0"
                                              },
                                              "next": {
                                                "block": {
                                                  "type": "text_print",
                                                  "inputs": {
                                                    "TEXT": {
                                                      "shadow": {
                                                        "type": "text",
                                                        "fields": {
                                                          "TEXT": "one-pose calibration stored"
                                                        }
                                                      }
                                                    }
                                                  },
                                                  "next": {
                                                    "block": {
                                                      "type": "evn_comment",
                                                      "fields": {
                                                        "TEXT": "two poses: the same spot, half a turn in between; a slope up to 20 degrees is left out"
                                                      },
                                                      "next": {
                                                        "block": {
                                                          "type": "text_print",
                                                          "inputs": {
                                                            "TEXT": {
                                                              "shadow": {
                                                                "type": "text",
                                                                "fields": {
                                                                  "TEXT": "Put the robot on a flat surface, then press the button and let go."
                                                                }
                                                              }
                                                            }
                                                          },
                                                          "next": {
                                                            "block": {
                                                              "type": "evn_wait_for_button",
                                                              "next": {
                                                                "block": {
                                                                  "type": "evn_wait_until",
                                                                  "inputs": {
                                                                    "COND": {
                                                                      "block": {
                                                                        "type": "evn_imu_stationary",
                                                                        "fields": {
                                                                          "PORT": "1",
                                                                          "WHAT": "stationary()"
                                                                        }
                                                                      }
                                                                    }
                                                                  },
                                                                  "next": {
                                                                    "block": {
                                                                      "type": "evn_imu_calibrate",
                                                                      "fields": {
                                                                        "PORT": "1",
                                                                        "POSE": "1"
                                                                      },
                                                                      "next": {
                                                                        "block": {
                                                                          "type": "evn_imu_reset_heading",
                                                                          "fields": {
                                                                            "PORT": "1"
                                                                          },
                                                                          "inputs": {
                                                                            "ANGLE": {
                                                                              "shadow": {
                                                                                "type": "math_number",
                                                                                "fields": {
                                                                                  "NUM": 0
                                                                                }
                                                                              }
                                                                            }
                                                                          },
                                                                          "next": {
                                                                            "block": {
                                                                              "type": "text_print",
                                                                              "inputs": {
                                                                                "TEXT": {
                                                                                  "shadow": {
                                                                                    "type": "text",
                                                                                    "fields": {
                                                                                      "TEXT": "Turn the robot about half a turn (120 to 240 degrees) on the spot on its wheels, then let go."
                                                                                    }
                                                                                  }
                                                                                }
                                                                              },
                                                                              "next": {
                                                                                "block": {
                                                                                  "type": "evn_stopwatch_reset",
                                                                                  "fields": {
                                                                                    "ACTION": "reset"
                                                                                  },
                                                                                  "next": {
                                                                                    "block": {
                                                                                      "type": "evn_comment",
                                                                                      "fields": {
                                                                                        "TEXT": "wait for the half turn (the driver takes 120 to 240 degrees) with the robot still again, at most 110 s"
                                                                                      },
                                                                                      "next": {
                                                                                        "block": {
                                                                                          "type": "evn_wait_until",
                                                                                          "inputs": {
                                                                                            "COND": {
                                                                                              "block": {
                                                                                                "type": "logic_operation",
                                                                                                "fields": {
                                                                                                  "OP": "OR"
                                                                                                },
                                                                                                "inputs": {
                                                                                                  "A": {
                                                                                                    "block": {
                                                                                                      "type": "logic_operation",
                                                                                                      "fields": {
                                                                                                        "OP": "AND"
                                                                                                      },
                                                                                                      "inputs": {
                                                                                                        "A": {
                                                                                                          "block": {
                                                                                                            "type": "logic_operation",
                                                                                                            "fields": {
                                                                                                              "OP": "AND"
                                                                                                            },
                                                                                                            "inputs": {
                                                                                                              "A": {
                                                                                                                "block": {
                                                                                                                  "type": "logic_compare",
                                                                                                                  "fields": {
                                                                                                                    "OP": "GT"
                                                                                                                  },
                                                                                                                  "inputs": {
                                                                                                                    "A": {
                                                                                                                      "block": {
                                                                                                                        "type": "math_single",
                                                                                                                        "fields": {
                                                                                                                          "OP": "ABS"
                                                                                                                        },
                                                                                                                        "inputs": {
                                                                                                                          "NUM": {
                                                                                                                            "block": {
                                                                                                                              "type": "evn_imu_angle",
                                                                                                                              "fields": {
                                                                                                                                "PORT": "1",
                                                                                                                                "WHAT": "heading()"
                                                                                                                              }
                                                                                                                            }
                                                                                                                          }
                                                                                                                        }
                                                                                                                      }
                                                                                                                    },
                                                                                                                    "B": {
                                                                                                                      "shadow": {
                                                                                                                        "type": "math_number",
                                                                                                                        "fields": {
                                                                                                                          "NUM": 120
                                                                                                                        }
                                                                                                                      }
                                                                                                                    }
                                                                                                                  }
                                                                                                                }
                                                                                                              },
                                                                                                              "B": {
                                                                                                                "block": {
                                                                                                                  "type": "logic_compare",
                                                                                                                  "fields": {
                                                                                                                    "OP": "LT"
                                                                                                                  },
                                                                                                                  "inputs": {
                                                                                                                    "A": {
                                                                                                                      "block": {
                                                                                                                        "type": "math_single",
                                                                                                                        "fields": {
                                                                                                                          "OP": "ABS"
                                                                                                                        },
                                                                                                                        "inputs": {
                                                                                                                          "NUM": {
                                                                                                                            "block": {
                                                                                                                              "type": "evn_imu_angle",
                                                                                                                              "fields": {
                                                                                                                                "PORT": "1",
                                                                                                                                "WHAT": "heading()"
                                                                                                                              }
                                                                                                                            }
                                                                                                                          }
                                                                                                                        }
                                                                                                                      }
                                                                                                                    },
                                                                                                                    "B": {
                                                                                                                      "shadow": {
                                                                                                                        "type": "math_number",
                                                                                                                        "fields": {
                                                                                                                          "NUM": 240
                                                                                                                        }
                                                                                                                      }
                                                                                                                    }
                                                                                                                  }
                                                                                                                }
                                                                                                              }
                                                                                                            }
                                                                                                          }
                                                                                                        },
                                                                                                        "B": {
                                                                                                          "block": {
                                                                                                            "type": "evn_imu_stationary",
                                                                                                            "fields": {
                                                                                                              "PORT": "1",
                                                                                                              "WHAT": "stationary()"
                                                                                                            }
                                                                                                          }
                                                                                                        }
                                                                                                      }
                                                                                                    }
                                                                                                  },
                                                                                                  "B": {
                                                                                                    "block": {
                                                                                                      "type": "logic_compare",
                                                                                                      "fields": {
                                                                                                        "OP": "GT"
                                                                                                      },
                                                                                                      "inputs": {
                                                                                                        "A": {
                                                                                                          "block": {
                                                                                                            "type": "evn_stopwatch_time"
                                                                                                          }
                                                                                                        },
                                                                                                        "B": {
                                                                                                          "shadow": {
                                                                                                            "type": "math_number",
                                                                                                            "fields": {
                                                                                                              "NUM": 110000
                                                                                                            }
                                                                                                          }
                                                                                                        }
                                                                                                      }
                                                                                                    }
                                                                                                  }
                                                                                                }
                                                                                              }
                                                                                            }
                                                                                          },
                                                                                          "next": {
                                                                                            "block": {
                                                                                              "type": "controls_if",
                                                                                              "inputs": {
                                                                                                "IF0": {
                                                                                                  "block": {
                                                                                                    "type": "logic_operation",
                                                                                                    "fields": {
                                                                                                      "OP": "AND"
                                                                                                    },
                                                                                                    "inputs": {
                                                                                                      "A": {
                                                                                                        "block": {
                                                                                                          "type": "logic_operation",
                                                                                                          "fields": {
                                                                                                            "OP": "AND"
                                                                                                          },
                                                                                                          "inputs": {
                                                                                                            "A": {
                                                                                                              "block": {
                                                                                                                "type": "logic_compare",
                                                                                                                "fields": {
                                                                                                                  "OP": "GT"
                                                                                                                },
                                                                                                                "inputs": {
                                                                                                                  "A": {
                                                                                                                    "block": {
                                                                                                                      "type": "math_single",
                                                                                                                      "fields": {
                                                                                                                        "OP": "ABS"
                                                                                                                      },
                                                                                                                      "inputs": {
                                                                                                                        "NUM": {
                                                                                                                          "block": {
                                                                                                                            "type": "evn_imu_angle",
                                                                                                                            "fields": {
                                                                                                                              "PORT": "1",
                                                                                                                              "WHAT": "heading()"
                                                                                                                            }
                                                                                                                          }
                                                                                                                        }
                                                                                                                      }
                                                                                                                    }
                                                                                                                  },
                                                                                                                  "B": {
                                                                                                                    "shadow": {
                                                                                                                      "type": "math_number",
                                                                                                                      "fields": {
                                                                                                                        "NUM": 120
                                                                                                                      }
                                                                                                                    }
                                                                                                                  }
                                                                                                                }
                                                                                                              }
                                                                                                            },
                                                                                                            "B": {
                                                                                                              "block": {
                                                                                                                "type": "logic_compare",
                                                                                                                "fields": {
                                                                                                                  "OP": "LT"
                                                                                                                },
                                                                                                                "inputs": {
                                                                                                                  "A": {
                                                                                                                    "block": {
                                                                                                                      "type": "math_single",
                                                                                                                      "fields": {
                                                                                                                        "OP": "ABS"
                                                                                                                      },
                                                                                                                      "inputs": {
                                                                                                                        "NUM": {
                                                                                                                          "block": {
                                                                                                                            "type": "evn_imu_angle",
                                                                                                                            "fields": {
                                                                                                                              "PORT": "1",
                                                                                                                              "WHAT": "heading()"
                                                                                                                            }
                                                                                                                          }
                                                                                                                        }
                                                                                                                      }
                                                                                                                    }
                                                                                                                  },
                                                                                                                  "B": {
                                                                                                                    "shadow": {
                                                                                                                      "type": "math_number",
                                                                                                                      "fields": {
                                                                                                                        "NUM": 240
                                                                                                                      }
                                                                                                                    }
                                                                                                                  }
                                                                                                                }
                                                                                                              }
                                                                                                            }
                                                                                                          }
                                                                                                        }
                                                                                                      },
                                                                                                      "B": {
                                                                                                        "block": {
                                                                                                          "type": "evn_imu_stationary",
                                                                                                          "fields": {
                                                                                                            "PORT": "1",
                                                                                                            "WHAT": "stationary()"
                                                                                                          }
                                                                                                        }
                                                                                                      }
                                                                                                    }
                                                                                                  }
                                                                                                },
                                                                                                "DO0": {
                                                                                                  "block": {
                                                                                                    "type": "evn_imu_calibrate",
                                                                                                    "fields": {
                                                                                                      "PORT": "1",
                                                                                                      "POSE": "2"
                                                                                                    },
                                                                                                    "next": {
                                                                                                      "block": {
                                                                                                        "type": "text_print",
                                                                                                        "inputs": {
                                                                                                          "TEXT": {
                                                                                                            "shadow": {
                                                                                                              "type": "text",
                                                                                                              "fields": {
                                                                                                                "TEXT": "two-pose calibration stored"
                                                                                                              }
                                                                                                            }
                                                                                                          }
                                                                                                        }
                                                                                                      }
                                                                                                    }
                                                                                                  }
                                                                                                },
                                                                                                "ELSE": {
                                                                                                  "block": {
                                                                                                    "type": "evn_python",
                                                                                                    "fields": {
                                                                                                      "CODE": "imu_1.cancel_calibration()"
                                                                                                    },
                                                                                                    "next": {
                                                                                                      "block": {
                                                                                                        "type": "text_print",
                                                                                                        "inputs": {
                                                                                                          "TEXT": {
                                                                                                            "shadow": {
                                                                                                              "type": "text",
                                                                                                              "fields": {
                                                                                                                "TEXT": "no half turn seen: cancelled, the stored calibration stays"
                                                                                                              }
                                                                                                            }
                                                                                                          }
                                                                                                        }
                                                                                                      }
                                                                                                    }
                                                                                                  }
                                                                                                }
                                                                                              },
                                                                                              "extraState": {
                                                                                                "hasElse": true
                                                                                              },
                                                                                              "next": {
                                                                                                "block": {
                                                                                                  "type": "evn_comment",
                                                                                                  "fields": {
                                                                                                    "TEXT": "what the IMU reads: tilt the robot and turn it"
                                                                                                  },
                                                                                                  "next": {
                                                                                                    "block": {
                                                                                                      "type": "evn_imu_reset_heading",
                                                                                                      "fields": {
                                                                                                        "PORT": "1"
                                                                                                      },
                                                                                                      "inputs": {
                                                                                                        "ANGLE": {
                                                                                                          "shadow": {
                                                                                                            "type": "math_number",
                                                                                                            "fields": {
                                                                                                              "NUM": 0
                                                                                                            }
                                                                                                          }
                                                                                                        }
                                                                                                      },
                                                                                                      "next": {
                                                                                                        "block": {
                                                                                                          "type": "text_print",
                                                                                                          "inputs": {
                                                                                                            "TEXT": {
                                                                                                              "shadow": {
                                                                                                                "type": "text",
                                                                                                                "fields": {
                                                                                                                  "TEXT": "Tilt the robot and turn it for ten seconds."
                                                                                                                }
                                                                                                              }
                                                                                                            }
                                                                                                          },
                                                                                                          "next": {
                                                                                                            "block": {
                                                                                                              "type": "controls_repeat_ext",
                                                                                                              "inputs": {
                                                                                                                "TIMES": {
                                                                                                                  "shadow": {
                                                                                                                    "type": "math_number",
                                                                                                                    "fields": {
                                                                                                                      "NUM": 20
                                                                                                                    }
                                                                                                                  }
                                                                                                                },
                                                                                                                "DO": {
                                                                                                                  "block": {
                                                                                                                    "type": "text_print",
                                                                                                                    "inputs": {
                                                                                                                      "TEXT": {
                                                                                                                        "block": {
                                                                                                                          "type": "text_join",
                                                                                                                          "inputs": {
                                                                                                                            "ADD0": {
                                                                                                                              "shadow": {
                                                                                                                                "type": "text",
                                                                                                                                "fields": {
                                                                                                                                  "TEXT": "heading "
                                                                                                                                }
                                                                                                                              }
                                                                                                                            },
                                                                                                                            "ADD1": {
                                                                                                                              "block": {
                                                                                                                                "type": "evn_imu_angle",
                                                                                                                                "fields": {
                                                                                                                                  "PORT": "1",
                                                                                                                                  "WHAT": "heading()"
                                                                                                                                }
                                                                                                                              }
                                                                                                                            }
                                                                                                                          },
                                                                                                                          "extraState": {
                                                                                                                            "itemCount": 2
                                                                                                                          }
                                                                                                                        }
                                                                                                                      }
                                                                                                                    },
                                                                                                                    "next": {
                                                                                                                      "block": {
                                                                                                                        "type": "text_print",
                                                                                                                        "inputs": {
                                                                                                                          "TEXT": {
                                                                                                                            "block": {
                                                                                                                              "type": "text_join",
                                                                                                                              "inputs": {
                                                                                                                                "ADD0": {
                                                                                                                                  "shadow": {
                                                                                                                                    "type": "text",
                                                                                                                                    "fields": {
                                                                                                                                      "TEXT": "pitch "
                                                                                                                                    }
                                                                                                                                  }
                                                                                                                                },
                                                                                                                                "ADD1": {
                                                                                                                                  "block": {
                                                                                                                                    "type": "evn_imu_angle",
                                                                                                                                    "fields": {
                                                                                                                                      "PORT": "1",
                                                                                                                                      "WHAT": "tilt()[0]"
                                                                                                                                    }
                                                                                                                                  }
                                                                                                                                }
                                                                                                                              },
                                                                                                                              "extraState": {
                                                                                                                                "itemCount": 2
                                                                                                                              }
                                                                                                                            }
                                                                                                                          }
                                                                                                                        },
                                                                                                                        "next": {
                                                                                                                          "block": {
                                                                                                                            "type": "text_print",
                                                                                                                            "inputs": {
                                                                                                                              "TEXT": {
                                                                                                                                "block": {
                                                                                                                                  "type": "text_join",
                                                                                                                                  "inputs": {
                                                                                                                                    "ADD0": {
                                                                                                                                      "shadow": {
                                                                                                                                        "type": "text",
                                                                                                                                        "fields": {
                                                                                                                                          "TEXT": "roll "
                                                                                                                                        }
                                                                                                                                      }
                                                                                                                                    },
                                                                                                                                    "ADD1": {
                                                                                                                                      "block": {
                                                                                                                                        "type": "evn_imu_angle",
                                                                                                                                        "fields": {
                                                                                                                                          "PORT": "1",
                                                                                                                                          "WHAT": "tilt()[1]"
                                                                                                                                        }
                                                                                                                                      }
                                                                                                                                    }
                                                                                                                                  },
                                                                                                                                  "extraState": {
                                                                                                                                    "itemCount": 2
                                                                                                                                  }
                                                                                                                                }
                                                                                                                              }
                                                                                                                            },
                                                                                                                            "next": {
                                                                                                                              "block": {
                                                                                                                                "type": "controls_if",
                                                                                                                                "inputs": {
                                                                                                                                  "IF0": {
                                                                                                                                    "block": {
                                                                                                                                      "type": "evn_imu_up",
                                                                                                                                      "fields": {
                                                                                                                                        "PORT": "1",
                                                                                                                                        "SIDE": "TOP"
                                                                                                                                      }
                                                                                                                                    }
                                                                                                                                  },
                                                                                                                                  "DO0": {
                                                                                                                                    "block": {
                                                                                                                                      "type": "text_print",
                                                                                                                                      "inputs": {
                                                                                                                                        "TEXT": {
                                                                                                                                          "shadow": {
                                                                                                                                            "type": "text",
                                                                                                                                            "fields": {
                                                                                                                                              "TEXT": "top side up"
                                                                                                                                            }
                                                                                                                                          }
                                                                                                                                        }
                                                                                                                                      }
                                                                                                                                    }
                                                                                                                                  },
                                                                                                                                  "ELSE": {
                                                                                                                                    "block": {
                                                                                                                                      "type": "text_print",
                                                                                                                                      "inputs": {
                                                                                                                                        "TEXT": {
                                                                                                                                          "shadow": {
                                                                                                                                            "type": "text",
                                                                                                                                            "fields": {
                                                                                                                                              "TEXT": "on its side or upside down"
                                                                                                                                            }
                                                                                                                                          }
                                                                                                                                        }
                                                                                                                                      }
                                                                                                                                    }
                                                                                                                                  }
                                                                                                                                },
                                                                                                                                "extraState": {
                                                                                                                                  "hasElse": true
                                                                                                                                },
                                                                                                                                "next": {
                                                                                                                                  "block": {
                                                                                                                                    "type": "evn_wait",
                                                                                                                                    "inputs": {
                                                                                                                                      "TIME": {
                                                                                                                                        "shadow": {
                                                                                                                                          "type": "math_number",
                                                                                                                                          "fields": {
                                                                                                                                            "NUM": 500
                                                                                                                                          }
                                                                                                                                        }
                                                                                                                                      }
                                                                                                                                    }
                                                                                                                                  }
                                                                                                                                }
                                                                                                                              }
                                                                                                                            }
                                                                                                                          }
                                                                                                                        }
                                                                                                                      }
                                                                                                                    }
                                                                                                                  }
                                                                                                                }
                                                                                                              }
                                                                                                            }
                                                                                                          }
                                                                                                        }
                                                                                                      }
                                                                                                    }
                                                                                                  }
                                                                                                }
                                                                                              }
                                                                                            }
                                                                                          }
                                                                                        }
                                                                                      }
                                                                                    }
                                                                                  }
                                                                                }
                                                                              }
                                                                            }
                                                                          }
                                                                        }
                                                                      }
                                                                    }
                                                                  }
                                                                }
                                                              }
                                                            }
                                                          }
                                                        }
                                                      }
                                                    }
                                                  }
                                                }
                                              }
                                            }
                                          }
                                        }
                                      }
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}
