#include "FastAccelStepper.h"
#include "PinHeader.h"
#define FLOAT_CONSTATNT 5.625
#define PVOL_FMIN 0.00
#define PVOL_FMAX 10.00
#define ADCVOL_FMIN 0.00
#define ADCVOL_FMAX 3.30
#define ADCRAW_MIN 0
#define ADCRAW_MAX 4095
//#define LED1_PIN 15
#define LED1_PIN 2
FastAccelStepperEngine engine = FastAccelStepperEngine();
FastAccelStepper *stepmotor=NULL;
float float_variable1, float_variable2, float_variable3, float_variable4, float_variable5;
uint32_t uint32_value1;
union
{
float fv; // float value
uint16_t wv[2]; // word value (16-bit)
uint32_t uiv; // unsigned int (32-bit)
}
float_unit;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(LED1_PIN, OUTPUT);
//pinMode(LED_BUILTIN, OUTPUT);
/* for(uint8_t i=0; i<MAX_STEPMOTOR; i++)
{
stepmotor[i]=NULL;
Serial.println(i);
}
*/
engine.init();
stepmotor = engine.stepperConnectToPin(STEPMOT1_STEP_PIN);
if (stepmotor)
{
stepmotor->setDirectionPin(STEPMOT1_DIR_PIN);
stepmotor->setEnablePin(STEPMOT1_EN_PIN);
stepmotor->setAutoEnable(true); // The stepper driver will be enabled before stepping and will be disabled afterwards.
//stepmotor[0]->setSpeedInHz(RPS_TO_SPS(1)); // set current speed unit defined in steps per seconds.
stepmotor->setSpeedInHz(RPM_TO_SPS(60)); // set current speed unit defined in steps per seconds.
stepmotor->setAcceleration(1000); // 1000 steps/s²
//stepmotor[0]->disableOutputs(); // Disable stepper driver.
stepmotor->applySpeedAcceleration();
Serial.println("ok");
}
stepmotor->runForward();
Serial.println("SPS is: "); Serial.println(RPM_TO_SPS(60));
testing_floatValue_mapping();
testing_floatValue_transmission();
}
uint8_t dummy_count=0;
void loop() {
// put your main code here, to run repeatedly:
//delay(10); // this speeds up the simulation
Serial.println(++dummy_count);
digitalWrite(LED1_PIN, HIGH); // turn the LED on (HIGH is the voltage level)
// digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(LED1_PIN, LOW); // turn the LED off by making the voltage LOW
// digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
}
float map_fvalue(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void testing_floatValue_mapping(void)
{
float_variable1 = FLOAT_CONSTATNT;
Serial.println(float_variable1, 4);
Serial.println("min value :::");
float_variable1 = 900.9;
float_variable2 = map(float_variable1, ADCRAW_MIN, ADCRAW_MAX, ADCVOL_FMIN, ADCVOL_FMAX);
Serial.println(float_variable2, 4);
float_variable2 = map_fvalue(float_variable1, ADCRAW_MIN, ADCRAW_MAX, ADCVOL_FMIN, ADCVOL_FMAX);
Serial.println(float_variable2, 4);
float_variable3 = map_fvalue(float_variable2, ADCVOL_FMIN, ADCVOL_FMAX, PVOL_FMIN, PVOL_FMAX);
Serial.println(float_variable3, 4);
float_variable4 = pow(float_variable3, 2.2); // Testing if "pow" function works with float values.
Serial.println(float_variable4, 4);
float_variable5=pow(10, (float_variable3-FLOAT_CONSTATNT));
Serial.println(float_variable5, 4);
Serial.println("max value :::");
float_variable1 = 3480.75;
float_variable2 = map_fvalue(float_variable1, ADCRAW_MIN, ADCRAW_MAX, PVOL_FMIN, PVOL_FMAX);
Serial.println(float_variable2, 4);
float_variable5 = pow(10, (float_variable2-FLOAT_CONSTATNT));
Serial.println(float_variable5, 4);
}
void testing_floatValue_transmission(void)
{
float fvalue1=3.5;
Serial.println(fvalue1, HEX); // its not working. it does NOT populate HEX of float fvlaue1.
uint32_t uivalue1=208;
Serial.println(uivalue1, HEX); // it pouplates HEX of integer uivalue1.
// OPTION-1 : to transmit float values as interger.
// sending integer value as a representative of float values :
uint32_value1 = float_variable5*10000;
Serial.println(uint32_value1);
// OPTION-2 : to transmit float values using union.
// testing of Union based float and word conversion :
float_unit.fv = 12.15; // equivalent hex of float 12.15 is : 0x41426666 (single precision)
Serial.print("float: "); Serial.println(float_unit.fv);
Serial.print("uint32: "); Serial.println(float_unit.uiv);
Serial.print("uint32 hex: "); Serial.println(float_unit.uiv, HEX); // it stores hex value : 0x41426666
Serial.print("word0: "); Serial.println(float_unit.wv[0]); // displaying integer value of LSW ; 26214 = 0x6666
Serial.print("word0 hex: "); Serial.println(float_unit.wv[0], HEX); // it stores LSW (LOWER word) of float value float_unit.fv which is : 0x6666
Serial.print("word1: "); Serial.println(float_unit.wv[1]); // displaying integer value of MSW ; 16706 = 0x4142
Serial.print("word1 hex: "); Serial.println(float_unit.wv[1], HEX); // it stores MSW (HIGHER word) of float value float_unit.fv which is : 0x4142
}