Skip to content

Instantly share code, notes, and snippets.

@guigur
Last active August 5, 2018 19:10
Show Gist options
  • Save guigur/2f6797c8965dd77525c96512565883cc to your computer and use it in GitHub Desktop.
Save guigur/2f6797c8965dd77525c96512565883cc to your computer and use it in GitHub Desktop.
/**
** GUIGUR 2018
** This class move motors
*/
#include "headers/Motor.hpp"
Motor::Motor(int stepPin, int dirPin, int endStpPin)
{
Serial.println("construct motor");
_stepPin = stepPin;
_dirPin = dirPin;
_endStpPin = endStpPin;
_endStpState = HIGH;
pinMode(_endStpPin, INPUT);
const int maxAccelerationSpeed = 2000;
_motor.setAcceleration(maxAccelerationSpeed);
_motor.setSpeed(1000);
this->initMotor();
}
Motor::~Motor()
{
Serial.print("destruct motor");
}
/**
** This function init a motor by drving it until it hit a limit switch
*/
void Motor::initMotor()
{
Serial.println("init motor");
_motor.setMaxSpeed(150); //2500 stp/s^2
_motor.moveTo(3000);
while(_endStpState == HIGH)
{
Serial.println("loop motor");
_endStpState = digitalRead(_endStpPin);
_motor.run();
}
_motor.stop();
//this->_motor.setCurrentPosition(posPaddleMax);
_motor.setAcceleration(_acceleration);
_motor.setSpeed(1000);
}
float Motor::GetAbsolutePos()
{
return this->_pos;
}
void Motor::GotoAbsolutePos(float pos)
{
/* this->_pos = pos;
this->_motor.setTargetRel(this->_pos);
this->_controller.move(this->_motor);
*/
}
void Motor::GotoRelativePos(float pos)
{
/*
this->_pos = this->_pos + pos;
this->_motor.setTargetRel(this->_pos);
this->_controller.move(this->_motor);
*/
}
bool Motor::GetState()
{
//return this->_enable;
}
// Motor.hpp
#ifndef MOTOR_HPP_
#define MOTOR_HPP_
#include <String.h>
#include <Arduino.h>
#include <AccelStepper.h>
#ifndef CFG
#include "Config.hpp"
#endif
class Motor
{
public:
Motor(int stepPin, int dirPin, int endStpPin);
~Motor();
float GetAbsolutePos();
void GotoAbsolutePos(float pos);
void GotoRelativePos(float pos);
bool GetState();
void Enable();
void Disable();
void initMotor();
private:
int _stepPin;
int _dirPin;
int _endStpPin;
int _endStpState;
AccelStepper _motor = AccelStepper(1, _stepPin, _dirPin);
/*
Stepper _motor = Stepper(_stepPin, _dirPin);
StepControl<> _controller;*/
int _acceleration = 2500;
int _maxSpeed = 5000;
int _pullInSpeed = 200;
bool _enable;
float _pos;
};
#endif /* MOTOR_HPP_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment