Passing variables and arrays between classes - how???

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: roger, 99jonathan, imaqine

Passing variables and arrays between classes - how???

Postby moosooboo » Wed Feb 13, 2013 1:57 pm

Hi
I'm trying to write a simple benchmarking program to compare a few NXT firmwares, and I'm pretty new to leJOS so it is proving to be a bit of a struggle.

Question1: How do I pass variables and arrays between classes? (I think classes is the right word, too many new words from C to Java and right now it is getting worse as I read more tutorials:-(. I need to be able to pass variables/flags and data arrays between tasks.)

My leJOS code so far is below, I've based it on some code from the leJOS ebook by Juan Antonio Breña Moral. My console log is below that, and the current code throws an error exception 16 at 38:157, which I think means thread death in the ReadSensorTask.

Question2: What is wrong with my current code?

Thanks in advance, I really want to get this working as despite my initial misgivings about writing in Java, I'm liking leJOS!

Mike


Code: Select all
import lejos.nxt.*;

public class MainTask {
   private static ReadSensorTask rst;
   //private static DriveMotorTask dmt;
   //private static BluetoothCommsTask bct;
   private static DataExchange de;
   private static AddItUp aiu;


   public static void main(String[] args) {
      rst = new ReadSensorTask(de);
      //dmt = new DriveMotorTask();
      //bct = new BluetoothCommsTask();
      de = new DataExchange();
      aiu = new AddItUp(de);
      
      aiu.setDaemon(true);
      rst.setDaemon(true);
      //dmt.setDaemon(true);
      //bct.setDaemon(true);

      aiu.start();
      rst.start();
      // dmt.start();
      //bct.start();

      while (!Button.ESCAPE.isDown()) {
      }
      LCD.drawString("Finished",0, 7);
      System.exit(0);
   }
}



Code: Select all
// few loops filling arrays with sensor data, arrays to be used by main task
import lejos.nxt.*;

public class ReadSensorTask extends Thread {
   DataExchange deObj;

   UltrasonicSensor sonic = new UltrasonicSensor(SensorPort.S1);
   int [] int_array = new int[100];
   int [] float_array = new int[100];
   int i;
   int j;
   final int number_task_loops = 5;
   int sensor_value;


   public ReadSensorTask(DataExchange de) {
      deObj = de;
   }

   public void run() {   

      // Read sensor values, write an int array.
      for (int j = 0; j <= number_task_loops; j++) {      
         for (int i = 0; i < int_array.length; i++) {
            LCD.drawString("Sensor int:", 0, 0);
            sensor_value = sonic.getDistance();
            int_array[i] = sensor_value;
            LCD.drawInt(sensor_value, 0, 1);
            LCD.drawInt(i, 0, 2);
            LCD.refresh();
         }
      }
      //Read sensor value, write a float array.
      for (int j = 0; j <= number_task_loops; j++) {
         for (int i = 0; i < float_array.length; i++) {
            LCD.drawString("Sensor float:", 0, 0);
            sensor_value = sonic.getDistance();
            float_array[i] = sensor_value;
            LCD.drawInt(sensor_value, 0, 1);
            LCD.drawInt(i, 0, 2);
            LCD.refresh();
         }
      }
      // set flag to show task finished
      deObj.setis_sensor_task_finished(1);
   }
}


Code: Select all
import lejos.nxt.*;

public class AddItUp extends Thread {
   DataExchange deObj;

   public AddItUp(DataExchange de) {
      deObj = de;
   }

   public void run() {

      // set flag to show task finished
      deObj.setis_sensor_task_finished(0);

      // start timer

      // wait for read sensor task finished flag
      while (deObj.getis_sensor_task_finished() == 0) {
         // wait
         LCD.drawString("Wait:", 0, 4);
      }

      // stop/read timer

      // sum arrays and print values and times

      // exit??

   }

}


Code: Select all
// class to handle exchange of data between tasks.
// will need to exchange:
// 1. a flag for when one of the tasks is finished
// 2. an int array from readsensor task
// 3. a float array from readsensor task
// 4. an int array from bluetoothcomms task


public class DataExchange {
   
   // flag for a task running or finished
   private int is_sensor_task_finished = 0;
   
   public DataExchange() {
   }

   /*
    * Getters & Setters
    */
   public void setis_sensor_task_finished(int status) {
      is_sensor_task_finished = status;
   }

   public int getis_sensor_task_finished() {
      return is_sensor_task_finished;
   }
}


The log:
Code: Select all
Found NXT: NXT 00165317ACC9
leJOS NXJ> Upload successful in 2079 milliseconds
program has been uploaded
Linking ...
Class 0: java.lang.Object
Class 1: java.lang.Throwable
Class 2: java.lang.Error
Class 3: java.lang.OutOfMemoryError
Class 4: boolean
Class 5: char
Class 6: float
Class 7: double
Class 8: byte
Class 9: short
Class 10: int
Class 11: long
Class 12: void
Class 13: java.lang.Object[]
Class 14: java.lang.NoSuchMethodError
Class 15: java.lang.StackOverflowError
Class 16: java.lang.NullPointerException
Class 17: boolean[]
Class 18: char[]
Class 19: float[]
Class 20: double[]
Class 21: byte[]
Class 22: short[]
Class 23: int[]
Class 24: long[]
Class 25: reserved
Class 26: java.lang.ClassCastException
Class 27: java.lang.ArithmeticException
Class 28: java.lang.ArrayIndexOutOfBoundsException
Class 29: java.lang.IllegalArgumentException
Class 30: java.lang.InterruptedException
Class 31: java.lang.IllegalStateException
Class 32: java.lang.IllegalMonitorStateException
Class 33: java.lang.ThreadDeath
Class 34: java.lang.ArrayStoreException
Class 35: java.lang.NegativeArraySizeException
Class 36: java.lang.Class
Class 37: java.lang.String
Class 38: java.lang.Cloneable
Class 39: java.lang.Thread
Class 40: MainTask
Class 41: AddItUp
Class 42: ReadSensorTask
Class 43: lejos.nxt.SensorPort
Class 44: lejos.nxt.I2CPort
Class 45: lejos.nxt.VM
Class 46: java.lang.System
Class 47: java.lang.StringBuilder
Class 48: java.lang.Integer
Class 49: java.io.PrintStream
Class 50: lejos.nxt.LCD
Class 51: lejos.nxt.MotorPort
Class 52: lejos.nxt.Button
Class 53: java.lang.Runtime
Class 54: java.lang.RuntimeException
Class 55: java.lang.Exception
Class 56: java.lang.IndexOutOfBoundsException
Class 57: java.lang.Character
Class 58: java.lang.StringIndexOutOfBoundsException
Class 59: java.lang.StringUtils
Class 60: java.lang.Runnable
Class 61: java.lang.Thread.UncaughtExceptionHandler
Class 62: DataExchange
Class 63: lejos.nxt.VM.VMImage
Class 64: lejos.nxt.VM.VMClone
Class 65: lejos.nxt.LCDOutputStream
Class 66: java.lang.Number
Class 67: java.lang.NumberFormatException
Class 68: java.io.OutputStream
Class 69: java.io.IOException
Class 70: lejos.util.Delay
Class 71: lejos.nxt.NXTEvent
Class 72: lejos.nxt.Sound
Class 73: lejos.nxt.SystemSettings
Class 74: lejos.nxt.Button[]
Class 75: java.lang.Shutdown
Class 76: lejos.nxt.UltrasonicSensor
Class 77: lejos.nxt.Flash
Class 78: java.lang.String[]
Class 79: lejos.nxt.I2CSensor
Class 80: lejos.nxt.SensorPort.SensorReader
Class 81: lejos.nxt.SensorPort.StandardReader
Class 82: lejos.nxt.SensorPort[]
Class 83: lejos.nxt.FlashError
Class 84: lejos.nxt.NXT
Class 85: lejos.nxt.BasicSensorPort
Method 0: java.lang.Object.<init>() PC 4424 Signature id 2
Method 1: java.lang.Object.getClass() PC 4425 Signature id 125
Method 2: java.lang.Object.wait(long) Native id 8
Method 3: java.lang.Throwable.<init>() PC 4433 Signature id 2
Method 4: java.lang.Throwable.<init>(java.lang.String) PC 4443 Signature id 129
Method 5: java.lang.Throwable.getCause() PC 4458 Signature id 133
Method 6: java.lang.Throwable.fillInStackTrace() PC 4463 Signature id 136
Method 7: java.lang.Throwable.displayLocation(int, int, int) PC 4476 Signature id 139
Method 8: java.lang.Throwable.uncaughtException(int, int) PC 4520 Signature id 140
Method 9: java.lang.Error.<init>(java.lang.String) PC 4805 Signature id 129
Method 10: java.lang.NullPointerException.<init>() PC 4811 Signature id 2
Method 11: java.lang.IllegalArgumentException.<init>() PC 4816 Signature id 2
Method 12: java.lang.IllegalArgumentException.<init>(java.lang.String) PC 4821 Signature id 129
Method 13: java.lang.IllegalStateException.<init>(java.lang.String) PC 4827 Signature id 129
Method 14: java.lang.String.<init>(int) PC 4833 Signature id 141
Method 15: java.lang.String.<init>(char[]) PC 4845 Signature id 162
Method 16: java.lang.String.<init>(char[], int, int) PC 4854 Signature id 163
Method 17: java.lang.String.charAt(int) PC 4871 Signature id 167
Method 18: java.lang.String.equals(java.lang.Object) PC 4896 Signature id 123
Method 19: java.lang.String.length() PC 4968 Signature id 192
Method 20: java.lang.Thread.init(java.lang.String, java.lang.Runnable) PC 4974 Signature id 219
Method 21: java.lang.Thread.<init>() PC 5017 Signature id 2
Method 22: java.lang.Thread.run() PC 5029 Signature id 1
Method 23: java.lang.Thread.start() Native id 11
Method 24: java.lang.Thread.sleep(long) Native id 13
Method 25: java.lang.Thread.currentThread() Native id 14
Method 26: java.lang.Thread.getPriority() Native id 15
Method 27: java.lang.Thread.setPriority(int) Native id 16
Method 28: java.lang.Thread.interrupt() Native id 17
Method 29: java.lang.Thread.isDaemon() Native id 21
Method 30: java.lang.Thread.setDaemon(boolean) Native id 20
Method 31: java.lang.Thread.getUncaughtExceptionHandler() PC 5049 Signature id 227
Method 32: java.lang.Thread.systemUncaughtExceptionHandler(java.lang.Throwable, int, int) PC 5054 Signature id 4
Method 33: java.lang.Thread.exitThread() Native id 24
Method 34: MainTask.main(java.lang.String[]) PC 5115 Signature id 0
Method 35: AddItUp.<init>(DataExchange) PC 5199 Signature id 454
Method 36: AddItUp.run() PC 5209 Signature id 1
Method 37: ReadSensorTask.<init>(DataExchange) PC 5238 Signature id 454
Method 38: ReadSensorTask.run() PC 5283 Signature id 1
Method 39: lejos.nxt.SensorPort.<clinit> PC 5444 Signature id 3
Method 40: lejos.nxt.SensorPort.<init>(int) PC 5520 Signature id 141
Method 41: lejos.nxt.SensorPort.reset() PC 5591 Signature id 627
Method 42: lejos.nxt.SensorPort.setType(int) PC 5660 Signature id 634
Method 43: lejos.nxt.SensorPort.setPowerType(int) PC 5948 Signature id 641
Method 44: lejos.nxt.SensorPort.setPowerTypeById(int, int) Native id 34
Method 45: lejos.nxt.SensorPort.i2cEnableById(int, int) Native id 35
Method 46: lejos.nxt.SensorPort.i2cStartById(int, int, byte[], int, int, int) Native id 38
Method 47: lejos.nxt.SensorPort.i2cCompleteById(int, byte[], int, int) Native id 37
Method 48: lejos.nxt.SensorPort.i2cEnable(int) PC 5957 Signature id 642
Method 49: lejos.nxt.SensorPort.i2cTransaction(int, byte[], int, int, byte[], int, int) PC 6011 Signature id 648
Method 50: lejos.nxt.SensorPort.setSensorPinMode(int, int, int) Native id 42
Method 51: lejos.nxt.SensorPort.setSensorPin(int, int, int) Native id 40
Method 52: lejos.nxt.SensorPort.setSensorPinMode(int, int) PC 6129 Signature id 649
Method 53: lejos.nxt.SensorPort.setSensorPin(int, int) PC 6139 Signature id 650
Method 54: lejos.nxt.VM.<clinit> PC 6149 Signature id 3
Method 55: lejos.nxt.VM.<init>() PC 6168 Signature id 2
Method 56: lejos.nxt.VM.getVM() PC 6203 Signature id 228
Method 57: lejos.nxt.VM.memPeek(int, int, int) Native id 113
Method 58: lejos.nxt.VM.memCopy(java.lang.Object, int, int, int, int) Native id 114
Method 59: lejos.nxt.VM.getObjectAddress(java.lang.Object) Native id 115
Method 60: lejos.nxt.VM.memGetReference(int, int) Native id 116
Method 61: lejos.nxt.VM.memPeekByte(int, int) PC 6223 Signature id 229
Method 62: lejos.nxt.VM.memPeekShort(int, int) PC 6235 Signature id 230
Method 63: lejos.nxt.VM.memPeekInt(int, int) PC 6246 Signature id 231
Method 64: lejos.nxt.VM.getClassAddress(int) PC 6254 Signature id 233
Method 65: lejos.nxt.VM.getClassNo(java.lang.Object) PC 6270 Signature id 234
Method 66: lejos.nxt.VM.getClassNumber(java.lang.Class) PC 6297 Signature id 235
Method 67: lejos.nxt.VM.getClass(java.lang.Object) PC 6314 Signature id 237
Method 68: lejos.nxt.VM.suspendThread(java.lang.Object) Native id 86
Method 69: lejos.nxt.VM.resumeThread(java.lang.Object) Native id 87
Method 70: lejos.nxt.VM.createStackTrace(java.lang.Thread, java.lang.Object) Native id 32
Method 71: lejos.nxt.VM.firmwareExceptionHandler(java.lang.Throwable, int, int) Native id 118
Method 72: lejos.nxt.VM.access$100(java.lang.Object, int, int, int, int) PC 6330 Signature id 250
Method 73: java.lang.System.<clinit> PC 6340 Signature id 3
Method 74: java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int) Native id 26
Method 75: java.lang.System.exit(int) PC 6375 Signature id 260
Method 76: java.lang.System.currentTimeMillis() Native id 25
Method 77: java.lang.StringBuilder.ensureCapacity(int) PC 6383 Signature id 267
Method 78: java.lang.StringBuilder.<init>() PC 6442 Signature id 2
Method 79: java.lang.StringBuilder.append(java.lang.String) PC 6460 Signature id 271
Method 80: java.lang.StringBuilder.append(int) PC 6466 Signature id 280
Method 81: java.lang.StringBuilder.appendInternal(java.lang.String) PC 6504 Signature id 285
Method 82: java.lang.StringBuilder.toString() PC 6555 Signature id 127
Method 83: java.lang.Integer.<clinit> PC 6572 Signature id 3
Method 84: java.lang.Integer.parseInt(java.lang.String) PC 6577 Signature id 305
Method 85: java.lang.Integer.parseInt(java.lang.String, int) PC 6584 Signature id 306
Method 86: java.lang.Integer.parseInt(java.lang.String, int, int, boolean, int) PC 6631 Signature id 307
Method 87: java.io.PrintStream.<init>(java.io.OutputStream) PC 6756 Signature id 325
Method 88: lejos.nxt.LCD.<clinit> PC 6766 Signature id 3
Method 89: lejos.nxt.LCD.bitBlt(byte[], int, int, int, int, int, int, int, int, int) PC 6783 Signature id 349
Method 90: lejos.nxt.LCD.drawString(java.lang.String, int, int) Native id 46
Method 91: lejos.nxt.LCD.drawInt(int, int, int) Native id 47
Method 92: lejos.nxt.LCD.drawInt(int, int, int, int) Native id 48
Method 93: lejos.nxt.LCD.asyncRefresh() Native id 49
Method 94: lejos.nxt.LCD.getRefreshCompleteTime() Native id 56
Method 95: lejos.nxt.LCD.asyncRefreshWait() PC 6810 Signature id 353
Method 96: lejos.nxt.LCD.refresh() PC 6829 Signature id 354
Method 97: lejos.nxt.LCD.clear() Native id 50
Method 98: lejos.nxt.LCD.getDisplay() Native id 51
Method 99: lejos.nxt.LCD.getSystemFont() Native id 54
Method 100: lejos.nxt.LCD.setAutoRefreshPeriod(int) Native id 52
Method 101: lejos.nxt.LCD.setAutoRefresh(boolean) PC 6842 Signature id 355
Method 102: lejos.nxt.LCD.bitBlt(byte[], int, int, int, int, byte[], int, int, int, int, int, int, int) Native id 53
Method 103: lejos.nxt.LCD.clear(int) PC 6862 Signature id 360
Method 104: lejos.nxt.MotorPort.<clinit> PC 6883 Signature id 3
Method 105: lejos.nxt.MotorPort.<init>(int) PC 6917 Signature id 141
Method 106: lejos.nxt.MotorPort.getInstance(int) PC 6932 Signature id 361
Method 107: lejos.nxt.MotorPort.controlMotor(int, int) PC 6982 Signature id 362
Method 108: lejos.nxt.MotorPort.controlMotorById(int, int, int) Native id 60
Method 109: lejos.nxt.Button.<clinit> PC 7032 Signature id 3
Method 110: lejos.nxt.Button.<init>(int) PC 7122 Signature id 141
Method 111: lejos.nxt.Button.isDown() PC 7132 Signature id 368
Method 112: lejos.nxt.Button.waitForAnyPress(int) PC 7149 Signature id 374
Method 113: lejos.nxt.Button.waitForAnyPress() PC 7292 Signature id 375
Method 114: lejos.nxt.Button.getButtons() Native id 58
Method 115: lejos.nxt.Button.readButtons() PC 7297 Signature id 377
Method 116: lejos.nxt.Button.loadSystemSettings() PC 7346 Signature id 386
Method 117: java.lang.Runtime.<init>() PC 7427 Signature id 2
Method 118: java.lang.Runtime.getRuntime() PC 7432 Signature id 263
Method 119: java.lang.Runtime.exit(int) PC 7452 Signature id 260
Method 120: java.lang.RuntimeException.<init>() PC 7457 Signature id 2
Method 121: java.lang.RuntimeException.<init>(java.lang.String) PC 7462 Signature id 129
Method 122: java.lang.Exception.<init>() PC 7468 Signature id 2
Method 123: java.lang.Exception.<init>(java.lang.String) PC 7473 Signature id 129
Method 124: java.lang.IndexOutOfBoundsException.<init>(java.lang.String) PC 7479 Signature id 129
Method 125: java.lang.Character.<clinit> PC 7485 Signature id 3
Method 126: java.lang.Character.digit(int, int) PC 7490 Signature id 324
Method 127: java.lang.Character.forDigit(int, int) PC 7568 Signature id 415
Method 128: java.lang.StringIndexOutOfBoundsException.<init>(int) PC 7615 Signature id 141
Method 129: java.lang.StringUtils.parseDigit(char, int) PC 7639 Signature id 436
Method 130: java.lang.StringUtils.throwNumberFormat(java.lang.String, int) PC 7661 Signature id 437
Method 131: java.lang.StringUtils.getIntChars(char[], int, int, int) PC 7697 Signature id 439
Method 132: java.lang.StringUtils.exactStringLength(int, int) PC 7749 Signature id 442
Method 133: DataExchange.<init>() PC 7772 Signature id 2
Method 134: DataExchange.setis_sensor_task_finished(int) PC 7782 Signature id 455
Method 135: DataExchange.getis_sensor_task_finished() PC 7788 Signature id 456
Method 136: lejos.nxt.VM.VMImage.<init>(lejos.nxt.VM, int) PC 7793 Signature id 457
Method 137: lejos.nxt.VM.VMImage.<init>(lejos.nxt.VM, int, lejos.nxt.VM$1) PC 7807 Signature id 462
Method 138: lejos.nxt.VM.VMClone.update() PC 7814 Signature id 493
Method 139: lejos.nxt.VM.VMClone.<init>(int, int) PC 7830 Signature id 487
Method 140: lejos.nxt.VM.VMClone.<init>(int, int, lejos.nxt.VM$1) PC 7849 Signature id 489
Method 141: lejos.nxt.LCDOutputStream.<init>() PC 7856 Signature id 2
Method 142: java.lang.NumberFormatException.<init>(java.lang.String) PC 7871 Signature id 129
Method 143: java.io.OutputStream.<init>() PC 7877 Signature id 2
Method 144: lejos.util.Delay.msDelay(long) PC 7882 Signature id 498
Method 145: lejos.nxt.NXTEvent.<init>() PC 7934 Signature id 2
Method 146: lejos.nxt.NXTEvent.registerEvent() Native id 119
Method 147: lejos.nxt.NXTEvent.unregisterEvent() Native id 120
Method 148: lejos.nxt.NXTEvent.changeEvent(int, int) Native id 121
Method 149: lejos.nxt.NXTEvent.waitEvent(long) PC 7939 Signature id 501
Method 150: lejos.nxt.NXTEvent.waitEvent(int, long) PC 8062 Signature id 502
Method 151: lejos.nxt.NXTEvent.allocate(int, int, int) PC 8103 Signature id 509
Method 152: lejos.nxt.NXTEvent.free() PC 8153 Signature id 510
Method 153: lejos.nxt.Sound.<clinit> PC 8179 Signature id 3
Method 154: lejos.nxt.Sound.playFreq(int, int, int) Native id 62
Method 155: lejos.nxt.Sound.playTone(int, int, int) PC 8288 Signature id 522
Method 156: lejos.nxt.Sound.loadSettings() PC 8314 Signature id 385
Method 157: lejos.nxt.SystemSettings.<clinit> PC 8325 Signature id 3
Method 158: lejos.nxt.SystemSettings.getSlotIndex(java.lang.String) PC 8437 Signature id 532
Method 159: lejos.nxt.SystemSettings.setSlotValue(int, java.lang.String) PC 8469 Signature id 533
Method 160: lejos.nxt.SystemSettings.getSlotValue(int) PC 8592 Signature id 534
Method 161: lejos.nxt.SystemSettings.getStringSetting(java.lang.String, java.lang.String) PC 8666 Signature id 535
Method 162: lejos.nxt.SystemSettings.getIntSetting(java.lang.String, int) PC 8693 Signature id 536
Method 163: java.lang.Shutdown.<clinit> PC 8713 Signature id 3
Method 164: java.lang.Shutdown.halt(int) Native id 94
Method 165: java.lang.Shutdown.shutdown() Native id 95
Method 166: java.lang.Shutdown.shutdown(int) PC 8722 Signature id 539
Method 167: lejos.nxt.UltrasonicSensor.waitUntil(long) PC 8752 Signature id 600
Method 168: lejos.nxt.UltrasonicSensor.getData(int, byte[], int, int) PC 8763 Signature id 601
Method 169: lejos.nxt.UltrasonicSensor.sendData(int, byte[], int, int) PC 8796 Signature id 602
Method 170: lejos.nxt.UltrasonicSensor.<init>(lejos.nxt.I2CPort) PC 8829 Signature id 603
Method 171: lejos.nxt.UltrasonicSensor.getDistance() PC 8886 Signature id 604
Method 172: lejos.nxt.UltrasonicSensor.setMode(int) PC 8986 Signature id 608
Method 173: lejos.nxt.UltrasonicSensor.reset() PC 9097 Signature id 613
Method 174: lejos.nxt.Flash.<clinit> PC 9103 Signature id 3
Method 175: lejos.nxt.Flash.flashReadPage(byte[], int) Native id 81
Method 176: lejos.nxt.Flash.flashWritePage(byte[], int) Native id 80
Method 177: lejos.nxt.Flash.readPage(byte[], int) PC 9116 Signature id 764
Method 178: lejos.nxt.Flash.writePage(byte[], int) PC 9135 Signature id 765
Method 179: lejos.nxt.I2CSensor.<init>(lejos.nxt.I2CPort, int, int, int) PC 9222 Signature id 795
Method 180: lejos.nxt.I2CSensor.getData(int, byte[], int) PC 9260 Signature id 796
Method 181: lejos.nxt.I2CSensor.getData(int, byte[], int, int) PC 9269 Signature id 601
Method 182: lejos.nxt.I2CSensor.sendData(int, byte[], int) PC 9325 Signature id 797
Method 183: lejos.nxt.I2CSensor.sendData(int, byte[], int, int) PC 9334 Signature id 602
Method 184: lejos.nxt.SensorPort.SensorReader.<init>(lejos.nxt.SensorPort) PC 9403 Signature id 806
Method 185: lejos.nxt.SensorPort.SensorReader.setType(int) PC 9413 Signature id 634
Method 186: lejos.nxt.SensorPort.SensorReader.setMode(int) PC 9414 Signature id 635
Method 187: lejos.nxt.SensorPort.SensorReader.reset() PC 9415 Signature id 627
Method 188: lejos.nxt.SensorPort.StandardReader.<init>(lejos.nxt.SensorPort) PC 9416 Signature id 806
Method 189: lejos.nxt.FlashError.<init>(java.lang.String) PC 9427 Signature id 129
Method 190: lejos.nxt.NXT.getUserPages() Native id 93
Master record    : 20 bytes.
Class records    : 86 (1032 bytes).
Field records    : 94 (96 bytes).
Static fields    : 46 (92 bytes).
Static state     : 46 (176 bytes).
Constant records : 44 (176 bytes).
Constant values  : 44 (564 bytes).
Method records   : 191 (2292 bytes).
Exception records: 18 (144 bytes).
Interface maps   : 5 (4 bytes).
Code             : 144 (5012 bytes).
Total            : 9436 bytes.
Run time options : EnableCompact
Constant loads   : 41N 7O 1W 40S
Static load/store: 6N 102O
Field  load/store: 75N 108O
Program has been linked successfully
Uploading ...
Found NXT: NXT 00165317ACC9
leJOS NXJ> Upload successful in 1315 milliseconds
program has been uploaded


