Native function for fast Aseba array Deque
Sequential programming with the Aseba language is fastidious. Because the VM doesn't support blocking operations, preemptive scheduling, or a call stack, the approach in thymio_motion.aesl
is to use a queue that is polled at 100 Hz by a motor
event. The queue is implemented using arrays and index variables, with insertion handled by scanning forward through the arrays. This ad hoc solution is complicated, fragile, and inefficient.
Native functions to implement a fast vector deque would make it easier to implement sequential operations in Aseba programs, and reduce bytecode size. The implementation could be based on ArrayDeque: Fast Deque Operations Using an Array. Four operations would be defined:
-
deque.get(A, B, i)
Copy lenB
elements from positioni *
lenB
inA
-
deque.set(A, B, i)
Assign lenB
elements to positioni *
lenB
inA
-
deque.add(A, B, i)
Insert lenB
elements at positioni *
lenB
inA
, shifting the existing contents to make room (Figure 2.3). Elements are lost from the right ifA
is full. -
deque.remove(A, i, n)
Removen
elements at positioni * n
inA
, shifting the existing contents (Figure 2.3).
(The first two elements of A
are reserved for the j
and n
pointers, which is why get
and set
aren't simply implemented using existing Aseba vector addressing operations.)
Implementing a simple motion queue would give something like:
var Queue[122] # Store up to 40 operations e.g. for BeeBot
var operation[3]
sub motion_add
call deque.add(Queue,operation[0:2],-1) # push at back
onevent timer0
call deque.get(Queue,operation,0)
timer.period[0] = operation[0]
motor.left.target = operation[1]
motor.right.target = operation[2]
deque.remove(Queue,0,3) # pop from front