Skip to content

Commit

Permalink
added files
Browse files Browse the repository at this point in the history
  • Loading branch information
kameyer226 committed Oct 29, 2024
1 parent 490d585 commit 1aa44e6
Show file tree
Hide file tree
Showing 3 changed files with 399 additions and 0 deletions.
65 changes: 65 additions & 0 deletions yaqd_wright/_wright_arduino.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
__all__ = ["WrightArduino"]

import asyncio
from typing import Dict

from yaqd_core import HasPosition, UsesUart, UsesSerial, aserial


class WrightArduino(HasPosition, UsesUart, UsesSerial):
_kind = "wright-arduino"
serial_dispatchers: Dict[str, aserial.ASerial] = {}

def __init__(self, name, config, config_filepath):
super().__init__(name, config, config_filepath)
#self._reset_on_not_busy = False
if config["default_active_pin"]:
self._state["active_pin"] = int(config["default_active_pin"])
else:
self._state["active_pin"] = int(0)

if config["serial_port"] in WrightArduino.serial_dispatchers:
self._serial_port = WrightArduino.serial_dispatchers[config["serial_port"]]
else:
self._serial_port = aserial.ASerial(config["serial_port"], config["baud_rate"])
WrightArduino.serial_dispatchers[config["serial_port"]] = self._serial_port


def set_active_pin(self, pin):
pin = int(pin)
self._state["active_pin"] = pin


def get_active_pin(self):
pin= int(self._state["active_pin"])
return pin


def _set_position(self, position):
#self._reset_on_not_busy = True
if (position > 1.0):
position = 1.00
elif (position < 0.0):
position = 0.00
else:
position=float(round(position))
self._state["destination"] = position
self._busy=False


def direct_serial_write(self, message):
#self._busy = True
self._serial_port.write(message)


async def update_state(self):
while True:
if (self._state["destination"] != self._state["position"]):
activepin=int(self._state["active_pin"])
activepinhex=str(hex(activepin)[2:])
destination=int(self._state["destination"])
self._serial_port.write(f"M{activepinhex}{destination}\n".encode())
# note the line below assumes the write was successful
self._state["position"]=self._state["destination"]
await asyncio.sleep(0.1)

