Skip to content

Commit

Permalink
Merge pull request #10 from youseetoo/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
KillerInk authored Dec 8, 2022
2 parents 8f813f8 + 146b784 commit 5fa0a93
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 168 deletions.
1 change: 1 addition & 0 deletions main/ModuleConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ struct ModuleConfig
bool home = false;
bool state = true;
bool analogJoystick = false;
bool wifi = false;
};
4 changes: 4 additions & 0 deletions main/ModuleController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ DynamicJsonDocument ModuleController::get()
doc[key_modules][key_digitalin] = moduleConfig->digitalin;
doc[key_modules][key_scanner] = moduleConfig->scanner;
doc[key_modules][key_joy] = moduleConfig->analogJoystick;
doc[key_modules][key_wifi] = moduleConfig->wifi;
return doc;
}
// {"task":"/modules_set", "modules" : {"led" : 1, "motor": 1, "analogin" : 0, "pid" : 0, "laser" : 0, "dac" : 0, "analogout" : 0, "digitalout" : 0, "digitalin" : 0, "scanner" : 0}}
Expand Down Expand Up @@ -200,6 +201,9 @@ int ModuleController::set(DynamicJsonDocument j)
moduleConfig->scanner = j[key_modules][key_scanner];
if (j[key_modules].containsKey(key_joy))
moduleConfig->analogJoystick = j[key_modules][key_joy];
if (j[key_modules].containsKey(key_wifi))
moduleConfig->wifi = j[key_modules][key_wifi];

Config::setModuleConfig(moduleConfig);
setup();
WifiController::restartWebServer();
Expand Down
1 change: 1 addition & 0 deletions main/ModuleController.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ enum class AvailableModules
analogin,
state,
analogJoystick,
wifi
};

class ModuleController
Expand Down
8 changes: 6 additions & 2 deletions main/json_api_BD.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{"task":"modules_get"}
{"task":"/modules_get"}
{"task":"/modules_set", "modules" : {"led" : 1, "motor": 1, "slm" : 0, "home":1, "analogin" : 0, "pid" : 0, "laser" : 1, "dac" : 0, "analogout" : 0, "digitalout" : 0, "digitalin" : 1, "scanner" : 0}}


{"task":"/modules_set", "modules" : {"led" : 0, "motor": 1, "slm" : 0, "home":0, "analogin" : 0, "pid" : 0, "laser" : 0, "dac" : 0, "analogout" : 0, "digitalout" : 0, "digitalin" : 1, "scanner" : 0}}

# X,Y,Z,A => 1,2,3,0
{"task":"/motor_set",
"motor":
Expand Down Expand Up @@ -42,11 +44,13 @@
}
}



{"task":"/motor_act",
"motor":
{
"steppers": [
{ "stepperid": 1, "position": -100, "speed": 2000, "isabs": 0, "isaccel":0}
{ "stepperid": 1, "position": -1000, "speed": 1000, "isabs": 0, "isaccel":0}
]
}
}
Expand Down
21 changes: 19 additions & 2 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "src/serial/SerialProcess.h"
#include "src/bt/BtController.h"
#include "ModuleController.h"
#include "src/motor/FocusMotor.h"


#include "soc/soc.h"
Expand Down Expand Up @@ -45,11 +46,27 @@ void setup()
Config::checkifBootWentThrough();
}

long tProcessServer = 100; // process server every 100ms
long oldTime = 0;

void loop()
{
// for any timing-related purposes

// receive and process serial messages
serial.loop();
WifiController::handelMessages();

// process the server every 100ms
// if (true){//motor->motorsBusy() or millis()-oldTime > tProcessServer) {
if (moduleController.get(AvailableModules::wifi) != nullptr and moduleController.get(AvailableModules::wifi)){
oldTime = millis();
WifiController::handelMessages();
}

// handle PS-controller inputs
BtController::loop();

// process all commands in their modules
moduleController.loop();
unsigned long end = micros();
//log_i("loop took %i ms", end - now);
}
6 changes: 5 additions & 1 deletion main/pindef.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
// initial pin definitions
// initial pin definitions in "pindef.h"

// ESP32-WEMOS D1 R32
const int PIN_DEF_MOTOR_DIR_A = 0;
Expand All @@ -14,6 +14,10 @@ const int PIN_DEF_MOTOR_EN_A = GPIO_NUM_12;
const int PIN_DEF_MOTOR_EN_X = GPIO_NUM_12;
const int PIN_DEF_MOTOR_EN_Y = GPIO_NUM_12;
const int PIN_DEF_MOTOR_EN_Z = GPIO_NUM_12;
const bool PIN_DEF_MOTOR_EN_A_INVERTED = true;
const bool PIN_DEF_MOTOR_EN_X_INVERTED = true;
const bool PIN_DEF_MOTOR_EN_Y_INVERTED = true;
const bool PIN_DEF_MOTOR_EN_Z_INVERTED = true;

const int PIN_DEF_LASER_1 = GPIO_NUM_18;
const int PIN_DEF_LASER_2 = GPIO_NUM_19;
Expand Down
48 changes: 23 additions & 25 deletions main/src/bt/PsXController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ void PsXController::loop()
log_i("Turn on LED ");
IS_PS_CONTROLER_LEDARRAY = !led->TurnedOn();
led->set_all(255, 255, 255);
delay(1000); // Debounce?
}
if (psx->event.button_down.circle)
{
log_i("Turn off LED ");
IS_PS_CONTROLER_LEDARRAY = !led->TurnedOn();
led->set_all(0,0,0);
delay(1000); // Debounce?
}
}
/* code */
Expand All @@ -52,13 +50,11 @@ void PsXController::loop()
// Switch laser 2 on/off on up/down button press
Serial.print("Turning on LAser 10000");
ledcWrite(laser->PWM_CHANNEL_LASER_2, 20000);
delay(100); // Debounce?
}
if (psx->event.button_down.down)
{
Serial.print("Turning off LAser ");
ledcWrite(laser->PWM_CHANNEL_LASER_2, 0);
delay(100); // Debounce?
}

// LASER 2
Expand All @@ -67,13 +63,11 @@ void PsXController::loop()
{
Serial.print("Turning on LAser 10000");
ledcWrite(laser->PWM_CHANNEL_LASER_1, 20000);
delay(100); // Debounce?
}
if (psx->event.button_down.left)
{
Serial.print("Turning off LAser ");
ledcWrite(laser->PWM_CHANNEL_LASER_1, 0);
delay(100); // Debounce?
}
}

Expand All @@ -82,23 +76,25 @@ void PsXController::loop()
{
/* code */
FocusMotor *motor = (FocusMotor *)moduleController.get(AvailableModules::motor);
// Y-Direction
// Z-Direction
if (abs(psx->state.analog.stick.ly) > offset_val)
{
stick_ly = psx->state.analog.stick.ly;
stick_ly = stick_ly - sgn(stick_ly) * offset_val;
motor->data[Stepper::Y]->speed = stick_ly * 5 * global_speed;
motor->data[Stepper::Y]->isforever = true;
joystick_drive_Y = true;
if (motor->data[Stepper::Y]->stopped)
if(abs(stick_ly)>50)stick_ly=2*stick_ly; // add more speed above threshold
motor->data[Stepper::Z]->speed = stick_ly * global_speed;
Serial.println(motor->data[Stepper::Z]->speed);
motor->data[Stepper::Z]->isforever = true;
joystick_drive_Z = true;
if (motor->data[Stepper::Z]->stopped)
{
motor->startStepper(Stepper::Y);
motor->startStepper(Stepper::Z);
}
}
else if (motor->data[Stepper::Y]->speed != 0 && joystick_drive_Y)
else if (motor->data[Stepper::Z]->speed != 0 && joystick_drive_Z)
{
motor->stopStepper(Stepper::Y);
joystick_drive_Y = false;
motor->stopStepper(Stepper::Z);
joystick_drive_Z = false;
}

// X-Direction
Expand All @@ -107,7 +103,8 @@ void PsXController::loop()
// move_x
stick_rx = psx->state.analog.stick.rx;
stick_rx = stick_rx - sgn(stick_rx) * offset_val;
motor->data[Stepper::X]->speed = stick_rx * 5 * global_speed;
motor->data[Stepper::X]->speed = stick_rx * global_speed;
if(abs(stick_rx)>50)stick_rx=2*stick_rx; // add more speed above threshold
motor->data[Stepper::X]->isforever = true;
joystick_drive_X = true;
if (motor->data[Stepper::X]->stopped)
Expand All @@ -121,23 +118,24 @@ void PsXController::loop()
joystick_drive_X = false;
}

// Z-direction
// Y-direction
if ((abs(psx->state.analog.stick.ry) > offset_val))
{
stick_ry = psx->state.analog.stick.ry;
stick_ry = stick_ry - sgn(stick_ry) * offset_val;
motor->data[Stepper::Z]->speed = stick_ry * 5 * global_speed;
motor->data[Stepper::Z]->isforever = true;
joystick_drive_Z = true;
if (motor->data[Stepper::Z]->stopped)
motor->data[Stepper::Y]->speed = stick_ry * global_speed;
if(abs(stick_ry)>50)stick_ry=2*stick_ry; // add more speed above threshold
motor->data[Stepper::Y]->isforever = true;
joystick_drive_Y = true;
if (motor->data[Stepper::Y]->stopped)
{
motor->startStepper(Stepper::Z);
motor->startStepper(Stepper::Y);
}
}
else if (motor->data[Stepper::Z]->speed != 0 && joystick_drive_Z)
else if (motor->data[Stepper::Y]->speed != 0 && joystick_drive_Y)
{
motor->stopStepper(Stepper::Z);
joystick_drive_Z = false;
motor->stopStepper(Stepper::Y);
joystick_drive_Y = false;
}
}

