Différences entre les versions de « GadgetData »

De Semantic MediaWiki - Sandbox

Balise : Éditeur de wikicode 2017
m (change value and add the code used to do this on the RasPi)
Balise : Éditeur de wikicode 2017
Ligne 1 : Ligne 1 :
 
I am setting up a Raspberry Pi (call it "the gadget") that will read [[Property:GadgetTestProperty]] encoded on this page. If the value is between 0 and 10 it will drive a servo in a specific way.  
 
I am setting up a Raspberry Pi (call it "the gadget") that will read [[Property:GadgetTestProperty]] encoded on this page. If the value is between 0 and 10 it will drive a servo in a specific way.  
  
== setting the value ==
+
== Setting the value ==
Setting [[Property:GadgetTestProperty]] to [[GadgetTestProperty::9]]<nowiki></nowiki>
+
Setting [[Property:GadgetTestProperty]] to [[GadgetTestProperty::8]]<nowiki></nowiki>
 
* SMW could encode more, but for now let's have one so my gadget doesn't have to error check.
 
* SMW could encode more, but for now let's have one so my gadget doesn't have to error check.
  
== API code to read it ==
+
== API code to read the value ==
 
The following will query what the value of [[Property:GadgetTestProperty]] for this page:
 
The following will query what the value of [[Property:GadgetTestProperty]] for this page:
 
* '''<nowiki> https://sandbox.semantic-mediawiki.org/w/api.php?action=ask&query=[[GadgetData]]|?GadgetTestProperty&format=json </nowiki>'''
 
* '''<nowiki> https://sandbox.semantic-mediawiki.org/w/api.php?action=ask&query=[[GadgetData]]|?GadgetTestProperty&format=json </nowiki>'''
Ligne 11 : Ligne 11 :
 
The following will return all properties for this page:  
 
The following will return all properties for this page:  
 
* <nowiki> https://sandbox.semantic-mediawiki.org/w/api.php?action=browsebysubject&subject=GadgetData&format=json </nowiki>
 
* <nowiki> https://sandbox.semantic-mediawiki.org/w/api.php?action=browsebysubject&subject=GadgetData&format=json </nowiki>
 +
 +
== Python code that reads the value and moves servo ==
 +
<code>
 +
import pigpio
 +
import time
 +
import json
 +
import requests #used to grab the data at the URL
 +
import time #for sleeping
 +
 +
respURL = "https://sandbox.semantic-mediawiki.org/w/api.php?action=ask&query=[[GadgetData]]|?GadgetTestProperty&format=json"
 +
pollDelay = 10 # the delay in s between polls of respURL
 +
timeOneUnit = 1 # time it takes to travel DOWN one unit of time - empirical
 +
upRatio = 1.28 # the ratio of time it takes to go up rather than down - empirical
 +
tapeMax = 6.5 # 6.5s goes to not quite the max of red tape
 +
dataMax = 10 # show value between 0 and this, ie divide the tapeMax by this to get the distance/time per unit
 +
 +
 +
def tapeHold():
 +
print("tapeHold")
 +
pi.hardware_PWM(18,47,71000) # parameters are a mix between spec and empirical
 +
 +
def tapeUp(howMuch):
 +
print("tapeUp by " + str(howMuch))
 +
pi.hardware_PWM(18,47,61000) # parameters are a mix between spec and empirical
 +
time.sleep(howMuch * tapeMax/dataMax * upRatio)
 +
tapeHold()
 +
 +
def tapeDown(howMuch):
 +
print("tapeDown by " + str(howMuch))
 +
pi.hardware_PWM(18,46,78300) # parameters are a mix between spec and empirical
 +
time.sleep(howMuch * tapeMax/dataMax)
 +
tapeHold()
 +
 
 +
# initialize
 +
pi = pigpio.pi()
 +
curr = 0
 +
newVal = 0
 +
 +
while True:
 +
response = requests.get(respURL)
 +
AllData = (response.text)
 +
JSONData = json.loads(AllData)
 +
KeyValue = (JSONData["query"]["results"]["GadgetData"]["printouts"]["GadgetTestProperty"])
 +
if str(KeyValue) == "[]": # that's what we get when there is no value or an out of range value
 +
print ("Not getting good data from page, assuming current value stays")
 +
newVal = curr
 +
else:
 +
newVal = (KeyValue[0])
 +
 +
if newVal == curr:
 +
print("same")
 +
elif newVal > curr:
 +
tapeUp(newVal-curr)
 +
curr = newVal
 +
elif newVal < curr:
 +
tapeDown(curr-newVal)
 +
curr = newVal
 +
else:
 +
print("this should never happen")
 +
 +
time.sleep(pollDelay)
 +
</code>

Version du 30 mars 2019 à 07:08

I am setting up a Raspberry Pi (call it "the gadget") that will read Property:GadgetTestProperty encoded on this page. If the value is between 0 and 10 it will drive a servo in a specific way.

Setting the value

Setting Property:GadgetTestProperty to 8

  • SMW could encode more, but for now let's have one so my gadget doesn't have to error check.

API code to read the value

The following will query what the value of Property:GadgetTestProperty for this page:

  • https://sandbox.semantic-mediawiki.org/w/api.php?action=ask&query=[[GadgetData]]|?GadgetTestProperty&format=json

The following will return all properties for this page:

  • https://sandbox.semantic-mediawiki.org/w/api.php?action=browsebysubject&subject=GadgetData&format=json

Python code that reads the value and moves servo

import pigpio import time import json import requests #used to grab the data at the URL import time #for sleeping

respURL = "https://sandbox.semantic-mediawiki.org/w/api.php?action=ask&query=GadgetData|?GadgetTestProperty&format=json" pollDelay = 10 # the delay in s between polls of respURL timeOneUnit = 1 # time it takes to travel DOWN one unit of time - empirical upRatio = 1.28 # the ratio of time it takes to go up rather than down - empirical tapeMax = 6.5 # 6.5s goes to not quite the max of red tape dataMax = 10 # show value between 0 and this, ie divide the tapeMax by this to get the distance/time per unit


def tapeHold(): print("tapeHold") pi.hardware_PWM(18,47,71000) # parameters are a mix between spec and empirical

def tapeUp(howMuch): print("tapeUp by " + str(howMuch)) pi.hardware_PWM(18,47,61000) # parameters are a mix between spec and empirical time.sleep(howMuch * tapeMax/dataMax * upRatio) tapeHold()

def tapeDown(howMuch): print("tapeDown by " + str(howMuch)) pi.hardware_PWM(18,46,78300) # parameters are a mix between spec and empirical time.sleep(howMuch * tapeMax/dataMax) tapeHold()

  1. initialize

pi = pigpio.pi() curr = 0 newVal = 0

while True: response = requests.get(respURL) AllData = (response.text) JSONData = json.loads(AllData) KeyValue = (JSONData["query"]["results"]["GadgetData"]["printouts"]["GadgetTestProperty"]) if str(KeyValue) == "[]": # that's what we get when there is no value or an out of range value print ("Not getting good data from page, assuming current value stays") newVal = curr else: newVal = (KeyValue[0])

if newVal == curr: print("same") elif newVal > curr: tapeUp(newVal-curr) curr = newVal elif newVal < curr: tapeDown(curr-newVal) curr = newVal else: print("this should never happen")

time.sleep(pollDelay)

Les cookies nous aident à fournir nos services. En utilisant nos services, vous acceptez notre utilisation de cookies.