To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.robotics.rcxOpen lugnet.robotics.rcx in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / RCX / 387
386  |  388
Subject: 
Re: OO programing in RCX
Newsgroups: 
lugnet.robotics.rcx
Date: 
Tue, 11 Jan 2000 17:37:20 GMT
Viewed: 
1645 times
  
Hi Eric,

Yes, I believe you could create a sensor object on the VB side and write your
code thinking in terms of a sensor object. Here's one way:

=-=-=-= BEGIN SAMPLE CODE =-=-=-=
Private m_SensorNumber As Integer
Private m_SensorType As Integer
Private m_SensorMode As Integer
Private m_SensorAngle As Ineteger
Private m_Spirit As Spirit

Public Enum SensorNumbers
    Sensor1 = 0
    Sensor2 = 0
    Sensor3 = 0
End Enum

Public Enum SensorTypes
    None = 0
    Switch = 1
    Temperature = 2
    Light = 3
    Angle = 4
End Enum

Public Enum SensorModes
    Raw = 0
    Bool = 1
    TransistionCounter = 2
    PeriodicCounter = 3
    Percent = 4
    Celsius
    Farenheit
    Angle
End Enum

'Set the class variables to reasonable defaults.
Private Sub Sensor_Initialize()
    m_Spirit = Nothing
    m_SensorNumber = Sensor1
    m_SensorType = Switch
    m_SensorMode = Raw
    m_SensorAngle = 0
End Sub

'Store the Spirit.ocx control to be used by the sensor.
Public Sub SetSpirit(inSirit As Spirit)
    m_Spirit = inSpirit
End Sub

'Set the sensor number, type, and mode.
Public Sub SetupSensor(inNumber As Integer, inType As Integer, inMode As
Integer, inAngle As Integer)
    m_SensorNumber = inNumber
    m_SensorType = inType
    m_SensorMode = inMode
    m_SensorAngle = inAngle

    If Not m_Spirit Is Nothing Then
        m_Spirit.SetSensorType m_SensorNumber, m_SensorType
        m_Spirit.SetSensorMode , m_SensorNumber, m_SensorMode, m_SensorAngle
    End If
End Sub

Public Sub Clear()
    If Not m_Spirit Is Nothing Then
        m_Spirit.ClearSensorValue m_SensorNumber
    End If
End Sub

Public Property Get SensorNumber() As Integer
    SensorNumber = m_SensorNumber
End Property

Public Property Get SensorType() As Integer
    SensorType = m_SensorType
End Property

Public Property Get SensorMode() As Integer
    SensorMode = m_SensorMode
End Property

Public Property Get SensorAngle() As Integer
    SensorAngle = m_SensorAngle
End Property
=-=-=-= END SAMPLE CODE =-=-=-=

You create a sensor object like this:

=-=-=-= BEGIN EXAMPLE OF USE =-=-=-=
Dim snsrLightSensor1 As New Sensor

snsrLightSensor1.SetSpirit MySiritObj
snsrLightSensor1.SetupSensor SensorNumbers.Sensor1, SensorTypes.Light,
SensorModes.Raw, 0
=-=-=-= END EXAMPLE OF USE =-=-=-=

This is a quick and dirty example of a sensor class. You could get more refined
and create light sensor classes, touch sensor classes, etc. The classes could
have specialized methods that apply to that type of sensor. For example, on a
light sensor class, you could have a method that tells you if the sensor is
detecting a certain color. For example:

=-=-=-= BEGIN POSSIBLE EXTENTION =-=-=-=
Public Sub IfSeeColor(inColor As Integer)
Dim iColor As Integer

If inColor = Green Then
   iColor = 50 'hypothetical green value
ElseIf inColor = Blue Then
   iColor = 70 'hypothetical green value
ElseIf inColor = Red Then
   iColor = 40 'hypothetical green value
End If

m_Spirit.If SENSOR, m_SensorNumber, EQUALS, CONSTANT, iColor
End Sub
=-=-=-= END POSSIBLE EXTENTION =-=-=-=

The code calculates a light sensor value based on the passed parameter and
starts an if statement in the RCX to look for that color. You'd use the code
like this:

=-=-=-= BEGIN EXAMPLE OF USE =-=-=-=
Dim snsrLightSensor1 As New LightSensor

snsrLightSensor1.SetSpirit MySiritObj
snsrLightSensor1.SetupSensor SensorNumbers.Sensor1, SensorModes.Raw, 0
snsrLightSensor1.IfSeeColor Green Then
   'Code to handle green color goes here.
m_Spirit.EndIf
=-=-=-= END EXAMPLE OF USE =-=-=-=

Now this code is simplified, you'd need to add a calibration routine to handle
different lighting, and so on. But it gets the idea across.

Yet another advantage to the object-oriented method is you can encapsulate
specialized knowledge of you code in the class, freeing the programmer from
having to learn, remember, and use that knowledge. For example, in my
SoftBricks prototype I made four mapping functions RCX subroutines. This was
because they were too large to use as inline code. Now if you're not using
mapping, you can rip those subroutines out because you won't need them. This
saves a significant amount of RCX program memory. But the programmer has too
KNOW they can be ripped out, know what subroutines to rip out, and remember to
put them back in the right place if he later adds mapping. With the OO method,
all that knowledge is in the mapping classes, so if you're not using mapping,
those subroutines are not loaded to the RCX. If you later add mapping, those
subroutines get loaded. It makes the library easier to use, smaller on the RCX
side, and removes a big potential for coding errors.

David Leeper (likes OO)

In lugnet.robotics.rcx, Eric Huang writes:
What I mean is to think the RCX in OO. For example, I can add a sensor
object in my programming code when I plug a sensor into my RCX. I can make
the sensor object to interface with the RCX object in the OO manner. Is that
possible?
I learned some control algorithm is object oriented. But I have no idea to
apply it to RCX. Any comments?

fun,
Eric
----- Original Message -----
From: Matthew Miller <mattdm@mattdm.org>
To: <lugnet.robotics.rcx@lugnet.com>
Sent: Tuesday, January 11, 2000 12:43 AM
Subject: Re: OO programing in RCX


David Leeper <david.leeper@destiny.com> wrote:
There always Visual Basic. I'm currently building a few ActiveX controls • which
sit on top of Spirit. So far its working nicely and is a big improvement • over
the non-class method of VB coding I used in my original prototype.

I'm curious -- are you making it so you can think about your RCX program • in
OO terms, or just the VB program that sends opcodes?

--
Matthew Miller                      ---> • mattdm@mattdm.org
Quotes 'R' Us                       ---> • http://quotes-r-us.org/




Message has 1 Reply:
  Re: OO programing in RCX
 
Thank you, David. Your gode is great! I learned a lot from your quick example, though I prefer Java more. I will encapsulate RCX commands in some utility objects and use them in more higher level objects. What I focus now is make a RF/IR adapter to (...) (25 years ago, 12-Jan-00, to lugnet.robotics.rcx)

Message is in Reply To:
  Re: OO programing in RCX
 
What I mean is to think the RCX in OO. For example, I can add a sensor object in my programming code when I plug a sensor into my RCX. I can make the sensor object to interface with the RCX object in the OO manner. Is that possible? I learned some (...) (25 years ago, 11-Jan-00, to lugnet.robotics.rcx)

14 Messages in This Thread:




Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR