Beyond Basics Ex 7-11

Exercise 7: 'Sensor Blocks'

I have kept the original title for this exercise, 'Sensor Blocks', even though EV3 Basic doesn't use programming blocks like the Lego software does. 

Objective:

When the touch sensor is NOT pressed the robot will turn on the spot to the right at a speed that is proportional to the ambient light intensity. When the touch sensor IS pressed then the robot will stop moving for as long as the switch is pressed, and the status light will light up in red.

Solution:

Sensor.SetMode(3,1)  'set color sensor on port 3 to mode 1: ambient light level

Sensor.SetMode(1,0)  'set touch sensor on port 1 to mode 0

While "True"    'forever

    If Sensor.ReadPercent(1) = 0 Then    'touch sensor NOT pressed

        EV3.SetLEDColor("OFF","NORMAL")    'turn off LED

        P=Sensor.ReadPercent(3)   'get ambient light level

        'turn motors at opposite speeds to turn on the spot

        Motor.StartSync("BC",P,-P) 

    Else   ' touch sensor is pressed

        Motor.Stop("BC", "True")

        EV3.SetLEDColor("RED","NORMAL")    'no pulsing

    EndIf

EndWhile

Notes:

My solution is very different to the official EV3-G solution, shown below:

The official solution uses two threads which run simultaneously since they are both attached to a Start block. When the lower loop detects that the touch sensor has been pressed it issues a Stop Motor command but this seems to be in conflict with the Motor command in the top loop, which tells the motors to move. Presumably the Stop Motor command takes precedence, but how can it, since the stop motor command and the start motor command are in different loops and will be executed at different times? So this solution may work but is hard to understand and unnecessarily complex - why use two threads for this solution? 

Exercise 8: Text

This exercise is like exercise 5, but simpler. 

Objective:

The ultrasonic sensor will measure distance. The distance value will be displayed  in centimeters on the LCD screen followed by the string " cm". The text will be displayed in black in the 'Large' font (font 2). The display will be continuously updated with the current distance reading of the sensor.

Solution:

Sensor.SetMode(4,0)  'Set the US sensor on port 4 

'to mode 0: measure distance in mm (not cm)

While "True"    'forever

    LCD.StopUpdate() 'Don't update the screen before the text is ready

    LCD.Clear()

    LCD.Text(1,30,60,2,Sensor.ReadRawValue(4,0)/10 + " cm") ' convert to cm

    LCD.Update()

    Program.Delay(100)  'Don't update the display too frequently

EndWhile

Notes:

Exercise 9: Range

Objective:

Make the robot follow an object that is moving away from it - it should move forwards towards the object as long as the object is between 10 and 20 cm in front of the robot but will stop moving if it gets closer than 10 cm, thereby avoiding a collision. It will also stop moving (give up) if the leading object is more than 20 cm in front of the robot.

Solution:

Sensor.SetMode(4,0)  'will set the US sensor on port 4 

'to mode 0: measure distance in mm (not cm)

While "True"     'forever

    Distance=Sensor.ReadRawValue(4,0)/10  'convert to cm

    If Distance >10 and Distance <20 Then

        Motor.Start("BC",50)

    Else

        Motor.Stop("BC","True")

    EndIf

EndWhile

Exercise 10: Math - Basic

Objective:

This program will calculate how many wheel rotations are necessary to make the robot move forward 50 cm, then it will make the robot move, then it will calculate the speed of the robot during the motion.

Solution:

Each time a robot wheel turns through one rotation the entire circumference of the wheel rolls along the ground. The diameter of the EV3 (Education version) rubber tires is about 5.6 cm so the circumference is 5.6 cm * π = 5.6 cm * 3.14 = 17.6 cm. To find the number of rotations necessary to move the robot forward 50 cm we must divide 50 cm by the distance moved in each rotation of the wheel (equal to the circumference of the wheel) which is 17.6 cm. Having found the needed number of rotations we can convert this into the corresponding number of degrees by multiplying by 360.

'NumRots is number of wheel rotations needed for robot to advance 50 cm

NumRots=50/17.6        'Wheel circumference = 17.6 cm

StartTime=EV3.Time   'Time in milliseconds since program was started.

Motor.Move("BC",40, NumRots*360, "True")   'convert rotations to degrees

TravelTime=(EV3.Time - StartTime)/1000    'convert milliseconds to seconds

Speed= Math.Round(50/TravelTime)  'Speed = distance in cm / time in s

LCD.Clear()

LCD.Text(1,35,55,2,Speed + " cm/s")

Program.Delay(5000)    'Give enough time for the screen to be read

Notes:

Here is the official EV3-G solution to the same problem:

Ask yourself which solution looks neater, taking into account the fact that the EV3-G solution has no comments...

Exercise 11: Gyro - rate

Objective:

The program should continuously (every 0.5 seconds) display the rate of turn of the gyro sensor in degrees per second.

Solution:

Recall that whenever you use the gyro sensor it is vitally important to keep the sensor absolutely stationary when the EV3 brick is powered up or the gyro sensor is plugged in, otherwise the gyro reading will continually wander away from the correct value.

The instantaneous rate of turn is calculated by dividing the angle turned by the time taken, with the time taken being very short (an 'instant'). In mode 1, the gyro sensor will calculate this for you (in degrees per second) and you can obtain the value with Sensor.ReadRawValue(port number,0).

Sensor.SetMode(2,1)   'Set gyro sensor on port 2 to mode 1: rate of turn

While "True" 

    LCD.StopUpdate()

    LCD.Clear()

    LCD.Text(1,35,55,2,Sensor.ReadRawValue(2,0)+" deg/s")

    LCD.Update()

    Program.Delay(500)   'Don't update the display too frequently

EndWhile

Notes:

You are now ready to tackle Beyond Basics exercises 12-18.