title: Python threading for wheel encoders on a Robot
body: <p>I'm writing the code for a robot which my college is entering into a competition. I'm currently trying to build some wheel encoders using reflectance sensors. I realised a while back that I would probably need to use threading to achieve this, seeing as the robot needs to monitor both the left and right encoders at the same time. The code below is what I have so far: </p>
  
  <pre><code>from __future__ import division
  import threading
  import time
  from sr import *
  R = Robot()
  
  class Encoder(threading.Thread):
      def __init__(self, motor, pin, div=16):
          self.motor = motor
          self.pin = pin
          self.div = div
          self.count = 0
          threading.Thread.__init__(self)
  
      def run(self):
          while True: 
              wait_for(R.io[0].input[self.pin].query.d)
              self.count += 1
  
      def rotations(self, angle, start_speed=50):
          seg = 360/self.div
          startcount = self.count
          current_dist = angle #Distance away from target
          R.motors[self.motor].target = start_speed
          while current_dist &gt; 360:
              newcount = self.count - startcount
              current_dist = angle - newcount*seg
              R.motors[self.motor].target = 50
          while abs(current_dist) &gt; seg/2:  
              newcount = self.count - startcount
              current_dist = angle - newcount*seg
              current_speed = start_speed * current_dist / 360
              if current_speed &lt; 5:
                  R.motors[self.motor].target = 5
              else:
                  R.motors[self.motor].target = current_speed
          R.motors[self.motor].target = 0
  
  WheelLeft = Encoder(0,0)
  WheelLeft.start()
  WheelRight = Encoder(1,3)
  WheelRight.start()
  
  WheelRight.rotations(720)
  WheelLeft.rotations(720)
  </code></pre>
  
  <p>The sr module is provided by Southampton University, who are running the competition. It allows us to interact with the robot's hardware. </p>
  
  <p>Now, the threads which get created seem to allow the two reflectance sensors to be monitored separately. This bit of code: <code>R.io[0].input[self.pin].query.d</code> works out whether the value coming from the reflectance sensor has changed. The 'rotations' method turns the wheel through a certain angle by constantly checking how many degrees the wheel has already turned through, and slowing it down as it reaches the end. I would like both wheels to start turning when I run the program, and then slow down and stop when they have gone through 2 rotations. Currently though, when I run the program, one wheel starts turning and slows down and stops, followed by the other wheel. It seems to me like the 'rotations' method is not running in a thread, like the 'run' method is. Is it only the code under the 'run' method that runs in a thread, or is it the whole class?</p>
  
  <p>If it helps, I've been following this tutorial: <a href="http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/1/" rel="nofollow">http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/1/</a></p>
  
  <p>Also, I would like to know why it is possible to start a thread only with <code>Encoder(0,0).start()</code>. Why do you not have to create an object using the class (e.g. <code>Thread = Encoder(0,0).start()</code> for a new thread to be created?</p>
  
  <p>Sorry if the terminoligy I've used isn't up to scratch, as you can probably tell I'm quite new to threading, and programming in general. </p>