/ External libs.
#include <Arduino.h>
#include <PCA9685.h>
#include <WiiChuck.h>
// Hexapod libs.
#include <Hexapod_Demo.h>
#include <Hexapod_GPIO.h>
#include <Hexapod_Nunchuck.h>
#include <Hexapod_Kinematics.h>
#include <Hexapod_Serial.h>
#include <Hexapod_Servo.h>
#include <Hexapod_imu.h>
#include <Hexapod_WebServerApp.h>
AsyncWebSocket ws("/ws");
AsyncWebServer server(80);
AsyncEventSource events("/events");
IMU imu1;
const unsigned short fifoRate = 20U; // IMU refresh rate in Hz.
// Global variables.
angle_t servo_angles[NB_SERVOS];
Hexapod_Servo hx_servo; // Servo pins are defined in Hexapod_Config_`x`.h (where `x` is the file number)
Hexapod_Serial hx_serial;
Hexapod_Nunchuck hx_nunchuck;
Hexapod_Demo hx_demo;
Hexapod_GPIO hx_gpio;
/**
*
*/
void setup()
{
// Setup.
hx_gpio.setupGPIO();
hx_serial.setupSerial();
hx_serial.printSplashScreen();
hx_servo.setupServo();
#if ENABLE_NUNCHUCK_READ
hx_nunchuck.setupNunchuck();
#endif
#if ENABLE_IMU_READ
scanNetwork();
setupWebServer();
imu1.setupIMU(fifoRate);
#endif
hx_demo.demoMov_circles(DEMO_CIRCLES_NB_TURN);
// Go to home position.
uint8_t movOK = hx_servo.calcServoAngles({0, 0, 0, 0, 0, 0}, servo_angles);
hx_servo.updateServos(movOK);
// Find min and max coord.
hx_demo.findMinMax();
// Test calculation speed.
hx_demo.testCalcSpeed();
#if false
// Test calculations.
hx_demo.testCalculations();
#endif
}
/**
*
*/
void loop()
{
digitalWrite(DEBUG_PIN_1, LOW);
digitalWrite(DEBUG_PIN_1, HIGH);
#if ENABLE_NUNCHUCK_READ
hx_nunchuck.stopIfNotConnected();
hx_nunchuck.nunchuckControl();
#endif
#if ENABLE_SERIAL_READ
hx_serial.serialRead();
#endif
#if ENABLE_IMU_READ
static unsigned long T1 = millis();
if ((millis() - T1) < (1000 / fifoRate))
return;
T1 = millis();
ArduinoOTA.handle();
if (!ws.enabled())
{
Serial.println("NO WebSocket!");
return;
}
// Read IMU.
static unsigned short status;
static char jsonMsg[200];
status = imu1.readIMU(jsonMsg);
ws.textAll(jsonMsg);
if (status != 0)
Serial.println(jsonMsg);
euler_angles_t eulerAngles = imu1.getEulerAngles();
euler_angles_t angles = {
(eulerAngles.eA - 180) * DEG_TO_RAD,
(eulerAngles.eB - 360) * DEG_TO_RAD,
(eulerAngles.eC - 120) * DEG_TO_RAD,
};
Serial.print(angles.eA);
Serial.print(" ");
Serial.print(angles.eB);
Serial.print(" ");
Serial.print(angles.eC);
Serial.print("\n");
// X, Y, tilt X, tilt Y
int8_t movOK = hx_servo.calcServoAngles({0, 0, 0, angles.eA, angles.eB, angles.eC},
servo_angles);
hx_servo.updateServos(movOK);
#endif
}
``
[quote="mahmoud365, post:1, topic:1130136, full:true"]
#include <Servo.h>
const int servoPin = 9; // the pin number for the servo signal
const int trigPin = 11; //ultrasonic
const int echoPin = 12; //ultrasonic
const int servoMinDegrees = 0; // the limits to servo movement
const int servoMaxDegrees = 180;
Servo myservo; // create servo object to control a servo
int servoPosition = 0; // the current angle of the servo - starting at 90.
int servoSlowInterval = 20; // millisecs between servo moves
int servoFastInterval = 20;
int servoInterval = servoSlowInterval; // initial millisecs between servo moves
int servoDegrees = 2; // amount servo moves at each step
// will be changed to negative value for movement in the other direction
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousServoMillis = 0; // the time when the servo was last moved
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myservo.write(servoPosition); // sets the initial position
myservo.attach(servoPin);
}
void loop() {
// put your main code here, to run repeatedly:
// Notice that none of the action happens in loop() apart from reading millis()
// it just calls the functions that have the action code
currentMillis = millis(); // capture the latest value of millis()
// this is equivalent to noting the time from a clock
// use the same time for all LED flashes to keep them synchronized
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
//Tell the Arduino to print the measurement in the serial console
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
if (cm > 50) {servoSweep();
}
else if (cm <= 50) {myservo.write(servoPosition);
}
}
// Converts the microseconds reading to Inches
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
//Converts the Microseconds Reading to Centimeters
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
//========
void servoSweep() {
// this is similar to the servo sweep example except that it uses millis() rather than delay()
// nothing happens unless the interval has expired
// the value of currentMillis was set in loop()
if (currentMillis - previousServoMillis >= servoInterval) {
// its time for another move
previousServoMillis += servoInterval;
servoPosition = servoPosition + servoDegrees; // servoDegrees might be negative
if (servoPosition <= servoMinDegrees) {
// when the servo gets to its minimum position change the interval to change the speed
if (servoInterval == servoSlowInterval) {
servoInterval = servoFastInterval;
}
else {
servoInterval = servoSlowInterval;
}
}
if ((servoPosition >= servoMaxDegrees) || (servoPosition <= servoMinDegrees)) {
// if the servo is at either extreme change the sign of the degrees to make it move the other way
servoDegrees = - servoDegrees; // reverse direction
// and update the position to ensure it is within range
servoPosition = servoPosition + servoDegrees;
}
// make the servo move to the next position
myservo.write(servoPosition);
// and record the time when the move happened
}
My programming skills are really rusty and I am not sure where i am messing up any help is appreciated
So it probably is not your code and the error was in the code.
Where do you found it?
Did you copy it correctly?
Yes, it is not my code it got it on GITHUB stewart-platform-esp32/README.md at master · NicHub/stewart-platform-esp32 · GitHub
I couldn't find in the source code where this macro is defined. Try writing to the author
Thank you I appreciate it
My first find of DEMO_CIRCLES_NB_TURN
is in main.cpp
... I do not have linux so I do not have grep to "find in file"..
hx_demo.demoMov_circles(DEMO_CIRCLES_NB_TURN);
I spoke with the author and he helped me with it thanks a lot for the help
Could you please to share the solution with the forum?
I had to use platformio on vscode to upload and monitor
It is not looks a solution...
The demo circle no turn is defined in the platformio file
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.