Skip to content

Commit

Permalink
got it working!
Browse files Browse the repository at this point in the history
  • Loading branch information
ishmandoo committed Apr 9, 2018
1 parent 68a8f1a commit ca4aabb
Show file tree
Hide file tree
Showing 31 changed files with 28,176 additions and 18 deletions.
Binary file added 1518387710.model
Binary file not shown.
Binary file added __pycache__/cart_controller.cpython-35.pyc
Binary file not shown.
Binary file added __pycache__/encoder_analyzer.cpython-35.pyc
Binary file not shown.
Binary file added __pycache__/sabretooth_command.cpython-35.pyc
Binary file not shown.
8 changes: 4 additions & 4 deletions calib.p
@@ -1,12 +1,12 @@
(dp0 (dp0
S'right' S'right'
p1 p1
(I520 (I526
I234 I205
tp2 tp2
sS'left' sS'left'
p3 p3
(I130 (I142
I246 I210
tp4 tp4
s. s.
57 changes: 57 additions & 0 deletions cart_controller.py
@@ -0,0 +1,57 @@
from sabretooth_command import CartCommand
from encoder_analyzer import EncoderAnalyzer
import threading



class CartController:
def __init__(self, cart, analyzer):
self.cart = cart
self.analyzer = analyzer
self.speed = 0
self.cart.enabled = True

self.mm_per_s_per_dc = 0.5 #determined experimentally
#self.limit_switch_thread = threading.Thread(target=self._limit_switch_check)
#self.limit_switch_thread.daemon = True
#self.limit_switch_thread.start()

def _limit_switch_check(self):
while True:
if (self.speed > 0) and self.analyzer.end_stop_high:
self.cart.setSpeed(0)
print("high limit switch")
if (self.speed < 0) and self.analyzer.end_stop_low:
self.cart.setSpeed(0)
print("low limit switch")

def setSpeed(self, speed):
self.speed = max(-2046, min(speed, 2047)) # clamp to correct range
self.cart.setSpeed(self.speed)

def setSpeedMmPerS(self, speed):
speed *= self.mm_per_s_per_dc
if speed < 0:
speed -= 100
else:
speed += 100
self.speed = max(-2046, min(speed, 2047)) # clamp to correct range
self.cart.setSpeed(self.speed)

def zeroAnalyzer(self):
while not self.analyzer.checkEndStopHigh():
self.cart.setSpeed(700)
self.analyzer.setMax()
while not self.analyzer.checkEndStopLow():
self.cart.setSpeed(-700)
self.cart.setSpeed(0)
self.analyzer.setZero()

def goTo(self, target_x):
print(self.analyzer.getCartPosition(), self.analyzer.getPolePosition())
while (self.analyzer.getCartPosition() - target_x) > 25:
self.cart.setSpeed(-1000)
while (self.analyzer.getCartPosition() - target_x) < -25:
self.cart.setSpeed(1000)
self.cart.setSpeed(0)

Binary file added cart_controller.pyc
Binary file not shown.
Binary file added cart_pole_messages.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 11 additions & 11 deletions cartpole_env.py
Expand Up @@ -8,22 +8,22 @@
from image_analyzer_pseye import ImageAnalyzer from image_analyzer_pseye import ImageAnalyzer
import cv2 import cv2


from time import sleep



logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)


class CartPoleEnv(gym.Env): class CartPoleEnv(gym.Env):
metadata = {
'render.modes': ['human', 'rgb_array'],
'video.frames_per_second' : 50
}


def __init__(self, cartport, imageport): def __init__(self, cartport="/dev/ttyACM0", imageport=1):
self.port = port
self.analyzer = ImageAnalyzer(imageport) self.analyzer = ImageAnalyzer(imageport)
self.cart = CartCommand(port=cartport) self.cart = CartCommand(port=cartport)


self.action_space = spaces.Discrete(2) self.action_space = spaces.Discrete(2)
self.observation_space = spaces.Box(np.array([0.,-50.,0.,-50.,-1.,-50.]), np.array([1.,50.,1.,50.,1.,50.])) self.observation_space = spaces.Box(np.array([0.,-50.,0.,-50.,-1.,-50.]), np.array([1.,50.,1.,50.,1.,50.]))


self.last_state = None
self.state = self._getState() self.state = self._getState()
self.last_state = self._getState() self.last_state = self._getState()


Expand Down Expand Up @@ -54,7 +54,7 @@ def _step(self, action):


def _reset(self): def _reset(self):
x, dx, theta, dtheta = self.analyzer.analyzeFrame() x, dx, theta, dtheta = self.analyzer.analyzeFrame()
cart.enabled = True self.cart.enabled = True
while not 0.4 < x < 0.6: while not 0.4 < x < 0.6:
x, dx, theta, dtheta = self.analyzer.analyzeFrame() x, dx, theta, dtheta = self.analyzer.analyzeFrame()


Expand All @@ -74,10 +74,10 @@ def _getData(self):
ypole = np.sin(theta) ypole = np.sin(theta)
return x, xpole, ypole return x, xpole, ypole


def _getState(): def _getState(self):
x, xpole, ypole = self._getData x, xpole, ypole = self._getData()
if not old_state is None: if not self.last_state is None:
state = [x, x-old_state[0], xpole, xpole - old_state[2], ypole, ypole - old_state[4]] state = [x, x-self.last_state[0], xpole, xpole - self.last_state[2], ypole, ypole - self.last_state[4]]
else: else:
state = [x, 0, xpole, 0, ypole, 0] state = [x, 0, xpole, 0, ypole, 0]
return state return state
Expand Down
Binary file added cartpole_env.pyc
Binary file not shown.
Binary file added cem_CartPole-v0_params.h5f
Binary file not shown.

0 comments on commit ca4aabb

Please sign in to comment.