Expand Down
4 changes: 2 additions & 2 deletions main/src/bt/PsXController.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PsXController
private:
PSController * psx = nullptr;
bool IS_PS_CONTROLER_LEDARRAY = false;
int offset_val = 1; // make sure you do not accidentally turn on two directions at the same time
int offset_val = 5; // make sure you do not accidentally turn on two directions at the same time
int stick_ly = 0;
int stick_lx = 0;
int stick_rx = 0;
Expand All @@ -32,7 +32,7 @@ class PsXController
int speed_x = 0;
int speed_y = 0;
int speed_z = 0;
int global_speed = 2; // multiplier for the speed
int global_speed = 10; // multiplier for the speed

int analogout_val_1 = 0;
int pwm_max = 0; // no idea how big it should be
Expand Down
1 change: 1 addition & 0 deletions main/src/config/JsonKeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ static const PROGMEM char *key_digitalout = "digitalout";
static const PROGMEM char *key_digitalin = "digitalin";
static const PROGMEM char *key_scanner = "scanner";
static const PROGMEM char *key_joy = "joy";
static const PROGMEM char *key_wifi = "wifi";
static const PROGMEM char *key_joiypinX = "joyX";
static const PROGMEM char *key_joiypinY = "joyY";
static const PROGMEM char *key_readanaloginID = "readanaloginID";
Expand Down
67 changes: 24 additions & 43 deletions main/src/laser/LaserController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,80 +82,61 @@ void LaserController::LASER_despeckle(int LASERdespeckle, int LASERid, int LASER
// Custom function accessible by the API
int LaserController::act(DynamicJsonDocument ob)
{
// here you can do something
Serial.println("LASER_act_fct");

isBusy = true;

int LASERid = (ob)["LASERid"];
int LASERval = (ob)["LASERval"];
int LASERdespeckle = (ob)["LASERdespeckle"];
int LASERdespecklePeriod = 20;
// JSON String
// {"task":"/laser_act", "LASERid":1, "LASERval":100, "LASERdespeckle":10, "LASERdespecklePeriod":20}

// assign values
int LASERid = 0;
int LASERval = 0;
int LASERdespeckle = 0;
int LASERdespecklePeriod = 0;
// default values overridden
if(ob.containsKey("LASERid")) LASERid = (ob)["LASERid"];
if(ob.containsKey("LASERval")) LASERval = (ob)["LASERval"];
if(ob.containsKey("LASERdespeckle")) LASERdespeckle = (ob)["LASERdespeckle"];
if(ob.containsKey("LASERdespecklePeriod")) LASERdespecklePeriod = (ob)["LASERdespecklePeriod"];
ob.clear();

if (ob.containsKey("LASERdespecklePeriod"))
{
LASERdespecklePeriod = (ob)["LASERdespecklePeriod"];
}

if (DEBUG)
{
Serial.print("LASERid ");
Serial.println(LASERid);
Serial.print("LASERval ");
Serial.println(LASERval);
Serial.print("LASERdespeckle ");
Serial.println(LASERdespeckle);
Serial.print("LASERdespecklePeriod ");
Serial.println(LASERdespecklePeriod);
}
// debugging
log_i("LaserID %i, LaserVal %i, LaserDespeckle %i, LaserDespecklePeriod %i", LASERid, LASERval, LASERdespeckle, LASERdespecklePeriod);

// action LASER 1
if (LASERid == 1 && pins.LASER_PIN_1 != 0)
{
LASER_val_1 = LASERval;
LASER_despeckle_1 = LASERdespeckle;
LASER_despeckle_period_1 = LASERdespecklePeriod;
if (DEBUG)
{
Serial.print("LaserPIN ");
Serial.println(pins.LASER_PIN_1);
}
ledcWrite(PWM_CHANNEL_LASER_1, LASERval);
log_i("LASERid %i, LASERval %i", LASERid, LASERval);
ob[key_return] = 1;
}
// action LASER 2
else if (LASERid == 2 && pins.LASER_PIN_2 != 0)
{
LASER_val_2 = LASERval;
LASER_despeckle_2 = LASERdespeckle;
LASER_despeckle_period_2 = LASERdespecklePeriod;
if (DEBUG)
{
Serial.print("LaserPIN ");
Serial.println(pins.LASER_PIN_2);
}
ledcWrite(PWM_CHANNEL_LASER_2, LASERval);
log_i("LASERid %i, LASERval %i", LASERid, LASERval);
ob[key_return] = 1;
}
// action LASER 3
else if (LASERid == 3 && pins.LASER_PIN_3 != 0)
{
LASER_val_3 = LASERval;
LASER_despeckle_3 = LASERdespeckle;
LASER_despeckle_period_3 = LASERdespecklePeriod;
if (DEBUG)
{
Serial.print("LaserPIN ");
Serial.println(pins.LASER_PIN_3);
}
ledcWrite(PWM_CHANNEL_LASER_3, LASERval);
log_i("LASERid %i, LASERval %i", LASERid, LASERval);
ob[key_return] = 1;
}
else{
ob[key_return] = 0;
}

isBusy = false;
return 1;
return ob[key_return];
}


int LaserController::set(DynamicJsonDocument ob)
{
// here you can set parameters
Expand Down
Loading

0 comments on commit 5fa0a93

Please sign in to comment.