Week 7. Oct 3.10 2018 - michelle-qin/Portfolio GitHub Wiki

Implementing Sensors

Currently working on implementing sensors so the "Start" process can start immediately when the ball comes back to home (thus, executing a "perpetual" motion).

To implement sensors, I imported the following:

import Slush
SlushEngine = Slush.sBoard()

The new code was implemented as follows (work in progress):

def auto(self):
    self.ids.auto.text = "Stop"
    self.ids.auto.color = 0.180, 0.188, 0.980, 1
        
    self.initialize()
    sleep(1)
        #if (SlushEngine.getIOState(j, i) == 
        #if (self.isBallAtBottomOfRamp()):
    self.homeRamp(TOP)
        
    self.homeRamp(HOME)
        
    self.turnOnStaircase(False)
        
    sleep(1)
        
    self.turnOnStaircase(True)
    sleep(5)
    self.openGate(True)
    sleep(1)
    self.openGate(CLOSE)
    self.turnOnStaircase(False)
    self.openGate(False)
        #self.ids.auto.text = "Start"
    if (SlushEngine.getIOState(0, 0) == False):
        self.numberOfCycles = self.numberOfCycles + 1
        if(self.numberOfCycles < 5):          #NOTE: 5 is the number of cycles to run perpetually. This can be changed in the code. I'm currently trying to add a slider on the user interface so the user can decide how many cycles to run. But, when I tried to do that, I got many errors (something to do with kivy I think). I will work with a mentor to figure this out later. 
            self.auto()
        self.ids.auto.text = "Start"

Throughout the week, we received surface tablets and learned to transfer the files on the raspberry pi to the surface tablet so that we no longer need to use the separate tablet and mouse, and we can use a debugger to check our code. It's been quite the learning process - but it has definitely cleaned up the workplace! I followed my teacher Mr. Harlow's tutorial to set everything up.

A problem that we were running into was PyCharm wasn't detecting kivy on the main.py file of Perpetual Motion so it wouldn't run the file. To fix that, we had to change the deployment path to /home/pi/kivy/examples/PerpetualMotion (because of differences in file location) and project interpreter.

Now, the file runs. However, a new issue popped up: when I press the "Staircase" and "Gate" buttons, the machine doesn't respond. But, when I press the "Start" it only brings the ramp up. On the other tablet, everything is working as is. Not sure why it's not working on the surface tablet. Currently using PyCharm's debugger to figure it out.

For the time being, I decided to connect the tablet again and work with it (since its touch screen responds and the machine runs as it's supposed to). Now, I'm working on making the "Start" process perpetual by using the sensor. My updated code is below. I added a small delay after the ball returns to the ramp to give the sensor some time to detect it (after realizing that sometimes the ball would return too late which would end the process too early because the sensor wouldn't detect anything).

def auto(self):
    self.ids.auto.text = "Stop"
    self.ids.auto.color = 0.180, 0.188, 0.980, 1
        
    self.initialize()
    sleep(1)
        #if (SlushEngine.getIOState(j, i) == 
        #if (self.isBallAtBottomOfRamp()):
    self.homeRamp(TOP)
        
    self.homeRamp(HOME)
        
    self.turnOnStaircase(False)
        
    sleep(1)
        
    self.turnOnStaircase(True)
    sleep(5)
    self.openGate(True)
    sleep(1)
    self.openGate(CLOSE)
    self.turnOnStaircase(False)
    self.openGate(False)
        #self.ids.auto.text = "Start"
    sleep(2)
    if (SlushEngine.getIOState(0, 0) == False):
        self.numberOfCycles = self.numberOfCycles + 1
        if(self.numberOfCycles < 3):
            self.auto()
        self.ids.auto.text = "Start"