285 changes: 285 additions & 0 deletions yaqd_wright/wright-arduino.avpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
{
"config": {
"baud_rate": {
"default": 9600,
"origin": "uses-uart",
"type": "int"
},
"default_active_pin": {
"default": 0,
"doc": "default pin for digital control",
"type": "int"
},
"enable": {
"default": true,
"doc": "Disable this daemon. The kind entry-point will not attempt to start this daemon.",
"origin": "is-daemon",
"type": "boolean"
},
"log_level": {
"default": "info",
"doc": "Set daemon log-level.",
"origin": "is-daemon",
"type": {
"name": "level",
"symbols": [
"debug",
"info",
"notice",
"warning",
"error",
"critical",
"alert",
"emergency"
],
"type": "enum"
}
},
"log_to_file": {
"default": false,
"doc": "Optionally force logging to a file.",
"origin": "is-daemon",
"type": "boolean"
},
"make": {
"default": null,
"origin": "is-daemon",
"type": [
"null",
"string"
]
},
"model": {
"default": null,
"origin": "is-daemon",
"type": [
"null",
"string"
]
},
"port": {
"doc": "TCP port for daemon to occupy.",
"origin": "is-daemon",
"type": "int"
},
"serial": {
"default": null,
"doc": "Serial number for the particular device represented by the daemon",
"origin": "is-daemon",
"type": [
"null",
"string"
]
},
"serial_port": {
"origin": "uses-uart",
"type": "string"
}
},
"doc": "Expose an arduino as a yaq daemon.\n The arduino is currently configured for simple digital Writes to DO pins, and\n cannot be used to access DO higher than 16 (0x0F).",
"installation": {
"PyPI": "https://pypi.org/project/yaqd-wright",
"conda-forge": "https://anaconda.org/conda-forge/yaqd-wright"
},
"links": {
"bugtracker": "https://github.com/wright-group/yaqd-wright/issues",
"source": "https://github.com/wright-group/yaqd-wright"
},
"messages": {
"busy": {
"doc": "Returns true if daemon is currently busy.",
"origin": "is-daemon",
"request": [],
"response": "boolean"
},
"direct_serial_write": {
"doc": "Expose direct access to the serial port to clients.\n\nThis should not be encouraged for normal use, but may be very important for debugging.\nIf a device is expected to return data, it should be logged at the INFO level, not returned to the client.\nThis is done to allow long tasks to be run asynchronously, and to explicitly discourage use of this method except when debugging.\nSetting `busy` to true is encouraged, but individual daemon writers should consider their own use case.\n",
"origin": "uses-serial",
"request": [
{
"name": "message",
"type": "bytes"
}
],
"response": "null"
},
"get_active_pin": {
"doc": "Get the currently active pin for digital control.",
"request": [],
"response": "int"
},
"get_config": {
"doc": "Full configuration for the individual daemon as defined in the TOML file.\nThis includes defaults and shared settings not directly specified in the daemon-specific TOML table.\n",
"origin": "is-daemon",
"request": [],
"response": "string"
},
"get_config_filepath": {
"doc": "String representing the absolute filepath of the configuration file on the host machine.\n",
"origin": "is-daemon",
"request": [],
"response": "string"
},
"get_destination": {
"doc": "Get current daemon destination.",
"origin": "has-position",
"request": [],
"response": "double"
},
"get_position": {
"doc": "Get current daemon position.",
"origin": "has-position",
"request": [],
"response": "double"
},
"get_state": {
"doc": "Get version of the running daemon",
"origin": "is-daemon",
"request": [],
"response": "string"
},
"get_units": {
"doc": "Get units of daemon. These units apply to the position and destination properties.",
"origin": "has-position",
"request": [],
"response": [
"null",
"string"
]
},
"id": {
"doc": "JSON object with information to identify the daemon, including name, kind, make, model, serial.\n",
"origin": "is-daemon",
"request": [],
"response": {
"type": "map",
"values": [
"null",
"string"
]
}
},
"set_active_pin": {
"doc": "Set the chunk count.",
"request": [
{
"name": "activepin",
"type": "int"
}
],
"response": "null"
},
"set_position": {
"doc": "Give the daemon a new destination, and begin motion towards that destination.",
"origin": "has-position",
"request": [
{
"name": "position",
"type": "double"
}
],
"response": "null"
},

"shutdown": {
"doc": "Cleanly shutdown (or restart) daemon.",
"origin": "is-daemon",
"request": [
{
"default": false,
"name": "restart",
"type": "boolean"
}
],
"response": "null"
}
},
"properties": {
"active_pin": {
"control_kind": "normal",
"dynamic": true,
"getter": "get_active_pin",
"limits_getter": null,
"options_getter": null,
"record_kind": "metadata",
"setter": "set_active_pin",
"type": "int",
"units_getter": null
},
"destination": {
"control_kind": "hinted",
"dynamic": true,
"getter": "get_destination",
"limits_getter": null,
"options_getter": null,
"record_kind": "data",
"setter": "set_position",
"type": "double",
"units_getter": "get_units"
},
"position": {
"control_kind": "hinted",
"dynamic": true,
"getter": "get_position",
"limits_getter": null,
"options_getter": null,
"record_kind": "data",
"setter": null,
"type": "double",
"units_getter": "get_units"
}
},
"protocol": "wright-arduino",
"requires": [],
"state": {
"active_pin": {
"default": 0,
"doc": "Active pin for digital control.",
"type": "int"
},
"destination": {
"default": NaN,
"origin": "has-position",
"type": "double"
},
"position": {
"default": NaN,
"origin": "has-position",
"type": "double"
}
},
"traits": [
"has-position",
"is-daemon",
"uses-serial",
"uses-uart"
],
"types": [
{
"fields": [
{
"name": "shape",
"type": {
"items": "int",
"type": "array"
}
},
{
"name": "typestr",
"type": "string"
},
{
"name": "data",
"type": "bytes"
},
{
"name": "version",
"type": "int"
}
],
"logicalType": "ndarray",
"name": "ndarray",
"type": "record"
}
]
}
Loading

0 comments on commit 1aa44e6

Please sign in to comment.