Created
March 17, 2017 04:36
-
-
Save rhasbury/24e9f988f2469fb6c72464ee3426f2f4 to your computer and use it in GitHub Desktop.
Python code for Stepper Gauge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import sys | |
import json | |
import RPi.GPIO as GPIO | |
import socket | |
import time | |
step_pin = 12 | |
direction_pin = 16 | |
current_position = 0 | |
def Main(): | |
host = '192.168.1.104' | |
port = 50008 | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(step_pin, GPIO.OUT) | |
GPIO.setup(direction_pin, GPIO.OUT) | |
while(1): | |
try: | |
mySocket = socket.socket() | |
mySocket.connect((host,port)) | |
message = "get_powers" | |
mySocket.send(message.encode()) | |
data = mySocket.recv(1024).decode() | |
print ('Received from server: ' + data) | |
mySocket.close() | |
except: | |
raise | |
try: | |
thepowers = json.loads(data) | |
new_position = int(thepowers['totalwatts']) | |
MoveMotorAbs(new_position, 1) | |
except: | |
raise | |
time.sleep(2) | |
def MoveMotorAbs(new_position, speed=100): | |
global current_position | |
moveto = new_position - current_position | |
current_position = new_position | |
print("moving to {}".format(moveto)) | |
if(moveto > 0): | |
GPIO.output(direction_pin, GPIO.LOW) | |
print("forward") | |
else: | |
GPIO.output(direction_pin, GPIO.HIGH) | |
print("reverse") | |
for i in range(0, abs(moveto)): | |
GPIO.output(step_pin, GPIO.HIGH) | |
time.sleep(speed*0.001) | |
GPIO.output(step_pin, GPIO.LOW) | |
time.sleep(speed*0.001) | |
current_position = new_position | |
if __name__ == "__main__": | |
Main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment