Merge "Deploy IMU driver and configure IRQs"
diff --git a/aos/starter/starter.sh b/aos/starter/starter.sh
index d5812c2..ff210dc 100755
--- a/aos/starter/starter.sh
+++ b/aos/starter/starter.sh
@@ -1,62 +1,27 @@
 #!/bin/bash
 
+set -x
+
 if [[ "$(hostname)" == "roboRIO"* ]]; then
   /usr/local/natinst/etc/init.d/systemWebServer stop
 
-  ROBOT_CODE="/home/admin/robot_code"
+  ROBOT_CODE="/home/admin/bin"
 
   # Get the CTRE libraries in the shared library search path
   for f in $(ls *.so);
   do
-    ln -f -s /home/admin/robot_code/$f /usr/local/frc/third-party/lib/$f
+    ln -f -s /home/admin/bin/$f /usr/local/frc/third-party/lib/$f
   done
 
   ln -s /var/local/natinst/log/FRC_UserProgram.log /tmp/FRC_UserProgram.log
   ln -s /var/local/natinst/log/FRC_UserProgram.log "${ROBOT_CODE}/FRC_UserProgram.log"
 elif [[ "$(hostname)" == "pi-"* ]]; then
-  function chrtirq() {
-    ps -ef | grep "\\[$1\\]" | awk '{print $2}' | xargs chrt $2 -p $3
-  }
-
-  chrtirq "irq/20-fe00b880" -f 50
-  chrtirq "irq/66-xhci_hcd" -f 1
-  chrtirq "irq/50-VCHIQ do" -o 0
-  chrtirq "irq/27-DMA IRQ" -f 50
-  chrtirq "irq/51-mmc1" -o 0
-  chrtirq "irq/51-mmc0" -o 0
-  chrtirq "irq/51-s-mmc0" -o 0
-  chrtirq "irq/64-v3d" -o 0
-  chrtirq "irq/24-vc4 hvs" -o 0
-  chrtirq "irq/42-vc4 hdmi" -o 0
-  chrtirq "irq/43-vc4 hdmi" -o 0
-  chrtirq "irq/39-vc4 hdmi" -o 0
-  chrtirq "irq/39-s-vc4 hd" -o 0
-  chrtirq "irq/38-vc4 hdmi" -o 0
-  chrtirq "irq/38-s-vc4 hd" -o 0
-  chrtirq "irq/29-DMA IRQ" -f 50
-  chrtirq "irq/48-vc4 hdmi" -o 0
-  chrtirq "irq/49-vc4 hdmi" -o 0
-  chrtirq "irq/45-vc4 hdmi" -o 0
-  chrtirq "irq/45-s-vc4 hd" -o 0
-  chrtirq "irq/44-vc4 hdmi" -o 0
-  chrtirq "irq/44-s-vc4 hd" -o 0
-  chrtirq "irq/30-DMA IRQ" -f 50
-  chrtirq "irq/19-fe004000" -f 50
-  chrtirq "irq/34-vc4 crtc" -o 0
-  chrtirq "irq/35-vc4 crtc" -o 0
-  chrtirq "irq/36-vc4 crtc" -o 0
-  chrtirq "irq/35-vc4 crtc" -o 0
-  chrtirq "irq/37-vc4 crtc" -o 0
-  chrtirq "irq/23-uart-pl0" -o 0
-  chrtirq "irq/57-eth0" -f 10
-  chrtirq "irq/58-eth0" -f 10
-
   # We have systemd configured to handle restarting, so just exec.
-  export PATH="${PATH}:/home/pi/robot_code"
+  export PATH="${PATH}:/home/pi/bin"
   rm -rf /dev/shm/aos
   exec starterd
 else
-  ROBOT_CODE="${HOME}/robot_code"
+  ROBOT_CODE="${HOME}/bin"
 fi
 
 cd "${ROBOT_CODE}"
diff --git a/frc971/config/robotCommand b/frc971/config/robotCommand
index 7b726a7..cb1d6f1 100755
--- a/frc971/config/robotCommand
+++ b/frc971/config/robotCommand
@@ -1 +1 @@
-/home/admin/robot_code/starter.sh
+/home/admin/bin/starter.sh
diff --git a/frc971/config/setup_roborio.sh b/frc971/config/setup_roborio.sh
index f00212f..3434482 100755
--- a/frc971/config/setup_roborio.sh
+++ b/frc971/config/setup_roborio.sh
@@ -30,11 +30,11 @@
   ssh "admin@${ROBOT_HOSTNAME}" 'echo "alias l=\"ls -la\"" >> /etc/profile'
   echo "Adding symbolic link to loging directory"
   ssh "admin@${ROBOT_HOSTNAME}" ln -s /media/sda1 logs
