After I discovered how to control motor velocity, I integrated the Python scripts I developed in OctoPi.
Installing OctoPi on a Raspberry Pi board is quite straightforward: you can burn the image on an SD card using the Raspberry Pi Imager. You then have to choose the OctoPi image under “Choose OS”, by selecting “Other Specific Purpose OS” > “3D printing” > “OctoPi” and then the “stable” version. Before proceeding, it's a good idea to enable SSH server and configure Wifi SSID and password
When the image has been written, you can insert the SD card in the Raspberry Pi's SD slot and power up the system. You should be able to access OctoPi's web interface at the URL https://<Raspberry Pi's IP>/. You can login using the username "admin" and the default password "admin".
My initial thought was to develop a plugin with a frontend and some new REST endpoints to control the motor. However, after some researches, I found a better way to control the camera. There is a configuration file (.octoprint/config.yaml), which has a section called "controls" where you can add custom controls that will appera in the "Control" tab of the web interface, just below the live stream of the webcam. I changed the config.yaml file as shown in the snippet below (more details about the supported entries can be found in the official Octoprint documentation)
controls: layout: vertical name: Camera motor- children: - name: CW command: OCTO901 %(speed) enabled: true input: - default: 255 name: Speed (0-255) parameter: speed slider: max: 255 min: 0 - command: OCTO900 enabled: true name: Stop - name: CCW command: OCTO902 %(speed) enabled: true input: - default: 255 name: Speed (0-255) parameter: speed slider: max: 255 min: 0
This will result in the new section shown in screenshot below
As you can see in the config.yaml file, the new buttons are associated with a custom G-Code instruction (namely OCTO900, OCTO901, OCTO902 - see the "command" field). This commands are handled by a plugin called "GCode System commands". This plugin can be installed from the Octoprint's Plugin manager
This plugin invokes a system command (for example, a shell script) when a specific GCode command is executed
In this case, I will create three new GCode commands
- OCTO900: calls /home/pi/move_stop.sh
- OCTO901: calls /home/pi/move_cw.sh
- OCTO902: calls /home/pi/move_ccw.sh
Scripts can access the command line parameters and other useful information by means of three environment variables
- OCTOPRINT_GCODESYSTEMCOMMAND_ID: this is the GCode command (for example OCTO900)
- OCTOPRINT_GCODESYSTEMCOMMAND_ARGS: this variables store the command arguments. For example, the OCTO901 and OCTO902 commands will be called with 1 parameter, as per config.yaml
command: OCTO902 %(speed)
In this case, OCTOPRINT_GCODESYSTEMCOMMAND_ARGS will contain the value of the speed parameter, which can be changed through the slider control
- OCTOPRINT_GCODESYSTEMCOMMAND_LINE: stores the command (for example, /home/pi/move_cw.sh)