I am going to use NodeMCU processor that can be programmed from Arduino IDE. I am not going to explain its installation here, as it is very well documented online. Also, you can take a look at "robotic hardware" sections of this site.
The overall idea is to install Arduino IDE (currently, it is v.2). It comes with some libraries, for example, when you use
... you do not have to load any additional packages. However, some libraries are not installed by default, so you will have to install them; the procedure is simple and well documented. However... You should always keep in mind security considerations. For example, let's say I want to use WiFi library:
If you follow the standard procedure, you will find that there are few libraries with that name, and who knows what troyans they contain. For you (and myself in future) to get a correct library, I have included the direct link: you will have to download the zip file from GIT and use Arduino IDE to "add library as Zip":
In this article, the spider doesn't use solenoids. Nevertheless, let's provide a simple script for controlling them so the section is complete.
Solenoid is a rather simple device: it cas coil and a metal rod; when coil has current, rod is pulled into it. When not powered, a spring pushes the rod outside.
So we use a transistor that takes small signal from processor's pins and amplifies it for solenoid to use, that's all. Here is the code for solenoid.ino (included in the archive). It uses all solenoids, to turn them on and then off, in cycle. It also turns on and off the NodeMCU on-board LED:
An out-of-the-box servo functionality is based on pulse wide modulation: you send pulse of certain frequency, the motor turns to a certain angle. So, let's say we want our horizontal servo motor to turn within certain range ("certain", because I don't want a full range, after all, it is a spider, not a propeller).
As I mentioned in introduction, this approach has a huge problem: motor moves as fast as it can, which makes our spider look jerky. To fix it, we can use the following approach: having motor's current position and a goal position, break the interval to, say, 40 steps, and move motor one step in a time with certain delays between steps.
This approach works fine on a simgle motor, but if we have any other code to run (like another motor), we have a problem: it is blocking. And ESP8266 that is a processor in NodeMCU I use, doesn't really support multithreading.
But Arduino-style code has loop() function, so nothing prevents us from handling steps of the motors ourselves. This is what the following code is about.
It uses the following logic: create multiple queues, and at each step, make one queue control one motor, until queue is empty. For example, let's say queue number 3 at this moment controls motor 1. It has number currnt_pulse equal 300, end_pulse equal 400 and step equal 10. In a loop, we send "300" to a motor, so it turns. Then we add step to current pulse (it becomes 310) and do the same for all queues (and it means for all motors). And so on, until currnt_pulse becomes equal end_pulse.
Before we proceed to the code, here is an additional thing to keep in mind. Queue number 5 does not necessarily serve motor number 5. Actually, it is a nice way to handle sequential vs parallel motor movements. if we place commands for two different motors in two different queues, they will run parallel. If we put them to the same queue, the first motor runs first, and only then the second motor runs.
However, here is the catch. For the algorithm i just described to work, we need to know the current motor position. And... we don't. Standard servo library doesn't have "get position" function. This is why every time before we start, I have to move all motors to one of known positions in a fast way - at that moment we know the current position and "smooth" algorithm can work.
The code below is heavily commentsd, plus i have examples for all major cases.