Introduction: How to Add Emotions to a Machine

Talks about analog programming. In our computers each action is composed of discrete processor instructions. Instead, the brain can generate a single smooth movement of a complicated shape which alone solves the same task. There are 2 main emotions - attraction and aversion. Let's demonstrate it on an example.
As usual, we will use the development environment of WinNB and the script HOME_ROBO.SCP from the directory RSandbox.

https://www.softpedia.com/dyn-search.php?search_term=WinNB

Suppose a robot moves to some definite goal and suddenly discovers an obstacle in its way. How to get around it? The solution of discrete programming would be to stop, return for some steps, move sideways, then try again. You can watch it in the video below. Now let's try an alternative approach.

Step 1: Understand the Concept of Biofield.

Each living being perceives various sensory input and reacts accordingly. This works like an electric charge in an electric field. 2 charges of the opposite signs will attract each other. Otherwise - repel. No mysticism here. A field is a purely mathematical concept that tells about the distribution of force. Meanwhile our case is special. In an electric or magnetic field energy comes from the source. Here - from the receiver. A dog may weigh several kilograms and some substantial work (in the physical sense) is required to move her. The field itself is pure information. It may be emitted by any object (living or non-living), but affects only a living being (or his artificial replica).

Step 2: Choose the Sources of Biofield.

For concrete calculations we will use the same law that in electrostatics. The robot itself will have a positive charge. We will place 2 point charges to generate the biofield. One - in the center of the obstacle. Another - in the point of destination. In this case it will be in the middle of the kitchen door. The first one will be positive to repel the robot. The second - negative to attract it.

Step 3: Update the Program.

After
#draw("rectangle","red",88,50,98,60). %Obstacle.

add

remember: _biocharge(93, 55, 10). %Source of biofield X, Y, Charge.

The following rules should look like

_move_to("kitchen door") if

_static_coord("kitchen door",@X1,@Y1,@X2,@Y2);

#cut();

_get_curr_coord(@X,@Y);

@X3=@X1+(@X2-@X1)/2;

@Y3=@Y1+(@Y2-@Y1)/2;

@DX=@X3-@X;

@DY=@Y3-@Y;

remember: _biofield_on;

remember: _biocharge(@X3, @Y3, -10);

remember: _move_goal(@X3, @Y3);

_move_dxdy(100,@DX,@DY).

%From the opposite side without biofield.

_move_to("kitchen door1") if

_static_coord("kitchen door",@X1,@Y1,@X2,@Y2);

#cut();

_get_curr_coord(@X,@Y);

@X3=@X1+(@X2-@X1)/2;

@Y3=@Y1+(@Y2-@Y1)/2;

@DX=@X3-@X;

@DY=@Y3-@Y;

_move_dxdy(100,@DX,@DY).

This inserts the second source in the doorway.

Also after

_move_sideways(@DX2,@DY2) if

remember: _instruction(5,@DX2,@DY2).

calculate net biofield

_biofield(@X, @Y, @Val, @XVal, @YVal) if

_clear_biofield_val;

_biocharge(@Xch, @Ych, @Charge);

@DX = @X - @Xch;

@DY = @Y - @Ych;

@Dist2 = @DX * @DX + @DY * @DY;

@Dist = #pw(@Dist2,0.5);

@Val = @Charge / @Dist2;

@XVal = @Val * @DX / @Dist;

@YVal = @Val * @DY / @Dist;

_add_biofield_val(@Val, @XVal, @YVal);

_fail.

_biofield(@X, @Y, @Val, @XVal, @YVal) if

_biofield_val(@Val, @XVal0, @YVal0);

@XVal = 10 * @XVal0;

@YVal = 10 * @YVal0;

#cut().

_clear_biofield_val if

_biofield_val(@Val, @XVal, @YVal);

forget: _biofield_val(@Val, @XVal, @YVal);

remember: _biofield_val(0, 0, 0);

#cut().

