liveBook · Manning

Y-axis event

As explained in Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi

An axis event is a software input event that reports a change in the position of a joystick control axis.

A y-axis event reports that a joystick stick moved vertically. Each stick has x and y axes: vertical movement produces y-axis events, while horizontal movement produces x-axis events. For a two-motor robot, the left and right sticks can independently control forward and backward motion, with stick distance from center setting throttle level. section 7.3

Figure 7.5 Joystick events: the sticks generate y-axis events when moved up and down.

Use the stick’s up-and-down motion as the physical model for a y-axis event. section 7.3

A Python controller can read joystick data directly from a Linux device file, distinguish axis events from button events, and retain only the left_y and right_y axes. Axis events have type 2; the event number identifies the particular stick axis. section 7.5 section 7.5.1

Process a y-axis event in a small, reusable handler:

  • Confirm that the event is an axis event, then map its number to an axis name and ignore every axis except left_y and right_y. section 7.5.4
  • Use the sign of event.value to determine forward or backward direction. section 7.5.4
  • Divide the absolute value by MAX_VAL (32767), then multiply by 100 to express displacement from center as a percentage. section 7.5.4
  • For motor control, round the normalized value to a small level, send a throttle change only when that level differs from the previous one, and map each y axis to its motor. section 7.7.1

Print direction and stick position

A handler for left_y and right_y labels a negative value as forward and a positive value as backward, then prints the axis name, direction, and percentage. Moving a stick fully forward produces 100%; returning it to center produces 0%. section 7.5.4

Y-axis events can arrive quickly. Measuring 100 right-stick y-axis events with a timer reveals the event rate, helping determine whether sending a motor request for every physical movement would overload the motor server. section 7.6.1

How Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi covers it

How deep it goes
Works through filtering, direction, percentage normalization, event-rate measurement, and level-based throttle updates.
Examples
Demonstrates joystick_axis.py output, a 100-event rate measurement, and joystick_remote.py motor-level changes.
What it assumes
Introduces joystick event fields while using Python binary reading and a Linux joystick device file.
What you can do after
Build it from scratch by reading Linux joystick events and converting y-axis movement into motor throttle requests.
How it connects
Connects joystick events with x-axis events, button events, Linux input devices, motor throttle, and HTTP requests.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi. The section links point to the parts of the book it draws on.