-  ssh "admin@${ROBOT_HOSTNAME}" mkdir robot_code
-  ssh "admin@${ROBOT_HOSTNAME}" ln -s /media/sda1/aos_log-current robot_code/aos_log-current
+  ssh "admin@${ROBOT_HOSTNAME}" mkdir bin
+  ssh "admin@${ROBOT_HOSTNAME}" ln -s /media/sda1/aos_log-current bin/aos_log-current
   echo "Adding aos_dump autocomplete to profile"
-  ssh "admin@${ROBOT_HOSTNAME}" 'echo "if [ -f /home/admin/robot_code/aos_dump_autocomplete.sh ]; then source /home/admin/robot_code/aos_dump_autocomplete.sh; fi;" >> /etc/profile'
-  ssh "admin@${ROBOT_HOSTNAME}" 'echo "export PATH=\"\${PATH}:/home/admin/robot_code:/home/admin/bin\"" >> /etc/profile'
+  ssh "admin@${ROBOT_HOSTNAME}" 'echo "if [ -f /home/admin/bin/aos_dump_autocomplete.sh ]; then source /home/admin/bin/aos_dump_autocomplete.sh; fi;" >> /etc/profile'
+  ssh "admin@${ROBOT_HOSTNAME}" 'echo "export PATH=\"\${PATH}:/home/admin/bin\"" >> /etc/profile'
 fi
 
 ssh "admin@${ROBOT_HOSTNAME}" "sed -i 's/vm\.overcommit_memory=2/vm\.overcommit_memory=0/' /etc/sysctl.conf"
diff --git a/frc971/downloader.bzl b/frc971/downloader.bzl
index aebcb87..ec93f5b 100644
--- a/frc971/downloader.bzl
+++ b/frc971/downloader.bzl
@@ -25,7 +25,7 @@
         ] if target_type == "roborio" else []) + start_binaries,
         srcs = [
             "//aos:prime_binaries",
-        ] + binaries + data,
+        ] + binaries + data + ["//frc971/raspi/rootfs:chrt.sh"],
         dirs = dirs,
         target_type = target_type,
         default_target = default_target,
@@ -40,7 +40,7 @@
                      [expand_label(binary) + ".stripped" for binary in start_binaries],
         srcs = [
             "//aos:prime_binaries_stripped",
-        ] + [expand_label(binary) + ".stripped" for binary in binaries] + data,
+        ] + [expand_label(binary) + ".stripped" for binary in binaries] + data + ["//frc971/raspi/rootfs:chrt.sh"],
         dirs = dirs,
         target_type = target_type,
         default_target = default_target,
diff --git a/frc971/downloader/downloader.py b/frc971/downloader/downloader.py
index dc14df1..4314845 100644
--- a/frc971/downloader/downloader.py
+++ b/frc971/downloader/downloader.py
@@ -69,7 +69,7 @@
             user = "pi"
         elif args.type == "roborio":
             user = "admin"
-    target_dir = "/home/" + user + "/robot_code"
+    target_dir = "/home/" + user + "/bin"
 
     ssh_target = "%s@%s" % (user, hostname)
 
