Hey!
I control my servo via a Nextion. Well, the angle can be adjusted to -90 or 90 degrees. But now I would like to control the current of the servo via the Nextion and have the value displayed on the screen. Unfortunately this doesn't happen. Does somebody has any idea?
Thanks!
#include <Dynamixel2Arduino.h>
#include <SoftwareSerial.h>
SoftwareSerial soft_serial(7, 8);
#define DXL_SERIAL Serial
#define DEBUG_SERIAL soft_serial
const int DXL_DIR_PIN = 2; // DYNAMIXEL Shield DIR PIN
const uint8_t DXL_ID = 1;
const float DXL_PROTOCOL_VERSION = 2.0;
Dynamixel2Arduino dxl(DXL_SERIAL, DXL_DIR_PIN);
using namespace ControlTableItem;
float angle = 0.0;
float prevAngle = 0.0;
//= == NEXTION LCD == ==
#include "Nextion.h"
// Deklarieren Nextion Buttons - (page id = 0, component id = 1, component name = "b0")
//============= Page 0 (Main) =================
NexButton inc = NexButton(0, 5, "b3");
NexButton dec = NexButton(0, 6, "b4");
NexButton ang1 = NexButton(0, 4, "b2"); NexButton ang2 = NexButton(0, 3, "b1");
NexButton ang3 = NexButton(0, 2, "b0");
NexText status = NexText(0, 7, "t0");
NexText status1 = NexText (0, 10, "t1");
NexSlider slider = NexSlider(0, 8, "h0");
NexText stromText = NexText (0, 9, "t2");
NexTouch *nex_listen_list[] = {
&inc,
&dec,
&ang1,
&ang2,
&ang3,
&slider,
&stromText,
NULL
};
// Creating callback for each button
void incPopCallback(void *ptr) {
angle += 1; // Winkel um 1° erhöhen
angle = (angle > 90.0) ? 90.0: angle;
}
void decPopCallback(void *ptr) {
angle -= 1; // Winkel um 1° verringern
angle = (angle < -90.0) ? -90.0: angle;
}
void ang1PopCallback(void *ptr) {
angle = -90.0;
}
void ang2PopCallback(void *ptr) {
angle = 90.0;
}
void ang3PopCallback(void *ptr) {
angle = 0.0;
}
void sliderPopCallback (void*ptr) {
uint32_t val = 0;
slider.getValue(&val);
int16_t goal_current = map(val, 0, 100, 0, 5200);
dxl.writeControlTableItem(GOAL_CURRENT, DXL_ID, goal_current);
char currentStr[10];
sprintf(currentStr, "%d mA", goal_current);
stromText.setText(currentStr);
}
void setup() {
DEBUG_SERIAL.begin(115200);
nexInit();
inc.attachPop(incPopCallback, &inc);
dec.attachPop(decPopCallback, &dec);
ang1.attachPop(ang1PopCallback, &ang1);
ang2.attachPop(ang2PopCallback, &ang2);
ang3.attachPop(ang3PopCallback, &ang3);
slider.attachPop(sliderPopCallback, &slider);
status.setText("C");
delay(100);
status.setText("C");
delay(100);
dxl.begin(57600);
dxl.setPortProtocolVersion(DXL_PROTOCOL_VERSION);
dxl.ping(DXL_ID);
dxl.torqueOff(DXL_ID);
dxl.setOperatingMode(DXL_ID, OP_POSITION);
dxl.torqueOn(DXL_ID);
dxl.writeControlTableItem(PROFILE_VELOCITY, DXL_ID, 30);
dxl.setGoalPosition(DXL_ID, 0.0, UNIT_DEGREE);
}
void loop() {
if (prevAngle != angle) {
prevAngle = angle;
dxl.setGoalPosition(DXL_ID, angle, UNIT_DEGREE);
}
nexLoop(nex_listen_list);
}