Just for reference, here is my RobotC program:
Code: Select all
#pragma config(Sensor, S1,     Sonar,          sensorSONAR)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

// Project: Benchmarking two NXTs running various firmwares.
// Firmwares to test: RobotC, NXC, leJOS, nxt-python, nxtOSEK, ROS?
// Function: run various parallel sensing, motor and comms tasks and time them.
//      This is the code for the master NXT.
// Author: Mike McFarlane 23-01-13
// License: such a decision! It's free, there isn't much to it, but if you use it please give credit - Mike McFarlane moo_at_moosooboo.com
// Credits:
// 1: Includes code, and utilising RobotC driver suite by Xander Soldaat (xander_at_botbench.com)
// Version: 0-1: get it up and running
// task main: start timer, start tasks, wait for task1 to finish, stop other tasks, add the two arrays, display array sum results, message count & timer.
// task1: read sensor, store to int array, read sensor store to float array.
//       run the array filling loops a few times so runs longer for more accuracy, bin
//      last set of values in array.
// task2: drive motor back and forth to set points while task1 running
// task3: Tx/Rx messages to other NXT for a sensor value via BT (bluetooth), keep count, display count at end
// Version: 0-2: task3 will use Dexter Industries wifi
// Version: 0-3: task3 will use the I2C bus standard on ports 1-3
// Version: 0-4: task3 will use the I2C/RS-485 bus high speed on port 4
// Status: dev

// max array size is somewhere between 100 and 200 or get compiler error
const int array_size = 100;
const int number_task_loops = 500;

// semaphore to show when task finished
bool is_task_1_finished = false;

// define global arrays so can be accessed by all tasks
int int_array[array_size];
float float_array [array_size];
int slave_sensor_array [array_size];

// define global variables so can be accessed by all tasks
int message_count = 0;

task task1(){
   // task1: read sensor, store to int array, read sensor store to float array.
   is_task_1_finished = false;

   // loop x (const number_task_loops) times to get decent run time, as max array size is limited
   for (int i = 0; i <= number_task_loops; i++){
      for (int j = 0; j < array_size; j++){
         int_array[j] = SensorValue(Sonar);
      }
   }

   for (int i = 0; i <= number_task_loops; i++){
      for (int j = 0; j < array_size; j++){
         // get the sensor value, typecast, and multiply to give real non int
         float_array[j] = (float)SensorValue(Sonar) * 0.9;
      }
   }

   // i'm done, lets get the schizzle out of here!
   is_task_1_finished = true;
}

task task2(){
   // task2: drive motor back and forth to set points while task1 running
   nMotorEncoderTarget[motorA] = 0;

   while (true){
      nMotorEncoderTarget[motorA] = 720;
      motor[motorA] = 50;
      while (nMotorRunState[motorA]){
         // wait
      }
      motor[motorA] = 0;

      nMotorEncoderTarget[motorA] = 720;
      motor[motorA] = -50;
      while (nMotorRunState[motorA]){
         // wait
      }
      motor[motorA] = 0;
   }
}

task task3(){
   // task3: Tx/Rx messages to other NXT for a sensor value via BT (bluetooth), keep count, display count at end
   while (true){
      // keep looping round filling the array
      for (int i = 0; i < array_size; i++){
         sendMessage(1);
         // wait for response
         while (message == 0){
            // wait
            ClearMessage();
            abortTimeslice(); // seems to get more messages through
            //wait1Msec(5); // 5ms minimum or messages not working, aborttimeslice works better
         }
         slave_sensor_array[i] = message;
         ClearMessage();
         message_count = message_count + 1;

         // loop delay - according to RobotC doc, meant to wait 30ms
         // before sending another message.
         // Going to tweak this a little to try and maximise message count
         // 20ms seems fastest, lower drops messages
         wait1Msec(20);
      }
   }
}


task main()
{
   // task main: start timer, start tasks, wait for task1 to finish, add the two arrays, stop other tasks, display array sum results, message count & timer.

   ClearTimer (T1);

   int start_time = time1[T1];
   int end_time = 0;
   int sum_ints = 0;
   float sum_floats = 0;
   int sum_slave_sensor_array = 0;

   StartTask(task1);
   StartTask(task2);
   StartTask(task3);

   // wait for task1 to finish
   while(!is_task_1_finished){
      // wait
      //wait1Msec(1);
      // or
      abortTimeslice();
   }

   // needed??
   StopTask(task1);

   for (int i = 0; i <= array_size; i++){
      sum_ints = sum_ints + int_array[i];
   }

   for (int j = 0; j <= array_size; j++){
      sum_floats = sum_floats + float_array[j];
   }

   for (int k = 0; k <= array_size; k++){
      sum_slave_sensor_array = sum_slave_sensor_array + slave_sensor_array[k];
   }


   StopTask(task2);
   StopTask(task3);

   end_time = time1[T1];

   eraseDisplay();
   nxtDisplayTextLine(0, "start: %d", start_time);
   nxtDisplayTextLine(1, "end: %d", end_time);
   nxtDisplayTextLine(2, "msgs: %d", message_count);
   nxtDisplayTextLine(3, "sum int: %d", sum_ints);
   nxtDisplayTextLine(4, "sum flt: %1.1f", sum_floats);
   nxtDisplayTextLine(5, "sum slv: %d", sum_slave_sensor_array);

   // wait for button press to end
   nxtDisplayTextLine(6, "Press button to finish",);
   while (nNxtButtonPressed == -1){
      // wait
      wait1Msec(10);
   }

   StopAllTasks();

}
moosooboo
New User
 
