Medical Instruments

Everything is hosted in a wheelchair. Here is a picture of the manual control.

?

Here is another view of the wheelchair.

?

If you look at an encyclopedia, you will learn that electromyography (EMG) is a technique for the activation signal of muscles. EMG is performed by an instrument called electromyograph, to produce a record called an electromyogram. A program in Clean recognizes patterns in the electromyogram, and discovers the intention of the chairs drivers. Therefore, the guy or gall can move the chair around without using his/her hands; well, if one is quadriplegic, s/he cannot use his/her hands anyway. Here is a guy using emg sensors:

?

The chair needs to know where it is. Therefore it uses an instrument called GPS. It also needs to call a doctor if something goes wrong. Finally, it needs to show the way in a map to the doctor, so s/he will arrive without delay; this can be a question of life a death. To make a long story short, the chair needs a GPS and communication instruments to call the doctor and give him/her instructions on how to reach the patient on the Wolmer wheelchair. Here is the electronics that the chair uses to find where it is and to call a doctor if necessary:

?

Here is how everything works, communication and signal processing:

?

The medical instruments associated with the chair records and analyse five kinds of signals, to wit:
  1. Electrocardiogram checks the heart. It is an interpretation of the electrical activity of the heart over time for diagnostic purposes.
  2. Electroencephalogram checks the brain. It is a measurement of the electrical activity of the brain for diagnostic purposes.
  3. Electromyogram records and interprets electrical activity in the muscles, so the chair will know where the driver wants to go. Since the driver is unlikey to have healthy leg and arm muscles, the signals are taken from the head.
  4. Carbon dioxide monitoring. The excretion of CO2 is the final pathway of metabolism. As such it provides a useful global indication that all is well. There is a wave form of the CO2 from each breath which is known as the capnogram. Here is its shape:
    ?
    It starts with a rapid rise as CO2 comes out of the major airways. Then there is a slow rise. Finally there is a rapid decline as the next breath enters the lungs.
  5. Thermometer. Well, fever is the most common syntom that something is wrong.
The smart chair must know to interpret all the above signals. It has sensors, and pattern recognition programs. The sensors are small computers that collect physiological signals. They sensors need to communicate with the main computer. The communication is done by USB, RS232 (in fact, a USB-UART driver accepts serial signal from a USB port), infrared and wireless connections. Pattern recognition detects things like the intention of the wheelchair rider (through EMG), deviant capnograms, pathological ECG or EEG, and fever. Pattern recognition uses neural networks. All systems of the smart chair is supposed to be written in Clean, because it is safer than other computer languages. The embeded programs that go into the small sensor computers are writen in Clean generated C, i. e., a Clean program generates code for the microprocessors. Here is a very simple neural network written in Clean:

module neuralearn
import StdEnv

sig x = 1.0 / (1.0 + (exp (~ x)))

error vt = loop vt 0 0.0
where
  loop vt i acc | i>= size vt = (acc, vt)
  loop vt i acc
   # (e, ww) = vt![i]
   = loop ww (i+1) (acc+e)

na vt=:{[4]=x, [5]=y, [6]=z} [i1, i2]=
  (sig (x+y*i1+z*i2), vt)

ns vt [i1, i2, i3]
# (v0, vt)= vt![0]
# (v1, vt)= vt![1]
# (v2, vt)= vt![2]
# (v3, vt)= vt![3]
= (sig(v0+i1*v1+i2*v2+i3*v3), vt)

gate vt [i1, i2]
# (n1, vt)= na vt [i1, i2]
# (n2, vt)= ns vt [i1, n1, i2]
= (n2, vt)

errSum v2 [] acc= (acc, v2)
errSum v2 [ex:s] acc
# v= hd ex
# (vc, v3)= gate v2 (tl ex)
= errSum v3 s (acc+(vc-v)*(vc-v))

updweight vt nvt err0 ns exs = loop 0 vt nvt
where
  dx = 0.01
  mu = 0.5
  loop i vt vs | i > ns = (vs, vt)
  loop i vt vs
   # (v, v1) = vt![i]
   # v2 = {v1 & [i]= v+dx}
   # (nerr, v3)= errSum v2 exs 0.0
   # nv= v + mu*(err0 - nerr)/dx
   = loop (i+1) {v3& [i]=v} {vs & [i]=nv}

train exs = loop v1 v2
where
v1 = {0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0}
v2 = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}
loop vt nvt
# (nerr, vt) = errSum vt exs 0.0
| nerr < 0.01 =vt
# (vt, nvt)= updweight vt nvt nerr 6 exs
= loop vt nvt

VT :== train [eg e e <- [0,1,2,3,3,2,1,0]]

Start = (xor 0.0 0.0, xor 0.0 1.0, xor 1.0 0.0, xor 1.0 1.0)
where
  vt :: !.{#!Real}
  vt = VT
  exs = [eg e e <- [0,1,2,3,3,2,1,0]]
  xor i1 i2= fst(gate vt [i1,i2])

eg 0 = [0.0, 1.0, 1.0]
eg 1 = [1.0, 1.0, 0.0]
eg 2 = [1.0, 0.0, 1.0]
eg 3 = [0.0, 0.0, 0.0]


Communication is also in Clean. However, here is a simplified version of serial communication translated from Clean to Haskell:

{-# LANGUAGE ForeignFunctionInterface #-}
module SER.IAL where

  import Control.Monad
  import Foreign
  import Foreign.C.Types
  import Foreign.C

  foreign import ccall "rs232.h opencport" opencport :: CInt -> IO ()
  foreign import ccall "rs232.h closecport" closecport :: CInt -> CInt

  foreign import ccall "rs232.h rdrs232" c_sendmsg :: CInt -> CString -> CString
  sendMessage :: Int -> String -> IO String
  sendMessage n msg =
    withCString msg $
      str -> peekCString (c_sendmsg (fromIntegral n) str)


History

All instruments you have seen on this page were designed and manufactured by P. S. Caparelli, who made me a gift of the sensors, and a copy of the programs that drive the system. I simplified the programs and libraries, and translated the most interesting ones to Haskell. I will post the Haskell programs on this page in a near future.

Wolmer was the person who owned a malfunctioning wheelchair and contacted a manufacturer of elevators (lifts) to fix it. The elevator manufacturer hired Caparelli to do the job. The legend says that Caparelli, instead of fixing the wheelchair, designed a state of the art model. The new wheelchair was named after the person who sent his malfunctioning chair to the elevator factory. I suppose he received one of the new chairs free of charge.

Disclaimer and Aknowledgements

All systems described on this page were designed and build by P. S. Caparelli et al. This page has the sole goal of providing a real world example of functional programming. The Webmaster does not claim to have participated in building the sensors, the GPS, or the chair. I will like to express my gratitude to P. S. Caparelli for providing me the instruments and a copy of the Clean programs, so I could translate them to Haskell and test the Haskell versions.

free stats