avec thread

Dependencies:   mbed

Fork of T2_STM32 by Atechsys

Committer:
ketingue
Date:
Sun Jan 14 17:39:09 2018 +0000
Revision:
4:b01a3ce6ef01
Parent:
3:dfbd0ce2963a
test_pwm_01;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Tioneb 3:dfbd0ce2963a 1 #ifndef Cylinder_h
Tioneb 3:dfbd0ce2963a 2 #define Cylinder_h
Tioneb 3:dfbd0ce2963a 3 #include "Arduino.h"
Tioneb 3:dfbd0ce2963a 4 #include "StepperMotor.h"
Tioneb 3:dfbd0ce2963a 5
Tioneb 3:dfbd0ce2963a 6 class Cylinder: public StepperMotor{
Tioneb 3:dfbd0ce2963a 7 public:
Tioneb 3:dfbd0ce2963a 8 Cylinder(int, int, int, int, char, char);
Tioneb 3:dfbd0ce2963a 9 ~Cylinder(void);
Tioneb 3:dfbd0ce2963a 10 void Test_CW();
Tioneb 3:dfbd0ce2963a 11 void Test_CCW();
Tioneb 3:dfbd0ce2963a 12 void Sens_Rotation();
Tioneb 3:dfbd0ce2963a 13 void Reach_Target();
Tioneb 3:dfbd0ce2963a 14 void Update(unsigned char, unsigned char);
Tioneb 3:dfbd0ce2963a 15 void Origin(int);
Tioneb 3:dfbd0ce2963a 16 attachInterrupt(digitalPinToInterrupt(Cylinder_RackSensor), IRS_Rack, FALLING);
Tioneb 3:dfbd0ce2963a 17 attachInterrupt(digitalPinToInterrupt(Cylinder_OriginSensor), IRS_Origin, FALLING);
Tioneb 3:dfbd0ce2963a 18 private:
Tioneb 3:dfbd0ce2963a 19 unsigned char stepcw, stepccw;
Tioneb 3:dfbd0ce2963a 20 char Buffer;
Tioneb 3:dfbd0ce2963a 21 unsigned long stepnumber;
Tioneb 3:dfbd0ce2963a 22
Tioneb 3:dfbd0ce2963a 23
Tioneb 3:dfbd0ce2963a 24 };
Tioneb 3:dfbd0ce2963a 25
Tioneb 3:dfbd0ce2963a 26 Cylinder::Cylinder(int OriginSensor, int stepPin, int dirPin, int EnablePin, char Pos_Min, char Pos_Max):StepperMotor(OriginSensor, stepPin, dirPin, EnablePin, Pos_Min, Pos_Max){
Tioneb 3:dfbd0ce2963a 27 }
Tioneb 3:dfbd0ce2963a 28
Tioneb 3:dfbd0ce2963a 29 Cylinder::~Cylinder(void){
Tioneb 3:dfbd0ce2963a 30 }
Tioneb 3:dfbd0ce2963a 31
Tioneb 3:dfbd0ce2963a 32 void Cylinder::IRS_Rack(){
Tioneb 3:dfbd0ce2963a 33 stepnumber --
Tioneb 3:dfbd0ce2963a 34 }
Tioneb 3:dfbd0ce2963a 35
Tioneb 3:dfbd0ce2963a 36 /*
Tioneb 3:dfbd0ce2963a 37 void Cylinder::Test_CW() {
Tioneb 3:dfbd0ce2963a 38 stepcw = 0;
Tioneb 3:dfbd0ce2963a 39 Buffer = sCurrentPos;
Tioneb 3:dfbd0ce2963a 40 while (sTargetPos != Buffer) {
Tioneb 3:dfbd0ce2963a 41 stepcw++;
Tioneb 3:dfbd0ce2963a 42 Buffer++;
Tioneb 3:dfbd0ce2963a 43 if (Buffer > sPos_Max) {
Tioneb 3:dfbd0ce2963a 44 Buffer = sPos_Min;
Tioneb 3:dfbd0ce2963a 45 }
Tioneb 3:dfbd0ce2963a 46 }
Tioneb 3:dfbd0ce2963a 47 }
Tioneb 3:dfbd0ce2963a 48
Tioneb 3:dfbd0ce2963a 49 void Cylinder::Test_CCW() {
Tioneb 3:dfbd0ce2963a 50 stepccw = 0;
Tioneb 3:dfbd0ce2963a 51 Buffer = sCurrentPos;
Tioneb 3:dfbd0ce2963a 52 while (sTargetPos != Buffer) {
Tioneb 3:dfbd0ce2963a 53 stepccw++;
Tioneb 3:dfbd0ce2963a 54 Buffer--;
Tioneb 3:dfbd0ce2963a 55 if (Buffer == 255) Buffer = sPos_Max;
Tioneb 3:dfbd0ce2963a 56 }
Tioneb 3:dfbd0ce2963a 57 }
Tioneb 3:dfbd0ce2963a 58
Tioneb 3:dfbd0ce2963a 59 void Cylinder::Sens_Rotation() {
Tioneb 3:dfbd0ce2963a 60 Test_CW();
Tioneb 3:dfbd0ce2963a 61 Test_CCW();
Tioneb 3:dfbd0ce2963a 62 if (stepccw >= stepcw) {
Tioneb 3:dfbd0ce2963a 63 digitalWrite(sdirPin, HIGH);
Tioneb 3:dfbd0ce2963a 64 stepnumber = stepcw;
Tioneb 3:dfbd0ce2963a 65 }
Tioneb 3:dfbd0ce2963a 66 else {
Tioneb 3:dfbd0ce2963a 67 digitalWrite(sdirPin, LOW);
Tioneb 3:dfbd0ce2963a 68 stepnumber = stepccw;
Tioneb 3:dfbd0ce2963a 69 }
Tioneb 3:dfbd0ce2963a 70 }
Tioneb 3:dfbd0ce2963a 71 */
Tioneb 3:dfbd0ce2963a 72
Tioneb 3:dfbd0ce2963a 73 void Cylinder::Reach_Target() {
Tioneb 3:dfbd0ce2963a 74 Sens_Rotation();
Tioneb 3:dfbd0ce2963a 75 while (stepnumber != 0) {
Tioneb 3:dfbd0ce2963a 76 analogWrite(sstepPin, 127);
Tioneb 3:dfbd0ce2963a 77 while (digitalRead(Cylinder_RackSensor) == true);
Tioneb 3:dfbd0ce2963a 78 Serial.print("StepNumber = ");
Tioneb 3:dfbd0ce2963a 79 Serial.println(stepnumber);
Tioneb 3:dfbd0ce2963a 80 /*if (digitalRead(Cylinder_RackSensor) == true) {
Tioneb 3:dfbd0ce2963a 81 Serial.println("CAPTEUR");
Tioneb 3:dfbd0ce2963a 82 stepnumber --;
Tioneb 3:dfbd0ce2963a 83 Serial.print("StepNumber = ");
Tioneb 3:dfbd0ce2963a 84 Serial.println(stepnumber);
Tioneb 3:dfbd0ce2963a 85 if (stepnumber == 255) stepnumber = 0;*/
Tioneb 3:dfbd0ce2963a 86 }
Tioneb 3:dfbd0ce2963a 87 }
Tioneb 3:dfbd0ce2963a 88 analogWrite(sstepPin, 0);
Tioneb 3:dfbd0ce2963a 89 sCurrentPos = sTargetPos;
Tioneb 3:dfbd0ce2963a 90 CurrentPos = sCurrentPos;
Tioneb 3:dfbd0ce2963a 91 }
Tioneb 3:dfbd0ce2963a 92
Tioneb 3:dfbd0ce2963a 93 void Cylinder::Update(unsigned char CurrentPos, unsigned char TargetPos) {
Tioneb 3:dfbd0ce2963a 94 sTargetPos = TargetPos;
Tioneb 3:dfbd0ce2963a 95 sCurrentPos = CurrentPos;
Tioneb 3:dfbd0ce2963a 96 Reach_Target();
Tioneb 3:dfbd0ce2963a 97 }
Tioneb 3:dfbd0ce2963a 98
Tioneb 3:dfbd0ce2963a 99 void Cylinder::Origin(int OriginSensor) {
ketingue 4:b01a3ce6ef01 100
ketingue 4:b01a3ce6ef01 101 int periode = 100;
ketingue 4:b01a3ce6ef01 102 DigitalOut test_sortie_pwm(PD0);
ketingue 4:b01a3ce6ef01 103 SendPWM_Carre(periode, test_sortie_pwm)
ketingue 4:b01a3ce6ef01 104 while(DigitalOut(OriginSensor)==true);
ketingue 4:b01a3ce6ef01 105 SendPWM_Nul(test_sortie_pwm);
ketingue 4:b01a3ce6ef01 106 //digitalWrite(sdirPin, LOW);
ketingue 4:b01a3ce6ef01 107 //analogWrite(sstepPin, 127);
ketingue 4:b01a3ce6ef01 108 //while (digitalRead(sOriginSensor) == true);
ketingue 4:b01a3ce6ef01 109 //analogWrite(sstepPin, 0);
ketingue 4:b01a3ce6ef01 110 */
Tioneb 3:dfbd0ce2963a 111 }
Tioneb 3:dfbd0ce2963a 112
Tioneb 3:dfbd0ce2963a 113 #endif