Posts: 14
Joined: Thu Jan 17, 2013 5:10 pm

Re: Passing variables and arrays between classes - how???

Postby gloomyandy » Wed Feb 13, 2013 2:56 pm

Do you mind if I first of all ask why you are doing this?

I ask because I think you will find it very hard to ensure that all of the various benchmarks are actually doing exactly the same thing. So for instance RobotC handles sensors in a very different way to leJOS (for simple sensors it has internal threads that are basically reading the sensor A/D values an updating a variable that can be read by the program, leJOS actually makes calls to read the A/D data when you ask for it). Also I'm pretty sure that a simple translation of your benchmark will result in the motors running at different speeds (and since your code is basically measuring how fast the motor moves, it will impact the results). RobotC uses the notion of a power level to set the motor speed (value between 0 and 100) leJOS uses an actual degrees per second speed. You will also need to take into account that leJOS provides control of the motor acceleration during a move request and unless you set this correctly you will not be comparing like with like. Finally the two systems Bluetooth communication mechanisms are very different, RobotC uses a Mailbox model while leJOS uses a data stream, it will be hard to ensure you are comparing like with like.

Oh and exception 16 is NullPointerException you can decode the exception numbers by looking at the output from the leJOS linker. Simply look up the class number. The leJOS tutorial has details of all of this.

