Python script for Rabpberry Pi to command elevator based on computer vision The goal is to place the elevator on top of a hamster cage. A camera records the platform and activate the motor to lift the platform. When the elevator is on top, it stays in this position until the hamster leaves the platform, then the elevator goes down.
When anything on the camera changes (hamster climbed on the platform), the servotor rotates on 180° then stay still (platform go up and stay on top).
When something changed again (hamster left the platform), the platform rotates 180° in the opposite way (going back to ground floor).
WhatsApp.Video.2022-06-12.at.11.15.07.AM.mp4
Abstraction of the elevator. Basically a state machine, handling the transitions between states.
The camera module, handling the detection of movement
Handle the commands to the motor
Other tools and helpers
- Picamera : no need to install any library but the camera must be enabled : type
sudo raspi-config, go to the 3rd option Interface option and enable camera, then reboot - Install scikit-image :
pip3 install --upgrade scikit-image(need numpy >= 1.16.5) - In case the import of scikit-image causes an error :
sudo apt install libatlas-base-dev - Install opencv :
pip3 install python-opencv - Make sure GPIO is installed :
pip3 install RPi.GPIO
Need to transform the rotation movement of the motor to a translation movement. Could have done it with a rack and pinion, but I figured the easiest way would be to use a wheel and a string, especially because I want to place the raspberry Pi on top of the hamster cage that avoid taking too much space inside. Then the platform will needs movement constraints on the rotations and on the translations horizontally, to allow only vertical movements. We will need to add a basis and 4 pillars to guide the platform. At the end, the elevator will work like this :
We can calculate the radius of the wheel depending on the amplitude we want for our platform. I am using a servomotor that can rotate on 180°, which means the maximum amplitude of the elevator corresponds to half the perimeter or the wheel attached to the servomotor :
I am using a SG90 servomotor, that requires 5V alimentation and use PWM signals. Plug it as followed:
- Yellow cable on a GPIO pin that uses PWM signal
- Red cable on a 5V pin
- Black/brown cable on the ground
My servomotor has his wires attached together so I bought a bunch of jumper wires to connect to the relevant pins on the Pi
Here is the details about each pin of the raspberry pi 4 that I am using:
