For part I see this page.


For those that did part I, you may or may not have read the assignment I gave you, which was to try to break my code. If you need the solutions (solutions to break my code? you must not be trying) try shaking the robovero sideways left and right. Look at what GUI shows you? Notice how the bar wobbles quite a bit? Here's an explanation why:


Shaking the Robovero causes the accelerometer to read the shaking acceleration. If the shaking is not on the same direction of gravity, our program thinks gravity is pointing somewhere else!


So the accelerometer feels drift from shaking. How else can we determine the orientation of the Robovero? Let's use the gyroscope. The Robovero is possess a three axis gyroscope, which measures the rate the Robovero is turning about three axis.


Gyroscopes measure angular velocity. As any high school physics student should know, angular velocity can be given by the formula:


angular velocty = angle / time


Therefore, to find angle we multiple our speed by time. We take multiple samples in case if our angular speed changes. Give it a try. Place your Robovero on a flat surface then start the following script (which you should save in your Robovero folder as Then turn your Robovero around and look at the screen! (Note: I expect you did the tutorial in part I for this code to work).


from robovero.extras import Array, roboveroConfig
from robovero.lpc17xx_i2c import I2C_M_SETUP_Type, I2C_MasterTransferData, \
from robovero.lpc17xx_gpio import GPIO_ReadValue
from robovero.LPC17xx import LPC_I2C0
from robovero.lpc_types import Status
import time
import csv
from math import *
import cv

__author__ =      ["Neil MacMunn", "Danny Chan"]
__email__ =       ""
__copyright__ =   "Copyright 2012, Gumstix Inc"
__license__ =     "BSD 2-Clause"
__version__ =     "0.1"

u = 0.15

accel_ctrl_reg1 = 0×20
accel_ctrl_reg4 = 0×23
accel_x_low = 0×28

gyro_ctrl_reg1 = 0×20
gyro_ctrl_reg2 = 0×21
gyro_ctrl_reg3 = 0×22
gyro_ctrl_reg4 = 0×23
gyro_ctrl_reg5 = 0×24
gyro_status_reg = 0×27
gyro_x_low = 0×28
gyro_fifo_ctrl_reg = 0x2E

class I2CDevice(object):
  def __init__(self, address):
    self.config = I2C_M_SETUP_Type()
    self.tx_data = Array(2, 1)
    self.rx_data = Array(1, 1)
    self.rx_data6 = Array(6, 1)
    self.config.sl_addr7bit = address
    self.config.tx_data = self.tx_data.ptr
    self.config.retransmissions_max = 3

  def readReg(self, register):
    self.tx_data[0] = register
    self.config.tx_length = 1
    self.config.rx_data = self.rx_data.ptr
    self.config.rx_length = 1
    ret = I2C_MasterTransferData(LPC_I2C0, self.config.ptr,
    if ret == Status.ERROR:
      exit("I2C Read Error")   
    return self.rx_data[0]

  def read6Reg(self, register):
    self.tx_data[0] = register | 0b10000000 #MSB must be equal to 1 to read multiple bytes
    self.config.tx_length = 1
    self.config.rx_data = self.rx_data6.ptr
    self.config.rx_length = 6
    ret = I2C_MasterTransferData(LPC_I2C0, self.config.ptr,
    if ret == Status.ERROR:
      exit("I2C Read Error")   
    return self.rx_data6

  def writeReg(self, register, value):
    self.tx_data[0] = register
    self.tx_data[1] = value
    self.config.tx_length = 2
    self.config.rx_data = 0
    self.config.rx_length = 0
    ret = I2C_MasterTransferData(LPC_I2C0, self.config.ptr,
    if ret == Status.ERROR:
      exit("I2C Write Error")
    if self.readReg(register) != value:
      exit("I2C Verification Error")
    return None

# Initialize pin select registers

# configure accelerometer
accelerometer = I2CDevice(0×18)
accelerometer.writeReg(accel_ctrl_reg1, 0×27)
accelerometer.writeReg(accel_ctrl_reg4, 0×00)

# configure the gyro
gyro = I2CDevice(0×68)
gyro.writeReg(gyro_ctrl_reg3, 0×08) # enable DRDY
gyro.writeReg(gyro_ctrl_reg4, 0×80) # enable block data read mode
gyro.writeReg(gyro_ctrl_reg1, 0x0F) # normal mode, enable all axes, 250dps

def twosComplement(low_byte, high_byte):
  """Unpack 16-bit twos complement representation of the result.
  return (((low_byte + (high_byte << 8)) + 2**15) % 2**16 – 2**15)

cv.NamedWindow("Window", cv.CV_WINDOW_AUTOSIZE)
bar = cv.LoadImage("Bar.png")
cv.ShowImage("Window", bar)
#while True:
#  pass
rotatedBar = cv.LoadImage("Bar.png")
rotationMat = cv.CreateMat(2, 3, cv.CV_32FC1)
center = (bar.width/2,bar.height/2)
angle = 0
while True:
  gyrodata = gyro.read6Reg(gyro_x_low)
  angle += 0.1 * twosComplement(gyrodata[2], gyrodata[3]) / 1055.0
  cv.ShowImage("Window", rotatedBar)
  print angle
  var = cv.WaitKey(100)
  if var != -1:


Again try to break this code (for part III)! This one is a bit harder to break, but if you need a hint, try just leaving the script on for an half an hour or so.