Robot Navigation - Blocks
As alluded to in the Hello Robot - Robot Control section, robot control comes in many different forms. One of the control types to consider for robots with drivetrains, is robot navigation.
Robot navigation as a concept is dependent on the type of drivetrain and the type of operation mode. For instance, the code to control a mecanum drivetrain differs from the code used to control a differential drivetrain. There is also a difference between coding for teleoperated driving, with a gamepad, or coding for autonomous, where each movement of the robot must be defined within code.
The following section goes through some of the basics of programming for a differential drivetrain, as well as how to set up a teleoperated arcade style drivetrain code. The concepts and logic highlighted in this section are applicable to autonomous control, including the section Elapsed Time.
Goals of Section
What to consider when programming drivetrain motors and how to apply this to an arcade style teleoperated control.
For controlling the Class Bot V2 drivetrain, being able to control two motors simultaneously is important. This is done through the dual motor block within Blocks. To access the dual motor block, at the top of the Categorize Blocks section there is a drop down menu for Actuators. Selecting DcMotor will drop down the options Dual and another drop down menu Extended. Select Dual to access the dual motor blocks.
block to op mode while loop.
When there are multiple of the same type of variable (such as multiple Dc Motor variables) the variable specific blocks will choose a default variable based on alphabetical order. For this example Op Mode Dc Motor blocks will default to the arm variable.
Use the variable drop down menu on the block to change from arm to rightmotor.
Before moving on try running the code as is and consider the following questions:
- What behavior is the robot exhibiting?
- What direction is the robot spinning in?
When motors run at different speeds they spin along their center pivot point. But the motors are both set to a power (or duty cycle) of 1?
DC Motors are capable of spinning in two different directions depending on the current flow: clockwise and counter clockwise. When using a positive power value the Control Hub sends current to the motor for it to spin in a clockwise direction.
With the Class Bot and current code, both motors are currently set to run in the clockwise direction. If you set the robot on blocks and run the code again though, you can see that the motors run in opposing directions. With the mirrored way the motors mount to the drivetrain, one motor is naturally the inverse of the other.
Why would the inverse motor cause the robot to spin in a circle? Both speed and direction of rotation of the wheels impact the overall direction the robot moves in. In this case, both motors were assigned to have the same power and direction but how the motors transfer motion to the wheels, causes the robot to spin instead of moving forward.
Check the Introduction to Motion section for more information on the mechanics of transferring motion and power.
In the info block above you were asked to determine which direction the robot spun in. The robot pivots in the direction of the inversed motor. For instance, when the right motor is the inversed motor the robot will pivot to the right. If the left motor is the inversed motor the robot will pivot to the left.
For the Class Bot, the robot pivots to the right, so the right motor will be reversed. Add
to the op mode class, under the
block changes the direction of the right motor so that both motors will run in the same direction when power is set to one.
Recall that when the motors were running in opposing directions the robot spun in circles. This same logic will be used to control the robot using the arcade style of control mentioned in the Hello Robot - Autonomous Robot section.
To start, create two variables
. Add the
blocks to the while loop.
will be assigned as
, which is the y-axis of the right joystick.
Remember positive/negative values inputted by the gamepad's y-axis are inverse of the positive/negative values of the motor.
, which is the x axis of the right gamepad joystick. The x-axis of the joystick does not need to be inverted.
block sets assign values from the gamepad joystick to
. As previously mentioned, the joystick gives values along a two dimension coordinate system.
receives the value from the y- axis and
receives the value from the x-axis. Both axis output values between -1 and 1.
To better understand consider the following table. The table shows the expected value generated from moving the joystick all the way in one direction, along the axis. For instance, when the joystick is pushed all the way in the upwards direction the coordinate values are (0,1).
The table below assumes that the the
values from the gamepad have been inverted in code when assigned to the variable
Now that you have a better understanding of how the physical movement of the gamepad affects the numerical inputs given to your Control System; its time to consider how to control the drivetrain using the joystick. Recall, from the Programming Drivetrain Motors section, that the speed and direction of a motor plays a large part in how the drivetrain moves. The numerical outputs for
determine the speed and direction of the motors. For instance, when both motors are set to 1 they move in the forward direction at full speed (or 100% of duty cycle).
Much like the gamepads, the numerical value for
setPoweris in a range of -1 to 1. The absolute value of the assigned number determines percentage of duty cycle. As an example, 0.3 and -0.3 both indicate that the motor is operating at a duty cycle of 30%. The sign of the number indicates the direction the motor is rotating in. To better understand, consider the following graphic.
When a motor is assigned a
setPowervalue between -1 and 0, the motor will rotate in the direction it considers to be reverse. When a motor is assigned a value between 0 and 1, it will rotate forward.
In the Programming Drivetrain Motors section, it was discussed that a robot rotates when the motors are moving in opposing directions. However, this has more to do with both speed and direction. To think of it numerically, a differential drivetrain will turn to the right when the
setPowervalue for the right motor is less than that of the left motor. This is exhibited in the following example.
When both motors are
the robot will run at full speed in a mostly straight line. However, when the rightmotor is running in the same direction but at a lower speed, such as
the robot will turn or rotate to the right. This is likely to be an arching movement that is not as sharp as a full pivot. In contrast when the rightmotor is set to full speed but in the opposite direction of the leftmotor, the robot pivots to the right. So, mathematically the following is considered to be true:
rightMotor = leftMotor
Forward or Reverse
rightMotor > leftMotor
rightMotor < leftMotor
As previously implied,
send values to the Control System from the game pad joystick. In contrast,
interprets numerical information set in the code and sends the appropriate current to the motors to dictate how the motors behave.
In an arcade drive, the following joy stick inputs (directions) need to correspond with the following outputs (motor power values).
To get the outputs expressed in the table above, the gamepad values must be assigned to each motor in a meaningful way, where. Algebraic principles can be used to determine the two formulas needed to get the values. However, the formulas are provided below.
From the math menu grab the
blocks and add them to the respective motor in the
Next add the
to the formula blocks.
With this you now have a functional teleoperated arcade drive!