JBED: Java for Real-Time Systems
by Jörgen Tryggvesson, Torbjörn Mattsson, and Hansruedi Heeb 


Listing One
public class Task extends java.lang.Thread {
    public static final int timeslice;
    public Task (java.lang.Runnable target, long duration,long allowance,
        long deadline, RealtimeEvent event) throws AdmissionFailure;
 ...


}
Listing Two
// some real-time constructor is called:
com.jbed.tasks.RealtimeEvent event = new com.jbed.tasks....(...);
// some object implementing the java.lang.Runnable interface is created:
java.lang.Runnable target = ... ;
com.jbed.tasks.Task task;
try {
    task = new com.jbed.tasks.Task(target, duration,
        allowance, deadline, event);
}
catch (com.jbed.tasks.AdmissionFailure e) {
    ...
}
task.start;


Listing Three
public class LPointer {

    // the two clocks in ms:
    static final int ControllerClock = 5;
    static final int PlannerClock = 10;

    // the calculation parameters:
    static final float Kp0 = 500.0F, Kp1 = 600.0F;
    static final float Kd0 = 35.0F, Kd1 = 10.0F;

    // the Lissajou parameters:
    static float    a0 = 0.05F, a1 = 0.05F;
    static float    f0 = 12.56F, f1 = 6.28F;
    static float    fv0 = 0.0F, fv1 = 0.0F;

    // actual positions and previous positions:
    static float q0actual, q1actual, q0old , q1old;

    // the three tasks:
    static com.jbed.tasks.Task  controllerTask, plannerTask, watchdogTask;

    // the periphery (actuators and counters):
    static com.jbed.peripherals.AnalogActuator out0, out1;
    static com.jbed.peripherals.Counter counter0, counter1;

    // the desired positions:
    static float q0desired, q1desired;

    // the time:
    static float t=0;

    //...

    static void init() {
       //...
       try {
          // Configuration
          float gear = -100F;
          float nmPerAmp = 0.05F;
          float maxAmp = 6.0F;
          float maxNm = maxAmp * nmPerAmp * gear;
          float scale = 2 * maxNm / 4096;

          // define the scale for the actuators:
          com.jbed.drivers.LinearScale.defLinearScale("DAScale",   0, 4095, scale, maxNm);
          drivers.demo.DAC.defModule("DACMod", 0x0FFFF8100);
          drivers.demo.DAC.defChannel("A0", "DACMod", "DAScale", 3);
          drivers.demo.DAC.defChannel("A1", "DACMod", "DAScale", 4);

          // define the scale for the input counters:
          float counterConst = 2000; // = 4 * 500
          scale = 2 * com.jbed.math.FMath.PI / (counterConst * gear);
          com.jbed.drivers.LinearScale.defLinearScale(
              "EncScale", Integer.MIN_VALUE, Integer.MAX_VALUE, scale, 0F);

          drivers.demo.Enc6.defModule("Enc6Mod", 0x0FFFF8300);
         drivers.demo.Enc6.defChannel("Counter0", "Enc6Mod", "EncScale", 3);
          drivers.demo.Enc6.defChannel("Counter1", "Enc6Mod", "EncScale", 4);

          // Initialization
          out0 = new com.jbed.peripherals.AnalogActuator("A0");
          out1 = new com.jbed.peripherals.AnalogActuator("A1");
          counter0 = new com.jbed.peripherals.Counter("Counter0");
          counter1 = new com.jbed.peripherals.Counter("Counter1");
          //...
      } catch (Exception e) {
                    //...
     }
            // ...
   }
  //...
}
Listing Four
static class Planner extends com.jbed.tasks.Task {
    public Planner(long duration, long allowance, long deadline,
                        jbed.tasks.RealtimeEvent event)
        throws com.jbed.tasks.AdmissionFailure  {
        super (duration, allowance, deadline, event);
    }
    public final void run() {
        q0desired = (a0*com.jbed.math.FMath.sin(f0*t+fv0));
        q1desired = (a1*com.jbed.math.FMath.sin(f1*t+fv1));
        t = t+(float)(PlannerClock/1000.0F);
    }
}


Listing Five
static class Control extends com.jbed.tasks.Task {
    public Control(long duration, long allowance, long deadline,
                        jbed.tasks.RealtimeEvent event)
                        throws com.jbed.tasks.AdmissionFailure  {
        super (duration, allowance, deadline, event);
    }
    public final void run() {
        float tau0, tau1;
        float dpos0, dpos1;

        q0actual = counter0.read();
        q1actual = counter1.read();
        dpos0 = q0desired - q0actual;
        dpos1 = q1desired - q1actual;

        tau0 = Kp0*dpos0-Kd0*
                ((q0actual-q0old)/(float)(ControllerClock/1000.0F));
        tau1 = Kp1*dpos1-Kd1*
                ((q1actual-q1old)/(float)(ControllerClock/1000.0F));
        out0.write(tau0);  out1.write(tau1);
        q0old = q0actual; q1old = q1actual;
    }
}


Listing Six
com.jbed.tasks.RealtimeEvent periodicEvent =
    new com.jbed.tasks.PeriodicTimer(ControllerClock*1000);
controllerTask = new Control(2000, 0, ControllerClock*1000, periodicEvent);

controllerTask.start();

com.jbed.tasks.RealtimeEvent harmonicEvent =
    new com.jbed.tasks.HarmonicEvent(controllerTask, 2);
plannerTask = new Planner(1000, 0, 10000, harmonicEvent);
plannerTask.start();


Listing Seven
001F0F66
          MOVEA.L   (-16, A6), A0
001F0F6A  TST.L     A0
001F0F6C  TRAPEQ
001F0F6E  MOVEA.L   (12, A0), A0
001F0F72  MOVE.L    (-8, A6), D1
001F0F76  ADD.L     (-20, A6), D1
001F0F7A  MOVEA.L   D1, A1
001F0F7C  MOVE.L    (-20, A6), D0
001F0F80  TST.L     A0
001F0F82  TRAPEQ
001F0F84  CMP.L     (12, A0), D0
001F0F88  TRAPCC
001F0F8A  MOVE.B    (-3834, A1), (16, A0, D0.L*1)
001F0F90  ADDQ.L    #1, (-20, A6)
001F0F94  CMPI.L    #$00000008, (-20, A6)
001F0F9C  BLT       $001F0F66
Listing Eight
public class ByteRegisterField
{
    private int m_mask;
    private int m_base;
    private int m_pos=0;

    private int m_maxval;
    private static IllegalArgumentException IllegalArgument =
                                        new IllegalArgumentException();
    public ByteRegisterField(int base_adress, int mask)
    {
        m_mask=mask;
        m_base=base_adress;
        //check to see that the mask is in the register and nonzero
        if (m_mask>0xFF||m_mask<=0)
            throw IllegalArgument;
        //Find the lowest bit in the mask
        int i=1;
        while((m_mask&i)==0)
        {
            i<<=1;
            m_pos++;
        }
        m_maxval=m_mask>>>m_pos;
        //check to see if the mask is without holes;
        if(((m_maxval+1)&m_maxval)!=0)
            throw IllegalArgument;
    }
    public void Set(int value)
    {
        //instruct the compiler to turn of safety checks
        com.jbed.runtime.Unsafe.option(com.jbed.runtime.Unsafe.noChecks);
          //check that the value is valid
          if ((value > m_maxval) ||value <0)
               throw IllegalArgument;
          Unsafe.getByte(m_base) & ~m_mask | (value << m_pos)));
    }
    public int Get()
    {
      //instruct the compiler to turn of safety checks
      com.jbed.runtime.Unsafe.option(com.jbed.runtime.Unsafe.noChecks );
      return (com.jbed.runtime.Unsafe.getByte(m_base) & m_mask)>>>m_pos;
    }
}







