Tetrix Gamepads. Distance Sensor for a calm control of the robot Arm
ProPreview

By using the GamePad to control the arm with could lift it and return it back. The main problem with controlling the arm is that if you just use the motors you would hit the robot, bend the axles and breaks the gear wheels. We would use the distance sensor to make the arm move without hitting the robot.

Bent axle

This happens because the force applied by the gear mechanism on the arm is too large and when the arm is at the lowest position it would force the axle to bend.

Optical Distance Sensor

We place the optical distance sensor on the arm. When the arm is lowered the sensor detects the distance between the arm and the robot and from there we could control the speed with which the arm lowers.

Values of the Optical Distance Sensor

Without much details, when the distance sensor is close to the object it returns a value close to 1. When it is raw away it returns a value close to 0.

When lowering the arm it is important not to hit the robot. This is a simple program for controlling the speed of lowering the arm. It uses the distance sensor.

English

Using the gamepad to control our arm in the PushBot we can lift the arm and we can return it back. The problem is that if you just use the motors, you'll hit the body of the robot with the arm. And I've shown you in previous videos when this happens because the motor is quite powerful and because of the system of gears and at the end you have the arm hitting the robot. And what happens is that this axle here after a lot of use it bends. And it could break. Just for the camera so that you can see it if I place a sheet of paper like this, you can see that the axle is bent. And what I would like to do in this video is to use the distance sensor to make the arm move down without hitting the BoxRobot like this. Again.

And even though I continue pushing the trigger the arm does not move down. Let's see how we do this in the code. First, where is the distance sensor? The distance sensor is mounted right here on the arm and if I lift the arm, you can see the distance sensor. It's right here. So, as the arm moves down it will detect the distance between the arm and the body of the robot. And it's a good idea to mount it right here because here the speed with which the arm is lowered will be less than mounting it somewhere for example, here. It's difficult to show it on the camera but if we mount the distance sensor on this hole, the speed with which this hole moves and it will hit the body of the robot will be larger and it will be more difficult to use the values of the distance sensor. So, we mount the distance sensor close to the system of gears. As we lower it detects the distance between the arm and the body of the robot. Next is the cable management of the distance sensor. This here is the cable of the distance sensor and it moves through the arm, then it goes to right here. It's the only sensor that we have on our robot and you can see it attached let me just hold the robot like this you can see it attached right here. So, we have the distance sensor attached to the robot and it is time now to program the distance sensor. A few words before that. What does the sensor detect? We are not going to enter into details about the specific values of the sensor but the basic idea is that when you are very close to an object the distance sensor returns a value of 1. And when you are too far from an object it returns a value of 0. So, a larger value means that you are closer to the object while a smaller values means that you are far away from an object. And in a specific tutorial for the distance sensor we'll introduce you to the equations that we use for converting this to centimeters and for actually using the distance sensor but for now we can just know that 1 means that we are very close to an object like 1 mm away from an object while 0 means we are too far from an object like a few centimeters away from an object. What we'll do is to start from the previous program for just moving the arm and we need to create a new OpMode. I already have the OpMode implemented but I'll implement it a second time so that you can see it. I will copy this OpMode and I'll paste it right here. And I'll call it VideoSimpleCalm because it is implemented during the video. Then we change the name in the annotation before the class and the name is VideoSimpleCalm. And this is the name of our new linear OpMode. Next we must declare a variable and this variable is from type OpticalDistanceSensor and I'll just call it 'ods' and it's equal to NULL. We have a variable declared OpticalDistanceSensor. Next step. Right here when we initialize the variables from the hardwareMap we must initialize the OpticalDistanceSensor from hardwareMap not dcMotor but actually OpticalDistanceSensor get and on the robot station we've named this sensor ods. This is how we know the name from the robot station and it's configured on the robot station. Then we move down here where we set the power of the motor. And I'll just extract this to a new variable and this variable is like armPower equals to the previous calculation where we just use the left trigger and the right trigger. Now, we need a new variable that's called armPower and it's from type double armPower = 0. And what we should do is to somehow include the value from the OpticalDistanceSensor right here. So, currently the power of the arm depends only on the gamepad, its left trigger and right trigger and what we should do is to include the OpticalDistanceSensor in this calculation and the value that it returns. Recap for the current video. We create a new OpMode, we change the name in the annotation, we declare a variable OpticalDistanceSensor, then we initialize this variable OpticalDistanceSensor and we extract the variable armPower where we set the value for armPower depending just on the gamepads. Next step. We declare a new variable that's called OpticalDistanceSensorValue odsv = 0 and right here we'll set the value to the real value that the OpticalDistanceSensor detects. odsv = ods.getLightDetected(); and from here we have the percentage of light detected. Now, if we have 1, this means that we are very close to the object. If we have 0, this means that we are kind of away from the object. I won't enter into details of the specific values I'll just use them as a percentage. Our next task is to actually incorporate the ods into this calculation. And you can think of many different ways to do this and in some of the next videos I'll provide you with the source of a nice calculation that we've done but just as an example of the principle how this works is the following. We calculate the armPower based on the gamepads and then we check if armPower is less than 0 this means we are returning back to the robot so we are lowering the arm and in the same time odsv is larger that 0.3. Then what we take is we must reduce the power of the arm. And I'll just set armPower = armPower / 30. I don't know why, just a value. You can experiment. So, if we are getting closer to the robot, we reduce the power for the arm. And if we are very close to the robot, so armPower is less than 0 and odsv is greater that 0.8 then we set armPower to 0.

This means we would like to stop there. And this is the principle. You add a check for the OpticalDistanceSensorValue and depending on the value and depending on the distance you change the power that's passed to the arm motor. Now it's time to download and see how this works. I'll move the arm up and now when I lower the arm with the trigger it stops. Again. When it detects a value that is greater than 0.8 it just stops. So, this is a good example of how you can use the distance sensor - the optical distance sensor and the value returned from this sensor to preserve the robot and not to break this robot. What we'll actually do in some of the next videos is to modify our program and when we start

it will just reduce the speed and it will get to the bottom but very slightly with a lower speed. And for this we have a program but we also need to refactor the current code, extract some of the classes and continue from there to improve this program. Of course, you can come up with many solutions because the distance sensor could be placed in different places or you can have an entirely different construction but the principle is the same. If you happen to use the distance sensor for actually lowering the arm and not hitting on the robot don't forget to send us videos and pictures of how you use it so that we can comment on them and return some feedback.

Hey, this is only a preview of this Pro episode.

In order to watch the full video, you'll have to subscribe to FLLCasts.