No logic, no move

The aim of the lesson is to show the use of concepts learned during the math lesson where we use logic. Logic is close with computer science. We will use the truth table - this is a mathematical table used in logic. A truth table has one column for each input variable (for example, P and Q), and one final column showing all of the possible results of the logical operation that the table represents.

Preparing For This Tutorial:

Time constraints:

Exercise

  1. Write a program that will start the robot when two conditions are met simultaneously (e.g. holding down the touch sensor and the color sensor detects the red color).The choice of sensors is up to you Remember that 0 means FALSE and 1 means TRUE.

  2. Here are some logical phrases. Test all of them. Which one gives the same results?

\(\neg(p\ \land q)\), \((\neg\text{\ p\ } \vee \neg q)\), \(\neg(p \vee q),\ \)\((\neg\text{p\ } \land \neg q)\).

  1. Test the examples below:

\(\lbrack(p \land q) \vee r\rbrack \Leftrightarrow \lbrack p \land (q \vee r)\rbrack\), \(\lbrack(p \land q) \vee r\rbrack \Leftrightarrow \lbrack(p \vee r) \land (q \vee r)\rbrack\), \(\lbrack(p \vee q)\ \land r\rbrack \Leftrightarrow \lbrack p \vee (q \land r)\rbrack\), \(\lbrack(p \vee q) \land r\rbrack \Leftrightarrow \lbrack(p \land r) \vee (q \land r)\rbrack\),

Theory

Part 1

Exercise. Write a program that will start the robot when two conditions are met simultaneously (e.g. holding down the touch sensor and the color sensor detects the red color).The choice of sensors is up to you Remember that 0 means FALSE and 1 means TRUE.

Computer science:

  1. Let’s try to make a sound when you press the touch sensor. Below you have the pseudocode for the program - write it in Python

Import the necessary library

if the touch button is pressed:

makes a sound

  1. Let’s try to make a sound when you press the touch sensor and the color sensor at the same time. Below you have the pseudocode for the program - write it in Python

Import the necessary library

Create the variables related to the sensors you use and motors

Create two variables named p and q and assign them a value equals 0.

#!/usr/bin/env python3

while True:

  if p and:

  terminate the loop

  else:

    The LEDs are red

  if touch sensor is pressed:

    put p equals 1

  else:

    put p equals 0

  if color sensor recognize red color:

    put q equals 1

  else:

    put q equals 0

    The LEDs are green

  The robot drives forward for 30 seconds.

END

Try following modifications

#!/usr/bin/env python3

if p or q:

  terminate the loop

else:

  The LEDs are red

Describe the difference between the conjunctions ,,and” and ,,or”.

Try following modifications

#!/usr/bin/env python3

if not p:

  terminate the loop

else:

  The LEDs are red

Describe the results.

Try following modifications

#!/usr/bin/env python3

if not (p and q):

  terminate the loop

else:

  The LEDs are red

if not (p and q):

  The LEDs are red

else:

  terminate the loop

Which gave the same result as has been received in the basic example? Why?

Part 2

Exercise

In the previous lesson you have the following code:

#!/usr/bin/env python3

if not (p and q):

  The LEDs are red

else:

  terminate the loop

Here are some logical phrases. Test all of them. Which one gives the same results?

\(\neg(p\ \land q)\), \((\neg\text{\ p\ } \vee \neg q)\), \(\neg(p \vee q),\ \)\((\neg\text{p\ } \land \neg q)\).

We have just tested de Morgan’s laws.

Write your results.

Try complete the truth table presented below

p

q

p and q

not(p and q)

not p

not q

not p or not q

0

0

0

1

1

1

1

0

0

1

0

1

1

0

1

1

1

0

0

Part 3

Exercise.

How about adding one more sensor (ultrasonic sensor)?

You could test the examples below:

\(\lbrack(p \land q) \vee r\rbrack \Leftrightarrow \lbrack p \land (q \vee r)\rbrack\), \(\lbrack(p \land q) \vee r\rbrack \Leftrightarrow \lbrack(p \vee r) \land (q \vee r)\rbrack\),

\(\lbrack(p \vee q)\ \land r\rbrack \Leftrightarrow \lbrack p \vee (q \land r)\rbrack\), \(\lbrack(p \vee q) \land r\rbrack \Leftrightarrow \lbrack(p \land r) \vee (q \land r)\rbrack\),

Short help on programming

Commands/functions needed for the exercise

Connect large motor to port B

b = ev3.LargeMotor(‘outB’)

Plug a touch sensor into any sensor port

ts = ev3.TouchSensor()

Gives the information if the sensor is pressed

ts.value()

Plug a color sensor into any sensor port

cs = ColorSensor()

Set mode of color sensor

cs.mode = ‘COL-COLOR’

Detect the color. Gives the information about the color (5 means - red color)

cs.value()==5:

Sound.speak(‘Go!’).wait()

Put red color to left LED

Leds.set_color(Leds.LEFT, Leds.RED)

Run to a position relative to the current position value. The new position will be current position + position_sp. When the new position is reached, the motor will stop using the action specified by stop_action.

run_to_rel_pos(position_sp=<angle in degrees>, speed_sp=<value>,stop_action=<value>)

On the example one rotation of wheels:

#!/usr/bin/env python3

speed_sp is between 0 and 1000

position_sp >0 Motor will run forward

position_sp <0 Motor will run backwards

stop_action - can be "coast"or "brake"

b.run_to_rel_pos(position_sp=200,

speed_sp=200,stop_action= "brake ")
Next Section - Acceleration Due to Gravity with Lego EV3 & Python