Skip to content

Can't use screen AND motors ? #276

Closed
@romainschmid

Description

@romainschmid
  • **ev3dev version: 4.4.32-17-ev3dev-ev3
  • **ev3dev-lang-python version: python_ev3dev-0.8.0.post4-py2.7.egg (not sure this is what's wanted)

Hi,

I can't figure what the problem is with my code : I want to calibrate my light sensor, in order to follow a black line. I want the robot to oscillate 4 times, so that the sensor can update the max_white and max_black values.
I have succeeded using the lcd screen and the motors, but separately. If I put all my functions in the same file, it will not work, but if I use the functions in different files, the code works.
In fact, the motors will run, but the display isn't even changed from its original state, and the values are not updated.

I am not exactly sure if it's a bug somewhere, or if it's due to my very beginner level in programming.

Here is my code :

#!/usr/bin/python3
#  -*- coding: utf-8 -*-
from ev3dev.ev3 import *
from  PIL import ImageFont
from threading import Thread
from time import sleep
lcd = Screen()
globalstop = 0
ts = TouchSensor()
color_sensor = ColorSensor()
color_sensor.mode='COL-REFLECT'
motor_left = LargeMotor('outB')
motor_right = LargeMotor('outC')
f = ImageFont.truetype('/usr/share/fonts/truetype/msttcorefonts/Arial.ttf', 15)
max_white = 0
max_black = 100

def motors():
    global globalstop
    sleep(0.5)
    for i in range(4):
        if i % 2 == 0:
            motor_right.run_to_rel_pos(position_sp=180, speed_sp=200, stop_action='brake')
            motor_left.run_to_rel_pos(position_sp=-180, speed_sp=200, stop_action='brake')
            motor_right.wait_while('running')
            motor_left.wait_while('running')

        else:
            motor_right.run_to_rel_pos(position_sp=-180, speed_sp=200, stop_action='brake')
            motor_left.run_to_rel_pos(position_sp=180, speed_sp=200, stop_action='brake')
            motor_right.wait_while('running')
            motor_left.wait_while('running')
        print (i+1)
    globalstop = 1

motors = Thread(target=motors())


def display_and_update():
    global max_white, max_black, globalstop
    while globalstop == 0:
        lcd.clear()
        lcd.draw.text((10, 10), "max_white : %s" % max_white,font=f)
        lcd.draw.text((10, 40), "max_black : %s" % max_black,font=f)
        lcd.update()
        if max_white < color_sensor.value():
            max_white = color_sensor.value()
        if max_black > color_sensor.value():
            max_black = color_sensor.value()

disp_and_update = Thread(target=display_and_update())

#start the threads
motors.start()
disp_and_update.start()

#finally, output the values
print ("max_white : ", max_white, "max_black : ", max_black)

Any help appreciated !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions