Shop OBEX P1 Docs P2 Docs Learn Events
Thread Like Operation in Javelin Multi Sensor Acquistion — Parallax Forums

Thread Like Operation in Javelin Multi Sensor Acquistion

dkwondkwon Posts: 13
edited 2005-10-27 10:46 in General Discussion
Hello

I am using several sensors such as two compasses, two accelerometers, force sensors, etc.

Each sensor acquistion module is designed as class. When I get the data, the speed is quite slower.
Do I have to use VP or Timer or Task things?

Would you give me the best way to handle Thread like operation for javelin sensor acquistion?

I am doing like this.

Accel accel1 = new Accel();
Accel accel2 = new Accel();
Compass cmps1 = new Compass();
Compass cmps2 = new Compass();

while(true)
{
· accel1.getData();
· accel2.getData();
· cmps1.getData();
· cmps2.getData();
}

Best Regards,

Doo Young Kwon

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-10-25 14:27
    You already have the fastest while loop, so if things are slow,

    the problem is within the classes themselves.

    Can you post these classes?

    regards peter
  • dkwondkwon Posts: 13
    edited 2005-10-26 11:28
    Hello Peter

    Here is my code. You will notice that I am sending the data using "sendByte" function without using shift. So they do not work properly. I am working on that following your comment in another post. The main question was asking whehter i should install VP or timer for each acquisition class.

    In addition, In parallel, I am computing pitch, yaw, roll value using two compass and two accels by attaching perpendiculary. Since I am really beginner regarding Basic Stamp Bit Math, I am still working on it. If you have a good source, it will be enormous help for me.

    Thanks alot.

    //////////////////////////////////////
    // ACSensor.java (main)
    //////////////////////////////////////

    /**
    *
    *
    * Sensor:
    * Two two-axis accelerometers (Memsic2125)
    * Two two-axis digital compass (Hm55bCompass)
    * <P><U> Pin Arrangement </U> <BR>
    *
    * Pin 8 (Flat Accelerometer: X-Axis Output) <BR>
    * Pin 9 (Flat Accelerometer: Y-Axis Output) <BR>

    * Pin 11 (Vertical Acceleromter: X-Axis Output) <BR>
    * Pin 12 (Vertical Acceleromter: Y-Axis Output) <BR>
    *
    * Pin 6 (Flat Magnetometer: to/from Din/Dout) <BR>
    * Pin 5 (Flat Magnetometer: Clk) <BR>
    * Pin 4 (Flat Magnetometer: EN) <BR>

    * Pin 13 (Vertical Magnetometer: to/from Din/Dout) <BR>
    * Pin 5 (Vertical Magnetometer: Clk) <BR>
    * Pin 4 (Vertical Magnetometer: EN) <BR>

    * @version - 1.00 Initial test file
    * @author - Doo Young Kwon
    */

    import stamp.core.*;
    import stamp.peripheral.mems.Accel;
    import stamp.peripheral.mems.Compass;

    public class ACSensor
    {
    final static int txpin = CPU.pin0;
    static Uart txUart = new Uart(Uart.dirTransmit,txpin,Uart.dontInvert,Uart.speed9600,Uart.stop1);

    static int[noparse]/noparse bufsend = new int[noparse][[/noparse]7];
    static int index = 0;

    public static void main()
    {
    Accel faccel = new Accel(8, 9); // x pin, y pin
    Accel vaccel = new Accel(11, 12);
    Compass fcmps = new Compass(6, 5, 4); // dbinout pin, clk pin, en pin
    Compass vcmps = new Compass(13, 5, 4);

    while(true)
    {
    bufsend[noparse][[/noparse]0] = faccel.getAccelX(); // x - axis
    bufsend[noparse][[/noparse]1] = faccel.getAccelY(); // y - axis
    bufsend[noparse][[/noparse]2] = vaccel.getAccelX(); // z -axis


    fcmps.runCompass();
    bufsend[noparse][[/noparse]3] = fcmps.getCompassX(); // x - axis
    bufsend[noparse][[/noparse]4] = fcmps.getCompassY(); // y - axis
    vcmps.runCompass();
    bufsend[noparse][[/noparse]5] = vcmps.getCompassX(); // z -axis

    index = 0;
    while (index < 6)
    {
    txUart.sendByte(bufsend[noparse][[/noparse]index++]);
    }
    }
    }
    }

    //////////////////////////////////////
    // Accel.java (Accelerometer class)
    //////////////////////////////////////

    package stamp.peripheral.mems;
    import stamp.core.*;

    public class Accel {

    private int m_pinX;
    private int m_pinY;

    private int m_ctime;
    private static int m_timeout;

    public Accel(int pinX, int pinY)
    {
    m_timeout = 2000;
    m_pinX = CPU.pins[noparse][[/noparse] pinX ];
    m_pinY = CPU.pins[noparse][[/noparse] pinY ];
    }

    private void setCycleTime( )
    {
    m_ctime = (CPU.pulseIn(m_timeout, m_pinX, true) +
    CPU.pulseIn(m_timeout, m_pinX, false)) ;
    }

    public int getAccelX()
    {
    return CPU.pulseIn(m_timeout, CPU.pin8, true);
    }

    public int getAccelY()
    {
    return CPU.pulseIn(m_timeout, CPU.pin9, true);
    }

    }

    //////////////////////////////////////
    // Compass.java (Compass class)
    //////////////////////////////////////

    package stamp.peripheral.mems;
    import stamp.core.*;
    import stamp.math.IntegerMath;

    public class Compass {

    private static int m_DinDout;
    private static int m_Clk;
    private static int m_En;

    private static final int RESET = 0x0000;
    private static int m_Measure = 0x0008;
    private static int m_Report = 0x000c;
    private static final int READY = 0x000c;

    private int m_x;
    private int m_y;
    private int m_status;
    private int m_angle;

    public Compass(int DinDout, int Clk, int En) {
    m_DinDout = CPU.pins[noparse][[/noparse] DinDout ]; ;
    m_Clk = CPU.pins[noparse][[/noparse] Clk ];
    m_En = CPU.pins[noparse][[/noparse] En ];
    }
    public int getCompassX()
    {
    return m_x;
    }

    public int getCompassY()
    {
    return m_y;
    }

    public int getAngle(){

    m_angle = IntegerMath.atan2(-m_y, m_x);

    return m_angle;
    }


    public void runCompass(){
    CPU.writePin(m_Clk, false);
    CPU.writePin(m_En, true);
    CPU.writePin(m_En, false);
    CPU.shiftOut(m_DinDout, m_Clk, 4, CPU.SHIFT_MSB,RESET);

    CPU.writePin(m_En, true);
    CPU.writePin(m_En, false);
    CPU.shiftOut(m_DinDout, m_Clk, 4, CPU.SHIFT_MSB, m_Measure << 12);

    do {
    CPU.writePin(m_En, true);
    CPU.writePin(m_En, false);
    CPU.shiftOut(m_DinDout, m_Clk, 4, CPU.SHIFT_MSB, m_Report << 12);
    m_status = CPU.shiftIn(m_DinDout, m_Clk, 4, CPU.POST_CLOCK_MSB );
    } while (m_status != READY);

    m_x = CPU.shiftIn(m_DinDout, m_Clk, 11, CPU.POST_CLOCK_MSB );
    m_y = CPU.shiftIn(m_DinDout, m_Clk, 11, CPU.POST_CLOCK_MSB );

    CPU.writePin(m_En, true);

    if ((m_x & 0x0400) != 0) m_x |= (short)0xF800;
    if ((m_y & 0x0400) != 0) m_y |= (short)0xF800;
    }
    }
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-10-27 08:06
    I changed your classes a little.
    First, I don't think you can share the compass pins clk and en, because both
    compasses must be read out individually. So I changed it so that the compasses
    share their datapin but not clk and en pins.

    Secondly, I changed the way you store passed pins to constructors.
    Instead of using pins[noparse]/noparse I simply store the pins that are now defined in your main
    class as constants. Pins[noparse]/noparse may not be initialized (in CPU class) properly when
    the constructor is called because it is not known when the CPU class is initialized,
    unless you do all variable initialization in your main(), as you did.
    No need for that·now, variables are neatly declared outside main() which
    gives a better overview I think.

    Thirdly, I split up your runCompass method in 3 methods: reset(), start() and poll().
    This allows you to start a measurement, then while polling you can do other tasks as
    well. In the main·class I read in the accelerometer values while the compasses are
    progressing their measurement. This should speed up things·a little.

    Let us know the results.

    regards peter


    Post Edited (Peter Verkaik) : 10/27/2005 8:54:50 AM GMT
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-10-27 08:38
    I added some printouts to the ide message window.

    I use Format.printf() for easy printing.

    Format class can be found here:

    http://groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/util/text/

    I also noted you only send the lowbytes from the int array bufsend.

    To send all bytes use

    int value = bufsend[noparse][[/noparse]index++];

    txUart.sendByte(value);

    txUart.sendByte(value>>>8);

    inside that while loop.

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-10-27 09:13
    I have put in some statistics for your compasses.

    It shows how many times you must poll before the compasses have new data.

    Ideally it should be 1, then the new data is available after the first poll.

    If it is larger than 1, you get an idea of the time it takes for the compasses

    to complete their measurement, and thus how much time you have

    to perform additional tasks while the compasses are progressing the measurements.

    regards peter




    Post Edited (Peter Verkaik) : 10/27/2005 9:44:40 AM GMT
  • dkwondkwon Posts: 13
    edited 2005-10-27 10:46
    Thanks Peter,
    I will post the testing result soon.
Sign In or Register to comment.