_clear_biofield_val if

remember: _biofield_val(0, 0, 0).

_add_biofield_val(@Val, @XVal, @YVal) if

_biofield_val(@Val0, @XVal0, @YVal0);

forget: _biofield_val(@Val0, @XVal0, @YVal0);

@Val1 = @Val0 + @Val;

@XVal1 = @XVal0 + @XVal;

@YVal1 = @YVal0 + @YVal;

remember: _biofield_val(@Val1, @XVal1, @YVal1);

#cut().

Change _dec_nsteps()

_dec_nsteps(@N) if

_dx1dy1(@DX1,@DY1);

forget: _dx1dy1(@DX1,@DY1);

remember: _ready;

_move_goal(@DX2,@DY2);

forget: _move_goal(@DX2,@DY2);

forget: _biofield_on;

_biocharge(@X,@Y,@Charge);

@Charge < 0;

forget: _biocharge(@X,@Y,@Charge);

This removes the second charge at the end. The following rule translates the biofield into motion.

% Switch it off near goal because attraction becomes too large.

_emotions if

_nsteps(@N);

@N > 15;

_get_curr_coord(@X,@Y);

_biofield(@X, @Y, @Val, @XVal, @YVal);

_move_dx1dy1(@XVal, @YVal);

_get_curr_coord(@X2,@Y2);

_move_goal(@X1,@Y1);

@DX1=(@X1-@X2) / @N;

@DY1=(@Y1-@Y2) / @N;

_chng_dx1dy1(@DX1,@DY1);

#cut().

_emotions if

_true.

Now use it. In the virtual machine after

_expand if

_sq_dir("expand",@Add);

#cut();

_sq_nsteps(@N);

_dec_sq_nsteps(@N,"expand",@Add).

add

_emotions if

_biofield_on;

#cut().

Step 4: Test It.

The robot encounters the obstacle visible as a red square in the room when it tries to bring something from the kitchen stock. This is a bottle of Cola or an apple in later versions. Use
bring Cola.

Watch trace_bf.mp4

The biofield provides soft repulsion. In some cases the robot can still touch the object. Check that the discrete program of getting around is properly launched and completes its job.

Step 5: Adjust Parameters.

Robotics applications have many values which allow fine-tuning. You can regulate the step size of the robot and its manipulator, the frequency of these steps and accordingly the speed of movement. The value of charges is important. It is better when the robot doesn't move head-on towards the obstacle. Then the track will be gradually curved. Otherwise, it will make one step forth, one back. If repulsion is too strong, the machine will never reach the goal.

Step 6: Add Tracing.

If you want to view the full track of the robot or create the picture for a publication, do the following.
In the virtual machine after

_chng_instruction if

_ready.

add

_draw_trajectory if

_draw_traj_flag;

#cut().

The implementation will be

_draw_trajectory if

_get_curr_coord(@X,@Y);

#draw("dot",@X,@Y).

The dot will be placed in the center of the robot so during its movement it will be erased. Nevertheless all the dots are retained in the knowledge base and we can visualize the trace when all the necessary steps are performed. Keep in mind that if the path is long, this may slow the program.

_dec_nsteps(@N) if

_dx1dy1(@DX1,@DY1);

forget: _dx1dy1(@DX1,@DY1);

remember: _ready;

_move_goal(@DX2,@DY2);

_refresh;

forget: _move_goal(@DX2,@DY2);

forget: _biofield_on;

Here _refresh is added after checking that _move_goal was established so this will work only with the biofield. If you place it after _dx1dy1(@DX1,@DY1), it will be possible to trace how the robot handles obstacles using the common discrete algorithm.

You can see it in the left picture.

Finally, turn on tracing placingremember: _draw_traj_flag. after #draw("set_dot","black",0.5). at the beginning of the program.

See the result in the right picture.

Using these instructions you can add the described features yourself to the free version of Robotics Sandbox. Otherwise buy the ready solution on nbsite.