Menu

How to use OpenSwarm

Introduction

OpenSwarm is an embedded operating system designed for miniature robots. It provides time-sliced pre-emptive threads for computational intensive tasks and events to execute short run-to-completion tasks. The use of event is a basic feature of OpenSwarm. Events are used for inter-process communication mechanism and provide a defined interface between functions. OpenSwarm also provides a hardware-independent environment to develop user applications.

As it can be seen in this figure, OpenSwarm is an operating system that executed user processes and provided a variety of hardware-independent function. Each process is single threaded and executed in a non-protected memory-space. Consequently, in OpenSwarm, a process shares the same properties as a thread and can be used interchangeably.

OpenSwarm consists of three parts:

Hardware Abstraction Layer (HAL): It manages hardware signals and data. It converts hardware-independent into hardware-specific values/data and applies them on the I/O Devices. Similarly, values received from hardware are converted into hardware-independent data and send as events to subscribed processes.

Event Module: This is a central element of OpenSwarm. It defines how events are processed and provides functions to create, register of, subscribe to, or delete events.

Process Manager: It defines how processes are scheduled, created, suspended, released, and terminated.

Deploying on a New Robot

To deploy OpenSwarm on a new robot, only parts of the Process Manager and Hardware Abstraction Layer has to be reimplemented. Other functions can be used on any device.

A detailed description of “How to deploy OpenSwarm” will be added shortly. If you want to use OpenSwarm already, please contact us and we will give you all the information that you need.

Starting with OpenSwarm

If OpenSwarm has been deployed on your robot, you can start writing your first application.

//Finally, you can start OpenSwarm -> this will execute all threads and I/O

Sys_Start_Kernel();

while(1){

//Do something/nothing

}

}

Create and Run a Thread

A thread can be defined as a simple function with no return value:

How to define a thread function

C

1

voidthread_name(void);

To start a thread just have to write a code that is similar to this main.c.

main.c - with an additional thread

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

#include "system.h" //this contains OpenSwarm

voidthread_name();

intmain(void){

Sys_Init_Kernel();//First, initialise OpenSwarm

Sys_Start_Process(thread_name);

Sys_Start_Kernel();

while(1){

//Do something/nothing

}

}

voidthread_name(){

//do something

}

Note that creating a thread requires a significant amount of time and memory. As a result, creating threads that return quickly are inefficient and the programmer should think of using event handler instead. After the thread returns, the process is terminated and deleted. It is recommended but not required to tell the operating system which threads are executed before the kernel starts.

How to Use Events

Events can be used in different ways (for process communication/synchronisation or to execute event handlers asynchronously). Before a event can be used, event has to be registered to tell OpenSwarm that this event can happen: (The event ID in this example is 0x12)

How to register an event

C

1

2

3

#define USER_EVENT_TEST 0x12

Sys_Register_Event(USER_EVENT_TEST);

Following that, events with ID “USER_EVENT_TEST" can be sent and received. To send an event, you can use:

How to send a event

C

1

Sys_Send_Event(USER_EVENT_TEST,(void*)data,(uint16)length_of_data);

To receive an event, you have two options. You can subscribe an event handler, which gets executed asynchronously by occurrence of the event. Alternatively, you can block a thread until an event occurs (synchronously). In any case, you can define a condition function. Only if the condition function return true, the event is processed. For instance, an event-handler should only be executed when the event occurred is 250 times or the event has to reach a certain threshold.