# Presentation: 418869

## Robotics With the XBC ControllerSession 7

- Instructor: David Culp
- Email: _[culpd](mailto:culpd@cfbisd.edu)__[@](mailto:culpd@cfbisd.edu)__[cfbisd](mailto:culpd@cfbisd.edu)__[.](mailto:culpd@cfbisd.edu)__[edu](mailto:culpd@cfbisd.edu)_

## Learning Goals

- The student will learn about BEMF and the usage of the BEMF encoders on the XBC.  In addition the student will learn to use the multitasking capabilities of the XBC and learn about the engineering process.  The student will learn the advantages and disadvantages of an incremental design process.
- The student will combine all these elements into the implementation of a robot that chases and grabs orange balls.

## BEMF Encoders

- Back Electro Motive Force (BEMF)
- As a DC motor spins it generates a Voltage, or EMF (It follows from Faraday’s Law for changing magnetic fields) .
- This EMF opposes the applied voltage.
-  The level of EMF is proportional to the velocity of the motor and thus reduces the net applied voltage as the motor speeds up.
- If we momentarily remove the applied voltage we can read this EMF and have a measure of the velocity of the motor.
- Since velocity is position/time, we can get position data from the motor by adding up EMF values at small time intervals.
**Using BEMF encoders allows very precise control over our robots movements.**

## Seeing BEMF Encoder Data

- See on screen demonstration on how to view encoder data on the XBC in real-time.

## Basic BEMF Motor Functions

- long get_motor_position_counter(int motor)
- This function tells you the current position of the motor.
- void set_motor_position_counter(int motor, long value)
- This function sets the motor counter to the position you specified without moving the motor.  This is usually used to set the number of the position to zero or another number that is easy to work with.
- void clear_motor_position_counter(int motor)
- This function resets the motor counter to zero.

## Movement Functions

- void move_at_velocity(int motor, int velocity)
- This function will attempt to move the specified motor at the velocity between -1000 and 1000 pulses per second.  You can also use the shorthand -
- void mav(int motor, int velocity).
- void move_to_position(int motor, int **speed**, long goal_pos)
- This function will move the motor to the position goal specified at the speed chosen by the user.  **Note that we call it ‘speed’ because it is always positive; the polarity of the position goal determines direction.**  This also works with the shorthand -
- mtp(int motor, int speed, long goal_pos).

## More Movement Functions

- void move_relative_position(int motor, int **speed**, long delta_pos)
- This function moves the motor delta_pos (change in position) units at the specified speed.  The shorthand for this function is -
- mrp(int motor, int speed, long delta_pos).
- void freeze(int motor)
- This function will hold the motor in the current position until otherwise instructed. (**Note that position is based on BEMF and will drift; do not use for more than a second or so.**  The function off(int motor) is usually better for holding black motors.)

## How to Tell If a Motor Is Done Executing a Command

- int get_motor_done(int motor)
- This function will tell you if the motor is currently executing a command, returning a zero if it is and a one if it isn’t.
- void block_motor_done(int motor)
- This function will pause until the specified motor finishes its current command, so you can avoid sending commands to a motor before it finishes the last command.
- This function has a shorthand - bmd(int motor).

## A Simple Example

- #define LM 2
- #define RM 0
- void main()
- {  //Move forward for 2 seconds using the
-     // mav function
-     mav(LM, 900);
-     mav(RM, 900);
-     sleep(2.0);
- 	  ao(); // omitted on original slide
- }

## Using BMD and get_motor_done

- #define LM 2
- #define RM 0
- void main()
- {
-     //Move forward for 2000 ticks
-     mrp(LM,900, 2000L);
-     mrp(RM,900, 2000L);
-     bmd(RM);
-     //Move backward for 2000 ticks
-     mrp(LM, 900, -2000L);
-     mrp(RM, 900, -2000L);
-     // Pause until  get_motor_done returns a 1
- 	 while( !get_motor_done(RM))
-       {
-          printf(“motors running”);
- 	   sleep(.2); display_clear();
-  	}
- }

## Tips for Using the BEMF Encoders

- In general it is best to NOT run both motors at full velocity in case one motor cannot obtain full velocity.
- Try to match motors by using the BEMF encoder display to find two motors that return similar values for a full rotation.
- Do not forget to use the bmd or get_motor_done function when using position functions.
- The bmd and get_motor_done functions are not needed for the mav function.

## Multitasking

- Allows your XBC to do more than one thing at a time.
- Extremely valuable in Botball.
- Allows your robot to do tasks in the background while the main program runs.
- This saves you time.
- IC make multitasking EXTREMELY easy.

## IC Process information

- Separate processes work in parallel until they end or are killed.
- Each process that is active gets 50ms of processing time.
- Processes can communicate with one another by reading and modifying global variables.

## Multitasking functions

- int start_process(<*function_name*>(<*arg1*>, <*arg2*>, . . .));
- Used to get a process to start running in the back ground.
- Returns an int that is the process ID of the process.
- void kill_process(<*process_id*>);
- Stops the process indicated by process_id.
- defer();
- Causes a process to give up its remaining process time.

## A Practical Botball Example

- /*
- This is an example of using multitasking in IC
- The program will start a process that is designed to move
- a servo.  It will also move the robot forward WHILE the
- servo is being moved in the background.
- */
- #define ARM 0  //Servo port of the arm
- void main()
- {
-     int pid_arm;  // This variable will hold the ID of the raise_arm process
-     pid_arm= start_process(raise_arm()); // start raising arm in the background
-     forward();  // moves us forward while the arm is being moved.
-     sleep(3.0);
-     kill_process(pid_arm); //just incase raise_arm isn’t finished
-     ao();
- }

## Example Continued

- void raise_arm()
- {
-     int position;  // a counter to hold our servo position
-     for (position = 10; position < 180; position+=5)  //count from 10 to 180 in 5 step increments
-       {
-         set_servo_position(ARM,position);  // position our servo
-         sleep(0.11);  // This sleep slows things down a bit
-     }
- }
- void forward()
- {
-     motor(0,100);
-     motor(2,100);
- }

## The Engineering Process

- Define the problem and determine project requirements.
- Brainstorm solutions.
- Prototype solution.
- Test and observe.
- Determine cause of failings and brainstorm solutions.
- Go back to 4 and repeat until finished.

## Incremental Design

- Design and test a little at a time.
- Designing an entire solution and then implementing the whole solution without testing rarely works.
- Break problem into smaller problems.
- Design and test solutions to the smaller problems.
- Assemble smaller solutions into working solution.

## Tonight's Challenge

- You should have the arm built.
- Using what you know about IC, simple XBC vision, servos and motor control write a program that will:
- Seek out and find an orange ball.
- Grasp and pick up the orange ball.
- The solution to last weeks challenge will be VERY helpful.
- This is a big challenge, use incremental design!
- We will add to this challenge later!

## Possible Sub-problems to Solve

- Go out a fixed distance turn around and return [measure the repeatability by measuring the end points after careful positioning of the starting point and direction.]
- Go out to a ball/tribble at fixed position, about 3 feet away, and grab it; return to starting point and drop it. [note that both grabbing and lifting is needed to return reliably with the object.]
- Use vision to guide robot to a ball/tribble, about 3 feet away within the camera FOV, and grab it; return to starting point and drop it. [Set a color model to respond only to the target object; use the vision guidance function from the 6th class to direct the robot. Note the relation between the y track of a blob and how close it is.]