📄 halmodule.lyx
字号:
#LyX 1.3 created this file. For more info see http://www.lyx.org/\lyxformat 221\textclass book\begin_preamble\usepackage[plainpages=false,pdfpagelabels,colorlinks=true,linkcolor=blue]{hyperref}\end_preamble\language english\inputencoding default\fontscheme bookman\graphics default\float_placement !htbp\paperfontsize default\spacing single \papersize letterpaper\paperpackage a4\use_geometry 1\use_amsmath 1\use_natbib 0\use_numerical_citations 0\paperorientation portrait\leftmargin 1in\topmargin 1in\rightmargin 0.8in\bottommargin 0.8in\secnumdepth 1\tocdepth 5\paragraph_separation skip\defskip smallskip\quotes_language english\quotes_times 2\papercolumns 1\papersides 1\paperpagestyle default\layout ChapterCreating Userspace Python Components with the 'hal' module\layout SectionBasic usage\layout StandardA userspace component begins by creating its pins and parameters, then enters a loop which will periodically drive all the outputs from the inputs. The following component copies the value seen on its input pin (\family typewriter passthrough.in\family default ) to its output pin (\family typewriter passthrough.out\family default ) approximately once per second.\layout LyX-Code#!/usr/bin/python\newline import hal, time\newline h = hal.component("passthrough")\newline h.newpin("in", hal.HAL_FLOAT, hal.HAL_IN)\newline h.newpin("out", hal.HAL_FLOAT, hal.HAL_OUT)\newline h.ready()\newline try:\newline while 1:\newline time.sleep(1)\newline h['out'] = h['in']\newline except KeyboardInterrupt:\newline raise SystemExit\layout StandardCopy the above listing into a file named \begin_inset Quotes eld\end_inset passthrough\begin_inset Quotes erd\end_inset , make it executable (\family typewriter chmod +x), \family default and place it on your \family typewriter $PATH\family default . Then try it out:\layout LyX-Code$ halrun\newline halcmd: loadusr passthrough\newline halcmd: show pin\newline Component Pins:\newline Owner Type Dir Value Name\newline 03 float IN 0 passthrough.in\newline 03 float OUT 0 passthrough.out\newline halcmd: setp passthrough.in 3.14\newline halcmd: show pin\newline Component Pins:\newline Owner Type Dir Value Name\newline 03 float IN 3.14 passthrough.in\newline 03 float OUT 3.14 passthrough.out\layout SectionUserspace components and delays\layout StandardIf you typed \begin_inset Quotes eld\end_inset show pin\begin_inset Quotes erd\end_inset quickly, you may see that \family typewriter passthrough.out\family default still had its old value of 0. This is because of the call to 'time.sleep(1)', which makes the assignment to the output pin occur at most once per second. Because this is a userspace component, the actual delay between assignments can be much longer--for instance, if the memory used by the passthrough component is swapped to disk, the assignment could be delayed until that memory is swapped back in.\layout StandardThus, userspace components are suitable for user-interactive elements such as control panels (delays in the range of milliseconds are not noticed, and longer delays are acceptable), but not for sending step pulses to a stepper driver board (delays must always be in the range of microseconds, no matter what).\layout SectionCreate pins and parameters\layout LyX-Codeh = hal.component("passthrough")\layout StandardThe component itself is created by a call to the constructor '\family typewriter hal.component\family default '. The arguments are the HAL component name and (optionally) the prefix used for pin and parameter names. If the prefix is not specified, the component name is used.\layout LyX-Codeh.newpin("in", hal.HAL_FLOAT, hal.HAL_IN)\layout StandardThen pins are created by calls to methods on the component object. The arguments are: pin name suffix, pin type, and pin direction. For parameters, the arguments are: parameter name suffix, parameter type, and parameter direction.\layout Standard\begin_inset Float tablewide falsecollapsed false\layout CaptionHAL Option Names\layout Standard\begin_inset Tabular<lyxtabular version="3" rows="3" columns="5"><features><column alignment="center" valignment="top" leftline="true" width="0"><column alignment="center" valignment="top" leftline="true" width="0"><column alignment="center" valignment="top" leftline="true" width="0"><column alignment="center" valignment="top" leftline="true" width="0"><column alignment="center" valignment="top" leftline="true" rightline="true" width="0"><row topline="true"><cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">\begin_inset Text\layout Standard\series bold Pin and Parameter Types:\end_inset </cell><cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">\begin_inset Text\layout StandardHAL_BIT\end_inset </cell><cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">\begin_inset Text\layout StandardHAL_FLOAT\end_inset </cell><cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">\begin_inset Text\layout StandardHAL_S32\end_inset </cell><cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">\begin_inset Text\layout StandardHAL_U32\end_inset </cell></row><row topline="true"><cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">\begin_inset Text\layout Standard\series bold Pin Directions:\end_inset </cell><cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">\begin_inset Text\layout StandardHAL_IN\end_inset </cell><cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">\begin_inset Text\layout StandardHAL_OUT\end_inset </cell><cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">\begin_inset Text\layout StandardHAL_IO\end_inset </cell><cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">\begin_inset Text\layout Standard\end_inset </cell></row><row topline="true" bottomline="true"><cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">\begin_inset Text\layout Standard\series bold Parameter Directions:\end_inset </cell><cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">\begin_inset Text\layout StandardHAL_RO\end_inset </cell><cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">\begin_inset Text\layout StandardHAL_RW\end_inset </cell><cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">\begin_inset Text\layout Standard\end_inset </cell><cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">\begin_inset Text\layout Standard\end_inset </cell></row></lyxtabular>\end_inset \end_inset \layout StandardThe full pin or parameter name is formed by joining the prefix and the suffix with a \begin_inset Quotes eld\end_inset .\begin_inset Quotes erd\end_inset , so in the example the pin created is called \family typewriter passthrough.in\family default .\layout LyX-Codeh.ready()\layout StandardOnce all the pins and parameters have been created, call the \family typewriter .ready()\family default method.\layout SubsectionChanging the prefix\layout StandardThe prefix can be changed by calling the \family typewriter .setprefix()\family default method. The current prefix can be retrieved by calling the \family typewriter .getprefix()\family default method.\layout SectionReading and writing pins and parameters\layout StandardFor pins and parameters which are also proper Python identifiers, the value may be accessed or set using the attribute syntax:\layout LyX-Codeh.out = h.in\layout StandardFor all pins, whether or not they are also proper Python identifiers, the value may be accessed or set using the subscript syntax:\layout LyX-Codeh['out'] = h['in']\layout SubsectionDriving output (HAL_OUT) pins\layout StandardPeriodically, usually in response to a timer, all HAL_OUT pins should be \begin_inset Quotes eld\end_inset driven\begin_inset Quotes erd\end_inset by assigning them a new value. This should be done whether or not the value is different than the last one assigned. When a pin is connected to a signal, its old output value is not copied into the signal, so the proper value will only appear on the signal once the component assigns a new value.\layout SubsectionDriving bidirectional (HAL_IO) pins\layout StandardThe above rule does not apply to bidirectional pins. Instead, a bidirectional pin should only be driven by the component when the component wishes to change the value. For instance, in the canonical encoder interface, the encoder component only sets the \series bold index-enable\series default pin to \series bold FALSE\series default (when an index pulse is seen and the old value is \series bold TRUE\series default ), but never sets it to \series bold TRUE\series default . Repeatedly driving the pin \series bold FALSE\series default might cause the other connected component to act as though another index pulse had been seen.\layout SectionExiting\layout StandardA \begin_inset Quotes eld\end_inset \family typewriter halcmd unload\family default \begin_inset Quotes erd\end_inset request for the component is delivered as a \family typewriter KeyboardInterrupt\family default exception. When an unload request arrives, the process should either exit in a short time, or call the \family typewriter .exit()\family default method on the component if substantial work (such as reading or writing files) must be done to complete the shutdown process.\layout SectionProject ideas\layout ItemizeCreate an external control panel with buttons, switches, and indicators. Connect everything to a microcontroller, and connect the microcontroller to the PC using a serial interface. Python has a very capable serial interface module called \begin_inset LatexCommand \url[pyserial]{http://pyserial.sourceforge.net/}\end_inset (Ubuntu package name \begin_inset Quotes eld\end_inset python-serial\begin_inset Quotes erd\end_inset , in the universe repository)\layout ItemizeAttach a \begin_inset LatexCommand \url[LCDProc]{http://lcdproc.omnipotent.net/}\end_inset -compatible LCD module and use it to display a digital readout with information of your choice (Ubuntu package name \begin_inset Quotes eld\end_inset lcdproc\begin_inset Quotes erd\end_inset , in the universe repository)\layout ItemizeCreate a virtual control panel using any GUI library supported by Python (gtk, qt, wxwindows, etc)\the_end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -