QuanserMechatronicSensorsTrainerDisplay
- class quanser.devices.interfaces.QuanserMechatronicSensorsTrainerDisplay
A Python wrapper for the Quanser Devices API interface to the Quanser Mechatronic Sensors Trainer USB display.
Example
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> display = QuanserMechatronicSensorsTrainerDisplay()
- beginDraw()
Put the LCD display into drawing mode so that the display is not updated until endDraw is called. This allows both graphics and text to be mixed on the display as an atomic update. If this function is not called then the LCD display updates immediately.
Example
Draw an image and then write text on top of it.
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> img = cv2.imread('Sample.png', cv2.IMREAD_COLOR) >>> message = "Hello, world!" >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> display.beginDraw() >>> display.drawImage(0, 0, img.shape[0:2], ImageFormat.ROW_MAJOR_INTERLEAVED_BGR, image) >>> display.printText(5, 0, message) >>> display.endDraw() >>> ... ... >>> display.close()
Redirect a camera to the LCD display and superimpose the frame rate on the image.
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> from quanser.multimedia import VideoCapture, ImageFormat, ImageDataType >>> import numpy as np >>> import time >>> >>> fps = 20.0 >>> image_width = 800 >>> image_height = 480 >>> image_data = np.zeros((3, image_width, image_height), dtype = np.uint8) >>> >>> period = 1.0 / fps >>> >>> capture = VideoCapture("video://localhost:0", fps, image_width, image_height, ImageFormat.COL_MAJOR_PLANAR_RGB, ImageDataType.UINT8, None, 0) >>> >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> display.setDarkMode(True) >>> display.setRotation(True) >>> >>> capture.start() >>> >>> try : >>> while True : >>> capture.read(image_data) >>> >>> display.beginDraw() >>> >>> display.drawImage(0, 0, (image_height, image_width), ImageFormat.COL_MAJOR_PLANAR_RGB, image_data) >>> >>> message = "FPS: %.0f" % fps >>> display.printText(11, 0, message) >>> >>> display.endDraw() >>> time.sleep(period) >>> finally: >>> display.close() >>> capture.stop() >>> capture.close()
- clear()
Clear the screen to the background color.
Examples
Clear the screen.
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> display.clear() >>> ... ... >>> display.close()
- close()
Close the display.
Example
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> ... ... >>> display.close()
- drawImage(pixel_row, pixel_column, image_size, image_format, image)
Draw an image on the display at the given pixel coordinates. The display is not cleared before drawing the image, so multiple images may be drawn at different locations on the display.
- Parameters
pixel_row (int) – The pixel row at which to place the top edge of the image. A value of 0 corresponds to the top of the display.
pixel_column (int) – The pixel column at which to place the left edge of the image. A value of 0 corresponds to the left edge of the display.
image_size ((int, int)) – A tuple containing the height and width of the image as (height, width).
image_format (ImageFormat) – The image format. For OpenCV images, the format should either be ImageFormat.ROW_MAJOR_INTERLEAVED_BGR for color or ImageFormat.ROW_MAJOR_GREYSCALE for grayscale. For MATLAB images, the format should either be ImageFormat.COL_MAJOR_PLANAR_RGB for color or ImageFormat.COL_MAJOR_GREYSCALE for grayscale.
image (image) – The image to display in the image format specified by the image_format parameter.
Examples
Draw a bitmap on the LCD display.
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> import cv2 >>> img = cv2.imread('Sample.png', cv2.IMREAD_COLOR) >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> display.drawImage(0, 0, img.shape[0:2], ImageFormat.ROW_MAJOR_INTERLEAVED_BGR, img) ... >>> display.close()
- drawImageWithBlend(pixel_row, pixel_column, image_size, image_format, image, mask)
Draw an image on the display at the given pixel coordinates using the given mask as the alpha component of the image. The image is blended with the current screen contents according to the mask.
The display is not cleared before drawing the image, so multiple images may be drawn at different locations on the display.
- Parameters
pixel_row (int) – The pixel row at which to place the top edge of the image. A value of 0 corresponds to the top of the display.
pixel_column (int) – The pixel column at which to place the left edge of the image. A value of 0 corresponds to the left edge of the display.
image_size ((int, int)) – A tuple containing the height and width of the image as (height, width).
image_format (ImageFormat) – The image format. For OpenCV images, the format should either be ImageFormat.ROW_MAJOR_INTERLEAVED_BGR for color or ImageFormat.ROW_MAJOR_GREYSCALE for grayscale. For MATLAB images, the format should either be ImageFormat.COL_MAJOR_PLANAR_RGB for color or ImageFormat.COL_MAJOR_GREYSCALE for grayscale.
image (image) – The image to display in the image format specified by the image_format parameter.
mask (image) – The mask used to blend the image with the current display contents. A mask pixel of 0 will not write that pixel to the display and a mask pixel value of 255 will write that image pixel to the display. Any mask pixel value in between blends the image with the display contents proportional to the mask pixel value. The mask must be in the grayscale format corresponding to the given image_format.
Examples
Draw a bitmap on the LCD display using blending.
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> import cv2 >>> img = cv2.imread('Sample.png', cv2.IMREAD_COLOR) >>> mask = cv2.imread('Mask.png', cv2.IMREAD_GRAYSCALE) >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> display.drawImageWithBlend(0, 0, img.shape[0:2], ImageFormat.ROW_MAJOR_INTERLEAVED_BGR, img, mask) ... >>> display.close()
- drawImageWithMask(pixel_row, pixel_column, image_size, image_format, image, mask)
Draw an image on the display at the given pixel coordinates using the given mask. The image is written to the display wherever the mask pixels are non-zero.
The display is not cleared before drawing the image, so multiple images may be drawn at different locations on the display.
- Parameters
pixel_row (int) – The pixel row at which to place the top edge of the image. A value of 0 corresponds to the top of the display.
pixel_column (int) – The pixel column at which to place the left edge of the image. A value of 0 corresponds to the left edge of the display.
image_size ((int, int)) – A tuple containing the height and width of the image as (height, width).
image_format (ImageFormat) – The image format. For OpenCV images, the format should either be ImageFormat.ROW_MAJOR_INTERLEAVED_BGR for color or ImageFormat.ROW_MAJOR_GREYSCALE for grayscale. For MATLAB images, the format should either be ImageFormat.COL_MAJOR_PLANAR_RGB for color or ImageFormat.COL_MAJOR_GREYSCALE for grayscale.
image (image) – The image to display in the image format specified by the image_format parameter.
mask (image) – The mask used to determine which pixels are written to the display. A mask pixel of 0 will not write that pixel to the display and a non-zero mask pixel value will write that image pixel to the display. The mask must be in the grayscale format corresponding to the given image_format.
Examples
Draw a bitmap on the LCD display using blending.
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> import cv2 >>> img = cv2.imread('Sample.png', cv2.IMREAD_COLOR) >>> mask = cv2.imread('Mask.png', cv2.IMREAD_GRAYSCALE) >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> display.drawImageWithBlend(0, 0, img.shape[0:2], ImageFormat.ROW_MAJOR_INTERLEAVED_BGR, img, mask) ... >>> display.close()
- endDraw()
Take the LCD display out of drawing mode. The display will be updated at this point.
Example
Draw an image and then write text on top of it.
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> img = cv2.imread('Sample.png', cv2.IMREAD_COLOR) >>> message = "Hello, world!" >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> display.beginDraw() >>> display.drawImage(0, 0, img.shape[0:2], ImageFormat.ROW_MAJOR_INTERLEAVED_BGR, img) >>> display.printText(5, 0, message) >>> display.endDraw() >>> ... ... >>> display.close()
- getTouchInformation(touch)
Gets information about the touch panel state for the Quanser Mechatronic Sensors Trainer USB display. This method is non-blocking.
- Parameters
touch (LCDTouch) – An LCDTouch object into which the touch information is written. See the LCDTouch documentation for details.
- Return type
Returns 1 if new touch information is available and 0 otherwise.
Example
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay, LCDTouch >>> import time >>> >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> touch = LCDTouch() >>> for i in range(1, 1000): >>> new = display.getTouchInformation(touch) >>> if new: >>> print(f"# fingers: {touch.num_fingers} x: {touch.fingers[0].r} y: {touch.fingers[0].c}") >>> time.sleep(0.01) ... >>> display.close()
- open(uri='lcd://localhost:0')
Opens the Quanser Mechatronic Sensors Trainer USB display.
- Parameters
uri (string) – The URI indicating the communication channel with which to communicate with the display. For the Quanser Mechatronic Sensors Trainer USB display, the URI should be “lcd://localhost:0”. If this parameter is not specified, the default URI is set for the Quanser Mechatronics Sensors Trainer USB display.
Example
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> ... ... >>> display.close()
- printText(line, column, message, length=2147483647)
Print to the display.
- Parameters
line (int) – The line where the message will be displayed. Line 0 is the first line.
column (int) – The column where the message will be displayed. Column 0 is the first column.
message (string) –
The message to display. Unrecognized characters are skipped. If more than 16 characters would be printed on a line then the line is truncated. A newline will cause the characters to be displayed on the next line. As there are only two lines on the display there should only be one newline in the format string. Printing starts at the given line and column. Line 0 is the first line and column 0 is the first column.
The format string is UTF-8 and does support Unicode (particularly, Katakana characters).
length (int) – The maximum length of the message in characters.
Example
Print ‘Hello, World!’ to the top, left corner of the display.
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> message = "Hello, world!" >>> display.printText(0, 0, message) >>> ... ... >>> display.close()
- save(filename)
Save the current contents of the display to a PBM image file. The filename should have a .pbm extension.
- Parameters
filename (string) – The filename to which to write the display contents.
Examples
Save the current contents of the LCD display.
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> import cv2 >>> img = cv2.imread('Sample.png', cv2.IMREAD_COLOR) >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> display.drawImage(0, 0, img.shape[0:2], ImageFormat.ROW_MAJOR_INTERLEAVED_BGR, img) >>> display.save("lcd_display.ppm") ... >>> display.close()
- setCharacter(code, pattern)
Defines the character associated with the given character code.
- Parameters
code (int) – Defines the character associated with the given character code. Valid character codes are 0x10 to 0x17 i.e., ‘’ to ‘’. Using these characters will produce the bitmap defined in the pattern for that character.
pattern (bytes) –
The pattern defines each line of the character being defined, with the sixteen bits in each word defining the pixels of that line. For example to define the letter T, the pattern would be:
- pattern = np.array([0x0000, 0x0000, 0x0000, 0X1FF0, 0x0100, 0x0100, 0x0100, 0x0100, 0x0100, 0x0100, 0x0100, 0x0100,
0x0100, 0x0100, 0x0100, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000], dtype=np.uint16)
0x0000, # 0b0000000000000000 # # # 0x0000, # 0b0000000000000000 # # # 0x0000, # 0b0000000000000000 # # # 0x1ff0, # 0b0001111111110000 # # ……… # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0000, # 0b0000000000000000 # # # 0x0000, # 0b0000000000000000 # # # 0x0000, # 0b0000000000000000 # # # 0x0000, # 0b0000000000000000 # # # 0x0000 # 0b0000000000000000 # # #
Bit 0 is the rightmost pixel and bit 15 is the leftmost pixel. The baseline is in pattern[14]. The top of a typical character is in pattern[3], although some characters go higher.
Examples
Print a custom character.
Using array:
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> character_code = 0o20 >>> pattern = array('H', [0x0000, 0x0000, 0x0000, 0X1FF0, 0x0100, 0x0100, 0x0100, 0x0100, 0X1FF0, 0x0100, 0x0100, 0x0100, 0x0100, 0x0100, 0X1FF0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000]) >>> display.setCharacter(character_code, pattern) >>> message = str(chr(character_code)) >>> display.printText(0, 0, message) >>> ... ... >>> display.close()
Using numpy:
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> import numpy as np >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> character_code = 0o20 >>> pattern = np.array([0x0000, 0x0000, 0x0000, 0X1FF0, 0x0100, 0x0100, 0x0100, 0x0100, 0X1FF0, 0x0100, 0x0100, 0x0100, 0x0100, 0x0100, 0X1FF0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000], dtype = np.uint16) >>> display.setCharacter(character_code, pattern) >>> message = str(chr(character_code)) >>> display.printText(0, 0, message) >>> ... ... >>> display.close()
- setDarkMode(dark)
Determine whether to use dark mode for the display.
- Parameters
dark (bool) –
True
to enable dark mode i.e., white text on a black background;False
to use normal mode i.e., black text on a white background.
Examples
Print “Hello, world!” in dark mode.
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> display.setDarkMode(True) >>> message = "Hello, world!" >>> display.printText(0, 0, message) >>> ... ... >>> display.close()
- setRotation(rotate)
Determine whether to rotate the display 180 degrees.
- Parameters
rotate (bool) –
True
to rotate the displayFalse
to use the normal orientation
Examples
Print “Hello, world!” in the opposite orientation.
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> display.setRotation(True) >>> message = "Hello, world!" >>> display.printText(0, 0, message) >>> ... ... >>> display.close()
- setTextColor(red, green, blue)
Set the current color used for text. This setting only affects newly printed text.
- Parameters
red (int) – The red component as an integer between 0 and 255.
green (int) – The green component as an integer between 0 and 255.
blue (int) – The blue component as an integer between 0 and 255.
Examples
Print “Hello, world!” in dark mode.
>>> from quanser.devices import QuanserMechatronicSensorsTrainerDisplay >>> display = QuanserMechatronicSensorsTrainerDisplay() >>> display.open("lcd://localhost:0") >>> display.setTextColor(255, 0, 0) >>> message = "Hello, world in red!" >>> display.printText(1, 0, message) >>> display.setTextColor(0, 255, 0) >>> message = "Hello, world in green!" >>> display.printText(2, 0, message) >>> display.setTextColor(0, 0, 255) >>> message = "Hello, world in blue!" >>> display.printText(0, 0, message) >>> display.setTextColor(64, 96, 107) >>> message = "Hello, world in mystery color!" >>> display.printText(0, 0, message) >>> ... ... >>> display.close()