diff --git a/frc971/raspi/rootfs/BUILD b/frc971/raspi/rootfs/BUILD
new file mode 100644
index 0000000..ee5984a
--- /dev/null
+++ b/frc971/raspi/rootfs/BUILD
@@ -0,0 +1 @@
+exports_files(["chrt.sh"])
diff --git a/frc971/raspi/rootfs/chrt.sh b/frc971/raspi/rootfs/chrt.sh
new file mode 100755
index 0000000..535f4c6
--- /dev/null
+++ b/frc971/raspi/rootfs/chrt.sh
@@ -0,0 +1,37 @@
+#!/bin/bash
+
+set -e
+
+function chrtirq() {
+  PIDS="$(ps -ef | grep "\\[$1\\]" | awk '{print $2}')"
+
+  for PID in $PIDS; do
+    chrt $2 -p $3 "${PID}"
+
+    ps -q "${PID}" -o comm= | tr -d '[:space:]'
+    echo -n " "
+    chrt -p "${PID}"
+  done
+
+  if [ -z "${PID}" ]; then
+    echo "No such IRQ ${1}"
+  fi
+}
+
+chrtirq "irq/[0-9]*-fe00b880" -f 50
+chrtirq "irq/[0-9]*-fe204000" -f 60
+chrtirq "irq/[0-9]*-adis1650" -f 61
+chrtirq "irq/[0-9]*-xhci_hcd" -f 1
+chrtirq "irq/[0-9]*-VCHIQ do" -o 0
+chrtirq "irq/[0-9]*-DMA IRQ" -f 50
+chrtirq "irq/[0-9]*-mmc1" -o 0
+chrtirq "irq/[0-9]*-mmc0" -o 0
+chrtirq "irq/[0-9]*-s-mmc0" -o 0
+chrtirq "irq/[0-9]*-v3d" -o 0
+chrtirq "irq/24-vc4 hvs" -o 0
+chrtirq "irq/[0-9]*-vc4 hdmi" -o 0
+chrtirq "irq/[0-9]*-s-vc4 hd" -o 0
+chrtirq "irq/19-fe004000" -f 50
+chrtirq "irq/[0-9]*-vc4 crtc" -o 0
+chrtirq "irq/23-uart-pl0" -o 0
+chrtirq "irq/[0-9]*-eth0" -f 10
diff --git a/frc971/raspi/rootfs/enable_imu.sh b/frc971/raspi/rootfs/enable_imu.sh
new file mode 100755
index 0000000..3725207
--- /dev/null
+++ b/frc971/raspi/rootfs/enable_imu.sh
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+CONFIG=/boot/config.txt
+
+if grep -q adis16505 "${CONFIG}"; then
+  echo "Already enabled"
+  exit 0;
+fi
+
+sed -i '1h;1!H;$!d;x;s/.*dtparam=spi[^\n]*/&\n\n# Enable the IMU\ndtoverlay=adis16505/' "${CONFIG}"
+
+echo "Enabled 16505"
diff --git a/frc971/raspi/rootfs/frc971.service b/frc971/raspi/rootfs/frc971.service
index cdfb347..be6b37f 100644
--- a/frc971/raspi/rootfs/frc971.service
+++ b/frc971/raspi/rootfs/frc971.service
@@ -7,8 +7,8 @@
 User=pi
 Group=pi
 Type=simple
-WorkingDirectory=/home/pi/robot_code
-ExecStart=/home/pi/robot_code/starter.sh
+WorkingDirectory=/home/pi/bin
+ExecStart=/home/pi/bin/starter.sh
 KillMode=mixed
 TimeoutStopSec=10
 LimitRTPRIO=60
diff --git a/frc971/raspi/rootfs/frc971chrt.service b/frc971/raspi/rootfs/frc971chrt.service
index cc68934..6086fa0 100644
--- a/frc971/raspi/rootfs/frc971chrt.service
+++ b/frc971/raspi/rootfs/frc971chrt.service
@@ -3,7 +3,7 @@
 
 [Service]
 Type=oneshot
-ExecStart=/home/pi/robot_code/chrt.sh
+ExecStart=/home/pi/bin/chrt.sh
 
 [Install]
 WantedBy=multi-user.target
diff --git a/frc971/raspi/rootfs/modify_rootfs.sh b/frc971/raspi/rootfs/modify_rootfs.sh
index fd9bec9..15bdd80 100755
--- a/frc971/raspi/rootfs/modify_rootfs.sh
+++ b/frc971/raspi/rootfs/modify_rootfs.sh
@@ -36,8 +36,13 @@
 if ! grep "gpu_mem=128" "${BOOT_PARTITION}/config.txt"; then
   echo "gpu_mem=128" | sudo tee -a "${BOOT_PARTITION}/config.txt"
 fi
+if ! grep "enable_uart=1" "${BOOT_PARTITION}/config.txt"; then
+  echo "enable_uart=1" | sudo tee -a "${BOOT_PARTITION}/config.txt"
+fi
 # For now, disable the new libcamera driver in favor of legacy ones
 sudo sed -i s/^camera_auto_detect=1/#camera_auto_detect=1/ "${BOOT_PARTITION}/config.txt"
+# Enable SPI.
+sudo sed -i s/^.*dtparam=spi=on/dtparam=spi=on/ "${BOOT_PARTITION}/config.txt"
 
 sudo tar -zxvf "${KERNEL}" --strip-components 2 -C ${BOOT_PARTITION}/ ./fat32
 
@@ -80,11 +85,17 @@
   sudo mount -o loop,offset=${OFFSET} "${IMAGE}" "${PARTITION}"
 fi
 
+if [[ ! -e wiringpi-2.70-1.deb ]]; then
+  wget --continue https://software.frc971.org/Build-Dependencies/wiringpi-2.70-1.deb
+fi
+
 sudo cp target_configure.sh "${PARTITION}/tmp/"
+sudo cp wiringpi-2.70-1.deb "${PARTITION}/tmp/"
 sudo cp dhcpcd.conf "${PARTITION}/tmp/dhcpcd.conf"
 sudo cp sctp.conf "${PARTITION}/etc/sysctl.d/sctp.conf"
 sudo cp logind.conf "${PARTITION}/etc/systemd/logind.conf"
 sudo cp change_hostname.sh "${PARTITION}/tmp/change_hostname.sh"
+sudo cp enable_imu.sh "${PARTITION}/tmp/"
 sudo cp frc971.service "${PARTITION}/etc/systemd/system/frc971.service"
 sudo cp frc971chrt.service "${PARTITION}/etc/systemd/system/frc971chrt.service"
 sudo cp rt.conf "${PARTITION}/etc/security/limits.d/rt.conf"
@@ -96,6 +107,8 @@
 
 sudo rm -rf "${PARTITION}/lib/modules/"*
 sudo tar -zxvf "${KERNEL}" --strip-components 4 -C "${PARTITION}/lib/modules/" ./ext4/lib/modules/
+sudo cp adis16505.ko "${PARTITION}/lib/modules/5.10.78-rt55-v8+/kernel/"
+target /usr/sbin/depmod 5.10.78-rt55-v8+
 
 # Downloads and installs our target libraries
 target /bin/bash /tmp/target_configure.sh
diff --git a/frc971/raspi/rootfs/target_configure.sh b/frc971/raspi/rootfs/target_configure.sh
index 6d9171e..388d291 100755
--- a/frc971/raspi/rootfs/target_configure.sh
+++ b/frc971/raspi/rootfs/target_configure.sh
@@ -9,9 +9,12 @@
 
 # And provide a script to change it.
 cp /tmp/change_hostname.sh /root/bin/
+cp /tmp/enable_imu.sh /root/bin/
 chmod a+x /root/bin/change_hostname.sh
+chmod a+x /root/bin/enable_imu.sh
 
 chown -R pi.pi /home/pi/.ssh
+chown -R pi.pi /home/pi/bin
 
 apt-get update
 
@@ -41,13 +44,7 @@
   libnice-dev \
   feh
 
-# Install WiringPi gpio for PWM control
-if [[ ! -e "/usr/bin/gpio" ]]; then
-    cd /tmp
-    git clone https://github.com/WiringPi/WiringPi.git
-    cd WiringPi
-    ./build
-fi
+dpkg -i /tmp/wiringpi-2.70-1.deb
 
 echo 'GOVERNOR="performance"' > /etc/default/cpufrequtils
 
diff --git a/y2016/dashboard/dashboard.cc b/y2016/dashboard/dashboard.cc
index d01243f..a7cc821 100644
--- a/y2016/dashboard/dashboard.cc
+++ b/y2016/dashboard/dashboard.cc
@@ -301,7 +301,7 @@
   server.serve("www", 1180);
 #else
   // Absolute directory of www folder on the robot.
-  server.serve("/home/admin/robot_code/www", 1180);
+  server.serve("/home/admin/bin/www", 1180);
 #endif
 
   socket_handler.Quit();
diff --git a/y2019/vision/server/server.cc b/y2019/vision/server/server.cc
index 7334ab4..817da17 100644
--- a/y2019/vision/server/server.cc
+++ b/y2019/vision/server/server.cc
@@ -300,13 +300,13 @@
   bool serve_www = false;
   {
     struct stat result;
-    if (stat("/home/admin/robot_code/www", &result) == 0) {
+    if (stat("/home/admin/bin/www", &result) == 0) {
       serve_www = true;
     }
   }
 
   server.serve(
-      serve_www ? "/home/admin/robot_code/www" : "y2019/vision/server/www",
+      serve_www ? "/home/admin/bin/www" : "y2019/vision/server/www",
       1180);
 
   return 0;
diff --git a/y2020/vision/galactic_search_path.py b/y2020/vision/galactic_search_path.py
index 5e8ef54..00b572a 100755
--- a/y2020/vision/galactic_search_path.py
+++ b/y2020/vision/galactic_search_path.py
@@ -77,8 +77,8 @@
 AOS_SEND_PATH = "bazel-bin/aos/aos_send"
 
 def setup_if_pi():
-    if os.path.isdir("/home/pi/robot_code"):
-        AOS_SEND_PATH = "/home/pi/robot_code/aos_send.stripped"
+    if os.path.isdir("/home/pi/bin"):
+        AOS_SEND_PATH = "/home/pi/bin/aos_send.stripped"
         os.system("./starter_cmd stop camera_reader")
 
 setup_if_pi()