How it works...

The script starts by importing the curses and os modules. A standard Python input() command would require the Enter key to be pressed after each key press before we could act upon it. However, as we will see shortly, the curses module simply allows us to scan for keyboard presses and respond to them immediately. We use the os module to call the ServoBlaster commands, as we would via the Terminal.

First, we define our setup, such as the servo mappings, calibration positions, min/max ranges, our control keys, and the STEP size in degrees for each control command. We also define our parameters for our requested angle (in degrees) to target PWM signal up time (in milliseconds) calculation.

For these particular servos, an up time of 1 ms is equal to 0 degrees and 2.5 ms is 180 degrees, so we have an offset (OFFSET) of 1 ms and a scale (DEG2MS) of 180 degrees/1.5 ms.

Therefore, our required up time (in milliseconds) can be calculated as OFFSET + (degrees*DEG2MS). Finally, we define the SERVOD command line and servo IDLE timeout to initialize the ServoBlaster user daemon. Within initialize (), we use os.system() to start the servod daemon, as we did before.

In order to detect key presses, we call the main() function of the script from curses.wrapper(), allowing term to control the terminal input and output. We use term.nodelay(1) so that when we do check for any key presses (using term.getch()), execution will continue normally. We use term.addstr(text) to show the user the control keys and then update the display via term.refresh(). The remaining script checks the terminal for key presses and the result assigned to c. If no key was pressed, then term.getch() returns -1; otherwise, the ASCII equivalent value is returned, and we can check for it in each of the dictionaries we defined for control keys. We will use KEY_MORE and KEY_LESS to change the servo positions, and KEY_CMD (c or x) to allow us to set all the servos to their calibrated position or to exit cleanly. Finally, we display any useful debugging information (if DEBUG is set to True) using term.addstr(), and ensure that it is displayed at (1,0) in the terminal (one line down from the top).

For normal control, the position of the servos will be controlled using the updateServo() function, which adjusts the current position (stored in the POS array) by the required change (either +STEP or -STEP). We ensure the new position is within the MAX/MIN limits defined, and report if we've hit them. The servo is then instructed to move to the required position using setServo(), specifying the needed PWM up time in micro seconds.

The last function, calibrate(), called when C is pressed, simply sets each of the servos to the angle defined in the CAL array (using setServo()) and ensures that the current position is kept up to date.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.86.211