Skip to content

Commit

Permalink
tutorial: further updates to v0.9
Browse files Browse the repository at this point in the history
- update code samples
- drop logger (to be replaced with mqueues)

Signed-off-by: Markus Klotzbuecher <mk@mkio.de>
  • Loading branch information
kmarkus committed Jul 7, 2020
1 parent 89ed76d commit 0d247fe
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 192 deletions.
21 changes: 13 additions & 8 deletions docs/user/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,11 @@ Final listings of the block
~~~~~~~~~~~~~~~~~~~~~~~~~~~

The plant is, *mutatis mutandis*, built following the same rationale,
and will be not detailed here. the final code of the plant and the
controller can be retrieved here **TODO add link to the code**
and will be not detailed here. The final code of the plant and the
controller can be retrieved here:

- :download:`platform_2dof.c <../../examples/platform/platform_2dof.c>`.
- :download:`platform_2dof_control.c <../../examples/platform/platform_2dof_control.c>`.

Compiling the blocks
~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -333,14 +336,16 @@ Deployment via the usc (microblx system composition) file
independently. We want to run and compose them together and save the
results in a logger file. The composition file
**platform_2dof_and_control.usc** is quite self explanatory: It
indicates
contains

- which libraries are imported,
- which blocks (name, type) are created,
- the configuration values of properties.
- the libraries to be imported,
- which blocks (name, type) to create,
- the configuration values of blocks.
- the connections among ports

The code is the following
The file :download:`platform_2dof_and_control.usc
<../../examples/platform/platform_launch/platform_2dof_and_control.usc>`
is shown below:

.. literalinclude:: ../../examples/platform/platform_launch/platform_2dof_and_control.usc
:language: lua
Expand Down Expand Up @@ -431,7 +436,7 @@ Setup the build system starting from the build part
*configure.am*
:: code::
.. code::
m4_define([package_version_major],[0])
m4_define([package_version_minor],[0])
Expand Down
175 changes: 81 additions & 94 deletions examples/platform/platform_2dof.c
Original file line number Diff line number Diff line change
@@ -1,127 +1,114 @@
#include "platform_2dof.h"
#include "math.h"
#include <math.h>

/* define a structure for holding the block local state. By assigning an
* instance of this struct to the block private_data pointer (see init), this
* information becomes accessible within the hook functions.
*/
struct robot_state {
double pos[2];
double vel[2];
double vel_limit[2];
double pos[2];
double vel[2];
double vel_limit[2];
};

double sign(double x)
{
if(x > 0) return 1;
if(x < 0) return -1;
return 0;
if(x > 0) return 1;
if(x < 0) return -1;
return 0;
}

struct platform_2dof_info
{
/* add custom block local data here */
struct robot_state r_state;
struct ubx_timespec last_time;
/* this is to have fast access to ports for reading and writing, without
* needing a hash table lookup */
struct platform_2dof_port_cache ports;
/* add custom block local data here */
struct robot_state r_state;
struct ubx_timespec last_time;

/* this is to have fast access to ports for reading
* and writing, without needing a hash table lookup */
struct platform_2dof_port_cache ports;
};

/* init */
int platform_2dof_init(ubx_block_t *b)
{
int ret = -1;
long len;
struct platform_2dof_info *inf;
// double pos_vec[2];
double *pos_vec;

/* allocate memory for the block local state */
if ((inf = calloc(1, sizeof(struct platform_2dof_info)))==NULL) {
ubx_err(b, "platform_2dof: failed to alloc memory");
ret=EOUTOFMEM;
goto out;
}
b->private_data=inf;
update_port_cache(b, &inf->ports);

//read configuration - initial position
if ((len = ubx_config_get_data_ptr(b, "initial_position",(void **)&pos_vec)) < 0) {
ubx_err(b, "platform_2dof: failed to load initial_position");
goto out;
}
inf->r_state.pos[0]=pos_vec[0];
inf->r_state.pos[1]=pos_vec[1];
if ((len = ubx_config_get_data_ptr(b, "joint_velocity_limits",(void **)&pos_vec)) < 0) {
ubx_err(b, "platform_2dof: failed to load joint_velocity_limits");
goto out;
}
//read configuration - max velocity
inf->r_state.vel_limit[0]=pos_vec[0];
inf->r_state.vel_limit[1]=pos_vec[1];

inf->r_state.vel[0]=inf->r_state.vel[1]=0.0;
ret=0;
out:
return ret;
}
long len;
struct platform_2dof_info *inf;
const double *pos_vec;

/* start */
int platform_2dof_start(ubx_block_t *b)
{
struct platform_2dof_info *inf = (struct platform_2dof_info*) b->private_data;
/* allocate memory for the block local state */
if ((inf = calloc(1, sizeof(struct platform_2dof_info)))==NULL) {
ubx_err(b,"platform_2dof: failed to alloc memory");
return EOUTOFMEM;
}

b->private_data=inf;
update_port_cache(b, &inf->ports);

/* read configuration - initial position */
len = cfg_getptr_double(b, "initial_position", &pos_vec);

ubx_info(b, "platform_2dof start");
ubx_gettime(&(inf->last_time));
/* this will never assert unless we made an error
* (e.g. mistyped the configuration name), since min/max
* checking will catch misconfigurations before we get
* here. */
assert(len==2);

inf->r_state.pos[0] = pos_vec[0];
inf->r_state.pos[1] = pos_vec[1];

/* read configuration - max velocity */
len = cfg_getptr_double(b, "joint_velocity_limits", &pos_vec);
assert(len==2);

int ret = 0;
return ret;
inf->r_state.vel_limit[0] = pos_vec[0];
inf->r_state.vel_limit[1] = pos_vec[1];
inf->r_state.vel[0] = inf->r_state.vel[1] = 0.0;

return 0;
}

/* stop */
void platform_2dof_stop(ubx_block_t *b)
int platform_2dof_start(ubx_block_t *b)
{
/* struct platform_2dof_info *inf = (struct platform_2dof_info*) b->private_data; */
ubx_info(b, "platform_2dof stop");

struct platform_2dof_info *inf = (struct platform_2dof_info*) b->private_data;
ubx_info(b, "platform_2dof start");
ubx_gettime(&(inf->last_time));
return 0;
}

/* cleanup */
void platform_2dof_cleanup(ubx_block_t *b)
{
/* struct platform_2dof_info *inf = (struct platform_2dof_info*) b->private_data; */
ubx_info(b, "platform_2dof cleanup");
free(b->private_data);
/* struct platform_2dof_info *inf = (struct platform_2dof_info*) b->private_data; */
ubx_info(b, "%s", __func__);
free(b->private_data);
}

/* step */
void platform_2dof_step(ubx_block_t *b)
{
int32_t ret;
double velocity[2];
struct ubx_timespec current_time, difference;
struct platform_2dof_info *inf = (struct platform_2dof_info*) b->private_data;
//compute time from last call
ubx_gettime(&current_time);
ubx_ts_sub(&current_time,&(inf->last_time),&difference);
inf->last_time=current_time;
double time_passed= ubx_ts_to_double(&difference);

//read velocity from port
ret = read_desired_vel_2(inf->ports.desired_vel, &velocity);
if (ret<=0){ //nodata
velocity[0]=velocity[1]=0.0;
ubx_err(b, "no velocity data");
}

for (int i=0;i<2;i++){// saturate and integrate velocity
velocity[i]=fabs(velocity[i])> inf->r_state.vel_limit[i]? sign(velocity[i])*(inf->r_state.vel_limit[i]):velocity[i];
inf->r_state.pos[i]+=velocity[i]*time_passed;}
//write position in the port
write_pos_2(inf->ports.pos,&(inf->r_state.pos));

int32_t ret;
double velocity[2];
struct ubx_timespec current_time, difference;
struct platform_2dof_info *inf = (struct platform_2dof_info*) b->private_data;

/* compute time from last call */
ubx_gettime(&current_time);
ubx_ts_sub(&current_time,&(inf->last_time),&difference);
inf->last_time=current_time;
double time_passed = ubx_ts_to_double(&difference);

/* read velocity from port */
ret = read_double_array(inf->ports.desired_vel, velocity, 2);

if (ret <= 0) { /* nodata */
velocity[0] = velocity[1] = 0.0;
ubx_err(b,"no velocity data");
}

for (int i=0; i<2; i++) {
/* saturate and integrate velocity */
velocity[i] =
fabs(velocity[i]) > inf->r_state.vel_limit[i] ?
sign(velocity[i]) * (inf->r_state.vel_limit[i]) : velocity[i];

inf->r_state.pos[i] += velocity[i] * time_passed;
}
/* write position in the port */
write_double_array(inf->ports.pos, inf->r_state.pos, 2);
}


2 changes: 1 addition & 1 deletion examples/platform/platform_2dof.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ return block
{ name="desired_vel", in_type_name="double", in_data_len=2, doc="desired velocity [m/s]" }
},

operations = { start=true, stop=true, step=true }
operations = { start=true, step=true }
}

0 comments on commit 0d247fe

Please sign in to comment.