This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
bdev:filamentextrduer_code [2022/11/08 23:12] richard |
bdev:filamentextrduer_code [2022/12/01 03:02] (current) richard |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Filabotextruder code 1 ====== | ====== Filabotextruder code 1 ====== | ||
+ | |||
+ | 5 motor | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | #define motorInterfaceType 1 | ||
+ | |||
+ | |||
+ | struct flowData{ | ||
+ | int bfl; | ||
+ | int dir; | ||
+ | }; | ||
+ | |||
+ | //Define the aggressive and conservative Tuning Parameters | ||
+ | double aggKpA=150, aggKiA=0.5, aggKdA=0; | ||
+ | int thermoelectricval = 255; | ||
+ | int celsius; | ||
+ | int levelstreamon; | ||
+ | double settemp = 0; | ||
+ | double Setpoint, Input, Output, OutputA, OutputB, OutputC; | ||
+ | int tme = 250; | ||
+ | |||
+ | int directionA = 0; | ||
+ | int directionB = 0; | ||
+ | int directionC = 0; | ||
+ | int directionD = 0; | ||
+ | |||
+ | int ton = 0; | ||
+ | |||
+ | int buttonastate = 0; | ||
+ | int buttonbstate = 0; | ||
+ | int buttoncstate = 0; | ||
+ | int buttondstate = 0; | ||
+ | |||
+ | |||
+ | int afl = 0; | ||
+ | int bfl = 0; | ||
+ | int cfl = 0; | ||
+ | int dfl = 0; | ||
+ | |||
+ | String command; | ||
+ | long int currpos; | ||
+ | long int pos; | ||
+ | |||
+ | //Specify the links and initial tuning parameters | ||
+ | PID myPIDA(& | ||
+ | |||
+ | |||
+ | #define THERMISTOR_PINA A0 | ||
+ | #define TEMPCONTROLA 13 | ||
+ | #define PUMP 12 | ||
+ | |||
+ | int PS[] = {26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 45, 47, 49}; | ||
+ | |||
+ | // Define some steppers and the pins the will use | ||
+ | const int enablePinA = PS[0]; | ||
+ | const int directionPinA = PS[1]; | ||
+ | const int stepsPinA = PS[2]; | ||
+ | |||
+ | const int enablePinB = PS[3]; | ||
+ | const int directionPinB = PS[4]; | ||
+ | const int stepsPinB = PS[5]; | ||
+ | |||
+ | const int enablePinC = PS[6]; | ||
+ | const int directionPinC = PS[7]; | ||
+ | const int stepsPinC = PS[8]; | ||
+ | |||
+ | const int enablePinD = PS[9]; | ||
+ | const int directionPinD = PS[10]; | ||
+ | const int stepsPinD = PS[11]; | ||
+ | |||
+ | //const int speedpotA = A1; | ||
+ | //const int gobuttonA = A2; | ||
+ | //const int stopbuttonA = A3; | ||
+ | |||
+ | const int speedpotA = A4; | ||
+ | const int gobuttonA = A5; | ||
+ | const int stopbuttonA = A6; | ||
+ | const int stepspinA = LOW; | ||
+ | |||
+ | const int speedpotB = A7; | ||
+ | const int gobuttonB = A8; | ||
+ | const int stopbuttonB = A9; | ||
+ | const int stepspinB = LOW; | ||
+ | |||
+ | const int speedpotC = A10; | ||
+ | const int gobuttonC = A11; | ||
+ | const int stopbuttonC = A12; | ||
+ | const int stepspinC = LOW; | ||
+ | |||
+ | const int speedpotD = A13; | ||
+ | const int gobuttonD = A14; | ||
+ | const int stopbuttonD = A15; | ||
+ | const int stepspinD = LOW; | ||
+ | |||
+ | AccelStepper stepperA = AccelStepper(motorInterfaceType, | ||
+ | AccelStepper stepperB = AccelStepper(motorInterfaceType, | ||
+ | AccelStepper stepperC = AccelStepper(motorInterfaceType, | ||
+ | AccelStepper stepperD = AccelStepper(motorInterfaceType, | ||
+ | |||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | Serial.begin(115200); | ||
+ | pinMode(stepsPinA, | ||
+ | pinMode(directionPinA, | ||
+ | pinMode(enablePinA, | ||
+ | pinMode(stepsPinB, | ||
+ | pinMode(directionPinB, | ||
+ | pinMode(enablePinB, | ||
+ | pinMode(stepsPinC, | ||
+ | pinMode(directionPinC, | ||
+ | pinMode(enablePinC, | ||
+ | pinMode(stepsPinD, | ||
+ | pinMode(directionPinD, | ||
+ | pinMode(enablePinD, | ||
+ | pinMode(TEMPCONTROLA, | ||
+ | pinMode(PUMP, | ||
+ | digitalWrite(enablePinA, | ||
+ | digitalWrite(enablePinB, | ||
+ | digitalWrite(enablePinC, | ||
+ | digitalWrite(enablePinD, | ||
+ | digitalWrite(directionPinA, | ||
+ | digitalWrite(directionPinB, | ||
+ | digitalWrite(directionPinC, | ||
+ | digitalWrite(directionPinD, | ||
+ | digitalWrite(PUMP, | ||
+ | stepperA.setMaxSpeed(1000*16); | ||
+ | stepperB.setMaxSpeed(1000*16); | ||
+ | stepperC.setMaxSpeed(1000*16); | ||
+ | stepperD.setMaxSpeed(1000*16); | ||
+ | |||
+ | //turn the PID on | ||
+ | myPIDA.SetMode(AUTOMATIC); | ||
+ | } | ||
+ | |||
+ | void loop(){ | ||
+ | |||
+ | int speedA = map(analogRead(speedpotA), | ||
+ | int speedB = map(analogRead(speedpotB), | ||
+ | int speedC = map(analogRead(speedpotC), | ||
+ | int speedD = map(analogRead(speedpotD), | ||
+ | |||
+ | Input = read_temp(" | ||
+ | double gap = abs(Setpoint-Input); | ||
+ | myPIDA.SetTunings(aggKpA, | ||
+ | myPIDA.Compute(); | ||
+ | analogWrite(TEMPCONTROLA, | ||
+ | |||
+ | int rawvalue = analogRead(THERMISTOR_PINA); | ||
+ | int rawvalueA = rawvalue; | ||
+ | float celsiusA = read_temp(" | ||
+ | |||
+ | if (levelstreamon == 1) { | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | /* | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | */ | ||
+ | |||
+ | flowData A = {bfl, directionA}; | ||
+ | flowData *AA = &A; | ||
+ | flowData AAA = motorStop(stopbuttonA, | ||
+ | directionA = AAA.dir; | ||
+ | motorMove(gobuttonA, | ||
+ | |||
+ | flowData B = {bfl, directionB}; | ||
+ | flowData *BB = &B; | ||
+ | flowData BBB = motorStop(stopbuttonB, | ||
+ | directionB = BBB.dir; | ||
+ | motorMove(gobuttonB, | ||
+ | |||
+ | flowData C = {bfl, directionC}; | ||
+ | flowData *CC = &C; | ||
+ | flowData CCC = motorStop(stopbuttonC, | ||
+ | directionC = CCC.dir; | ||
+ | motorMove(gobuttonC, | ||
+ | |||
+ | flowData D = {bfl, directionD}; | ||
+ | flowData *DD = &D; | ||
+ | flowData DDD = motorStop(stopbuttonD, | ||
+ | directionD = DDD.dir; | ||
+ | motorMove(gobuttonD, | ||
+ | |||
+ | if (A.dir == 1) {stepperA.setSpeed(speedA); | ||
+ | if (B.dir == 1) {stepperB.setSpeed(speedB); | ||
+ | if (C.dir == 1) {stepperC.setSpeed(speedC); | ||
+ | if (D.dir == 1) {stepperD.setSpeed(speedD); | ||
+ | |||
+ | |||
+ | stepperD.runSpeed(); | ||
+ | stepperC.runSpeed(); | ||
+ | stepperB.runSpeed(); | ||
+ | stepperA.runSpeed(); | ||
+ | | ||
+ | | ||
+ | char c = Serial.read(); | ||
+ | if (c== ' | ||
+ | currpos = parseCommand(command, | ||
+ | command = ""; | ||
+ | } | ||
+ | else{ | ||
+ | command +=c; | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | |||
+ | void motorMove(int gobutton, int enablePin){ | ||
+ | if (digitalRead(gobutton) == 1){ | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | flowData motorStop(int stopbutton, int enablePin, int directionPin, | ||
+ | if (digitalRead(stopbutton) ==HIGH){ | ||
+ | digitalWrite(enablePin, | ||
+ | if (X->bfl == 0){ | ||
+ | if (X-> | ||
+ | X->bfl = 1; | ||
+ | } | ||
+ | } else { X->bfl = 0; } | ||
+ | |||
+ | Serial.print(" | ||
+ | Serial.println(stopbutton); | ||
+ | return *X; | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | long int parseCommand(String com, long int currpos){ | ||
+ | // | ||
+ | // | ||
+ | |||
+ | if(com.equalsIgnoreCase(" | ||
+ | | ||
+ | } | ||
+ | else if(com.equalsIgnoreCase(" | ||
+ | digitalWrite(PUMP, | ||
+ | } | ||
+ | else if(com.equalsIgnoreCase(" | ||
+ | digitalWrite(PUMP, | ||
+ | } | ||
+ | else if(com.equalsIgnoreCase(" | ||
+ | Serial.println(" | ||
+ | } | ||
+ | else if (com.substring(0, | ||
+ | settemp = com.substring(8).toDouble(); | ||
+ | Setpoint = settemp; | ||
+ | Serial.print(" | ||
+ | Serial.println(settemp); | ||
+ | } | ||
+ | else if(com.equalsIgnoreCase(" | ||
+ | | ||
+ | } | ||
+ | else if(com.equalsIgnoreCase(" | ||
+ | | ||
+ | } | ||
+ | else if(com.equalsIgnoreCase(" | ||
+ | int rawvalue = analogRead(THERMISTOR_PINA); | ||
+ | float celsius = read_temp(" | ||
+ | | ||
+ | | ||
+ | } | ||
+ | else { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | int readpotentionmetersanity(){ | ||
+ | int val = analogRead(speedpotA); | ||
+ | | ||
+ | | ||
+ | val = analogRead(speedpotB); | ||
+ | | ||
+ | | ||
+ | val = analogRead(speedpotC); | ||
+ | | ||
+ | | ||
+ | val = analogRead(speedpotD); | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | |||
+ | #define NUMTEMPS 20 | ||
+ | short temptable[NUMTEMPS][2] = { | ||
+ | {1, 841}, {54, 255}, | ||
+ | {107, 209}, | ||
+ | {160, 184}, | ||
+ | {213, 166}, | ||
+ | {266, 153}, | ||
+ | {319, 142}, | ||
+ | {372, 132}, | ||
+ | {425, 124}, | ||
+ | {478, 116}, | ||
+ | {531, 108}, | ||
+ | {584, 101}, | ||
+ | {637, 93}, | ||
+ | {690, 86}, | ||
+ | {743, 78}, | ||
+ | {796, 70}, | ||
+ | {849, 61}, | ||
+ | {902, 50}, | ||
+ | {955, 34}, | ||
+ | | ||
+ | }; | ||
+ | |||
+ | float read_temp(String sensor_type) | ||
+ | { | ||
+ | int THERMISTOR_PIN = 0; | ||
+ | if (sensor_type == " | ||
+ | THERMISTOR_PIN = THERMISTOR_PINA; | ||
+ | } | ||
+ | int rawtemp = analogRead(THERMISTOR_PIN); | ||
+ | float current_celsius = 0; | ||
+ | |||
+ | byte i; | ||
+ | for (i=1; i< | ||
+ | { | ||
+ | if (temptable[i][0] > rawtemp) | ||
+ | { | ||
+ | float realtemp | ||
+ | |||
+ | if (realtemp > 255) | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Overflow: We just clamp to 0 degrees celsius | ||
+ | if (i == NUMTEMPS) | ||
+ | | ||
+ | |||
+ | | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
< | < | ||
Line 102: | Line 474: | ||
stepperC.setMaxSpeed(1000*16); | stepperC.setMaxSpeed(1000*16); | ||
+ | // | ||
+ | myPIDA.SetMode(AUTOMATIC); | ||
} | } | ||