In terms of passing data, in Java you call methods and you can pass parameters to these. The parameter types can be simple types like int, float etc. or more complex types like arrays and objects. Simple types are basically passed by value (so if you change the value of the parameter in the called method you will not change the value of the variable you used in the call). For more complex data types you basically pass a reference to the object, you can use this reference to make changes to the contents of the object (like array members). Having said all of this you may be better off reading some of the many very good Java tutorials that are available on the web to get a better idea of this sort of thing.

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3012
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Passing variables and arrays between classes - how???

Postby moosooboo » Wed Feb 13, 2013 4:08 pm

Hi Andy, first, thanks for the really detailed and informative reply.

gloomyandy wrote:Do you mind if I first of all ask why you are doing this?


Two reasons really. I wanted to do a very quick and dirty 'real world robot' benchmark to see relative speed differences between the different languages,as might be coded by an idiot programer like myself:-> Secondly, I am trying to improve my programing a bit and decide on a language to go a bit deeper with Mindstorms so I thought I would 'expose' myself to a few languages to get a very rough feel for each one. (My list is RobotC, NXC, nxtOSEK, ROS, nxt-python and leJOS.)

gloomyandy wrote:I ask because I think you will find it very hard to ensure that all of the various benchmarks are actually doing exactly the same thing. So for instance RobotC handles sensors in a very different way to leJOS (for simple sensors it has internal threads that are basically reading the sensor A/D values an updating a variable that can be read by the program, leJOS actually makes calls to read the A/D data when you ask for it). Also I'm pretty sure that a simple translation of your benchmark will result in the motors running at different speeds (and since your code is basically measuring how fast the motor moves, it will impact the results). RobotC uses the notion of a power level to set the motor speed (value between 0 and 100) leJOS uses an actual degrees per second speed. You will also need to take into account that leJOS provides control of the motor acceleration during a move request and unless you set this correctly you will not be comparing like with like. Finally the two systems Bluetooth communication mechanisms are very different, RobotC uses a Mailbox model while leJOS uses a data stream, it will be hard to ensure you are comparing like with like.


I didn't know the details of any of that, but it is interesting to me to read. :-) I do understand that such differences exists and that a crude code translation between each language will not be the most efficient or optimised code. But my objective is a crude 'real world robot' or a very simple robot code relative performance comparison.

Reading around the various forums, generally people seem to say that RobotC is the fastest, but I wanted to try and find out how much faster, or if that was indeed true.

gloomyandy wrote:Oh and exception 16 is NullPointerException you can decode the exception numbers by looking at the output from the leJOS linker. Simply look up the class number. The leJOS tutorial has details of all of this.


I was confused on that one, some references gave it as NullPointerException, others as ThreadDeath. e.g. http://lejos.sourceforge.net/forum/viewtopic.php?t=1215&highlight=exception I must have misintepreted, I'll check the tutorial.

My question is, which bit of code caused it, and why? It seems to be 'deObj.setis_sensor_task_finished(1);' as if I comment that out then the exception disappears. But I don't know why as the code looks the same as that in the leJOS ebook I mentioned.

gloomyandy wrote:In terms of passing data, in Java you call methods and you can pass parameters to these. The parameter types can be simple types like int, float etc. or more complex types like arrays and objects. Simple types are basically passed by value (so if you change the value of the parameter in the called method you will not change the value of the variable you used in the call). For more complex data types you basically pass a reference to the object, you can use this reference to make changes to the contents of the object (like array members). Having said all of this you may be better off reading some of the many very good Java tutorials that are available on the web to get a better idea of this sort of thing.
Andy


