Introduction
In a previous guide, I covered how to control stepper motors using Gertbot. For this post, I'd like to cover the built-in endstop functionality.
Endstops (also know as "limit switches") can be very useful to stop motors when an end position has been reached. This is for example the case with 3D printers or CNC milling machines where the motors controlling each axis, back up until the endstops are triggered. This defines a reference point, also called "home position" with coordinates 0,0,0.
There are different types of endstops, just to name a few:
- mechanical: an ordinary switch makes or cuts a connection when triggered
- magnetic: a hall-effect sensor is used to measure the magnetic field
- optical: a sensor reacts to sudden changes in light levels
For this guide, I'll be using mechanical endstops, such as the one on the right.
Gertbot
The Gertbot add-on board for the Raspberry Pi offers the possibility to activate endstops, which when triggered, will automatically stop the associated motors without having to code any additional functions.
Pins
Four pairs of endstops can be defined: one pair per motor channel, covering two extremities. Seen from the top of the Gertbot, the endstop pairs are positioned as such:
The endstop pins are connected to 3.3V using a 4.7kOhm pull-up resistor.
Modes
The endstops can be configured in three different modes:
- OFF (0): don't enable the endstops
- ENDSTOP_LOW (1): enable endstop, stop motor channel when switch to GND is closed and pin is LOW
- ENDSTOP_HIGH (2): enable endstop, stop motor channel when switch to GND is open and pin is HIGH
When using endstops, the safest option is to use the ENDSTOP_HIGH mode, as a broken endstop wire would result in the motor channel to be disabled, ensuring the safest state.
Code
Activating endstops for a single motor channel is straightforward:
// gb.set_endstop(BOARD, CHANNEL, ENDSTOP_A_MODE, ENDSTOP_B_MODE) // Configure first board's first motor channel with two endstops, triggered with the pin is HIGH gb.set_endstop(0, 0, 2, 2) // board 0, channel A, endstops A & B in ENDSTOP_HIGH mode
Multiple endstop pins can also be hooked up to the same switch. For example if two servo motors are used, with motor #1 on channels A & B and motor #2 on channels C & D, the setup would be as follows:
// gb.set_endstop(BOARD, CHANNEL, ENDSTOP_A_MODE, ENDSTOP_B_MODE) // Configure first board's first motor channel with two endstops, triggered with the pin is LOW gb.set_endstop(0, 0, 1, 1) // board 0, channel A, endstops A & B in ENDSTOP_LOW mode gb.set_endstop(0, 2, 1, 1) // board 0, channel C, endstops A & B in ENDSTOP_LOW mode
Demo
Here's an animation of an endstop being triggered, resulting in the motors to stop. (You may have to click the image)
Conclusion
This is a very useful feature of the Gertbot, as the endstop functionality only needs to be activated and works independently from that moment on, without requiring specific code to manage them.