Understood, I have been working through some and googling lots before I posted, but to be honest I am really struggling as the Java syntax and 'jargon' are so complicated.

A lot of the tutorials seem to cover passing data between methods within a class, but I was unsure if it is the same thing to pass data between objects/classes (if that is the right words). I couldn't find any useful info on that in particular, but it is probably because I don't know the right keywords to google.

Yes, I know, put the time in, I was hoping someone would take pity and either write me some code or pseudo code to get me on my way :-> Or point me towards a relevant example or page in a tutorial.

Thanks again for the informative reply. I hope my answers make sense. More help would be appreciated if you, or anyone else has time.
moosooboo
New User
 
Posts: 14
Joined: Thu Jan 17, 2013 5:10 pm

Re: Passing variables and arrays between classes - how???

Postby moosooboo » Wed Feb 13, 2013 4:29 pm

That maybe didn't come out right, I didn't want someone to write code for me, a bit of pseudo code in the places where I have messed up or missed something was what I was hoping for.
moosooboo
New User
 
Posts: 14
Joined: Thu Jan 17, 2013 5:10 pm

Re: Passing variables and arrays between classes - how???

Postby skoehler » Wed Feb 13, 2013 7:15 pm

moosooboo wrote:
gloomyandy wrote:Oh and exception 16 is NullPointerException you can decode the exception numbers by looking at the output from the leJOS linker. Simply look up the class number. The leJOS tutorial has details of all of this.


I was confused on that one, some references gave it as NullPointerException, others as ThreadDeath. e.g. http://lejos.sourceforge.net/forum/viewtopic.php?t=1215&highlight=exception I must have misintepreted, I'll check the tutorial.

My question is, which bit of code caused it, and why? It seems to be 'deObj.setis_sensor_task_finished(1);' as if I comment that out then the exception disappears. But I don't know why as the code looks the same as that in the leJOS ebook I mentioned.


First of all, carefully read this:
http://lejos.sourceforge.net/nxt/nxj/tu ... ging.htm#1
Especially the part on the debug tool.

Secondly, you get a NullPointerException, because you pass the null reference to the constructor of ReadSensorTask. You should probably swap these two lines in your code:
Code: Select all
rst = new ReadSensorTask(de);
de = new DataExchange();


And you didn't notice your mistake,because de is an instance variable instead of a local variable for no good reason. (Instance variables are always initialized with null by default in Java, local variables have to be initialized before they are read for the first time). And in general, using an instance variable when a local variable would do should be considered bad style.
skoehler
leJOS Team Member
 
Posts: 1128
Joined: Thu Oct 30, 2008 4:54 pm

Re: Passing variables and arrays between classes - how???

Postby moosooboo » Thu Feb 14, 2013 7:10 am

@ skoehler

ok, thank you. I'll work through all that this afternoon when I am back at my desk. There is a lot to take in with Java (compared to C or Python), I really appreciate the help.

Mike
moosooboo
New User
 
Posts: 14
Joined: Thu Jan 17, 2013 5:10 pm

Re: Passing variables and arrays between classes - how???

Postby gloomyandy » Thu Feb 14, 2013 9:10 am

You may find this thread of interest...
viewtopic.php?f=5&t=4313

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3012
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Passing variables and arrays between classes - how???

Postby moosooboo » Thu Feb 14, 2013 11:49 am

gloomyandy wrote:You may find this thread of interest...
viewtopic.php?f=5&t=4313


Awesome, that is exactly what I have been looking for. Funny google didn't find the Berkley link. Thank you Andy, that has cheered me up after getting my ass kicked in this weeks quiz on the Coursera Mobile Robotics https://www.coursera.org/course/conrob course:-)
moosooboo
New User
 
Posts: 14
Joined: Thu Jan 17, 2013 5:10 pm

Re: Passing variables and arrays between classes - how???

Postby moosooboo » Thu Feb 14, 2013 2:55 pm

Via the Berkley Course I found out about the Head First Java book by O'Reilly http://www.headfirstlabs.com/books/hfjava/ which is about at my level, and there is a free pdf chapter covering some Java objects basics http://oreilly.com/catalog/hfjava2/chapter/ch02.pdf which has popped a whole lot of concepts about Java into their proper place. Top notch.
moosooboo
New User
 
Posts: 14
Joined: Thu Jan 17, 2013 5:10 pm


Return to NXJ Software

Who is online

Users browsing this forum: Google [Bot] and 2 guests

more stuff