haustorial/prosthesis_controller/prosthesis_controller.ino

115 lines
3.2 KiB
Arduino
Raw Normal View History

2024-09-06 13:33:19 +00:00
#include "definitions.h"
#include "vacuum.h"
#include "servo.h"
2024-09-11 09:40:46 +00:00
#include "bodytemp.h"
#include "behaviour.h"
2024-09-12 09:23:52 +00:00
#include "barometer.h"
2024-09-11 12:19:10 +00:00
#include "imu.h"
2024-09-06 13:33:19 +00:00
void setup() {
Serial.begin(115200);
pinMode(PIN_VIBRATION,OUTPUT);
analogWrite(PIN_VIBRATION,0);
2024-09-06 13:33:19 +00:00
pinMode(PIN_BUTTON,INPUT_PULLDOWN);
2024-09-11 09:40:46 +00:00
pinMode(PIN_LED,OUTPUT);
digitalWrite(PIN_LED,HIGH); //LOW=Light, HIGH=Off
2024-09-06 13:33:19 +00:00
2024-09-12 14:59:47 +00:00
initBehaviour();
2024-09-06 13:33:19 +00:00
initVacuum();
2024-09-11 09:40:46 +00:00
initBodytemp();
2024-09-06 13:33:19 +00:00
2024-09-12 09:23:52 +00:00
initBarometer();
2024-09-11 12:19:10 +00:00
initIMU(7.54,-15.26,-1.41);
2024-09-06 13:33:19 +00:00
2024-09-12 14:59:47 +00:00
pinMode(PIN_INPUTARM1,INPUT_PULLDOWN);
pinMode(PIN_INPUTARM2,INPUT_PULLDOWN);
pinMode(PIN_INPUTARM3,INPUT_PULLDOWN);
2024-09-06 14:51:50 +00:00
pinMode(PIN_INPUTAUX,INPUT);
2024-09-06 14:51:50 +00:00
initServos();
2024-09-06 13:33:19 +00:00
}
void loop() {
unsigned long loopmillis=millis();
2024-09-11 09:40:46 +00:00
bool body_present=checkBodypresence(loopmillis);
2024-09-12 14:59:47 +00:00
//digitalWrite(PIN_LED,!body_present);
2024-09-06 13:33:19 +00:00
loopVacuum(loopmillis);
2024-09-11 12:19:10 +00:00
2024-09-12 09:23:52 +00:00
loopBarometer(loopmillis);
2024-09-11 12:19:10 +00:00
loopIMU(loopmillis);
bool bellyup=isBellyUp();
2024-09-11 09:40:46 +00:00
bool buttonstate=digitalRead(PIN_BUTTON); //Temporary Vacuum Button. HIGH=Pressed
if (buttonstate) { //Button pressed
setVacuum(false); //release
}else{
setVacuum(body_present);
}
bool contact_main=digitalRead(PIN_INPUTARM1); //Sensor on tip of arm
2024-09-11 12:19:10 +00:00
bool contact_peripheralL=digitalRead(PIN_INPUTARM2); //Sensor on tip of arm
bool contact_peripheralR=digitalRead(PIN_INPUTARM3); //Sensor on tip of arm
2024-09-11 15:15:34 +00:00
float pitch=-getAngleY();
float roll=-getAngleX();
2024-09-12 09:23:52 +00:00
2024-09-12 14:59:47 +00:00
float maxAccX=getMaxAccX();
float maxAccY=getMaxAccY();
float maxAccZ=getMaxAccZ();
2024-09-12 09:23:52 +00:00
2024-09-12 14:59:47 +00:00
updateMood(loopmillis,getGyroAngleX(),getGyroAngleY(),getGyroAngleZ(),maxAccX,maxAccY,maxAccZ,bellyup,body_present,contact_main,contact_peripheralL,contact_peripheralR,getBarometerHeight(),getAmbienttemp(),getObjecttemp(),getAveragePumpspeed(),getAverageServoPositionerror());
2024-09-11 15:15:34 +00:00
updateMatrixWeights(loopmillis,bellyup,body_present,contact_main,contact_peripheralL,contact_peripheralR,pitch,roll);
updateServosByWeights(loopmillis,pitch,roll);
2024-09-11 09:40:46 +00:00
2024-09-11 15:15:34 +00:00
loopServos(loopmillis);
2024-09-06 14:51:50 +00:00
2024-09-06 13:33:19 +00:00
2024-09-11 09:40:46 +00:00
2024-09-06 13:33:19 +00:00
//Print Debug Information
static unsigned long last_print=0;
2024-09-12 14:59:47 +00:00
if (loopmillis - last_print >500) {
2024-09-06 13:33:19 +00:00
last_print=loopmillis;
2024-09-06 14:51:50 +00:00
//printVacuumValues();
//Serial.print(" Vac="); Serial.print(vac);
2024-09-11 12:19:10 +00:00
//printServoDebug();
//Serial.println();
2024-09-11 09:40:46 +00:00
//printBodytempDebug();
2024-09-11 12:19:10 +00:00
2024-09-11 09:40:46 +00:00
2024-09-12 09:23:52 +00:00
Serial.print("Servo Error:");Serial.print(getAverageServoPositionerror()); Serial.println();
Serial.print("Avg Pump:");Serial.print(getAveragePumpspeed()); Serial.println();
2024-09-12 14:59:47 +00:00
Serial.print("Height:");Serial.print(getBarometerHeight(),3); Serial.println();
Serial.print("Contact:");Serial.print(contact_main); Serial.print(", "); Serial.print(contact_peripheralL); Serial.print(", "); Serial.print(contact_peripheralR);Serial.println();
2024-09-06 14:51:50 +00:00
2024-09-12 14:59:47 +00:00
//Serial.print("GyroAngle: ");Serial.print(getGyroAngleX()); Serial.print(", ");Serial.print(getGyroAngleY()); Serial.print(", ");Serial.print(getGyroAngleZ()); Serial.println();
float maxAcc=sqrt(pow(maxAccX,2)+pow(maxAccY,2)+pow(maxAccZ,2));
Serial.print("Accel: ");Serial.print(maxAccX); Serial.print(", ");Serial.print(maxAccY); Serial.print(", ");Serial.print(maxAccZ); Serial.print(", max: ");Serial.print(maxAcc); Serial.println();
printMood();
2024-09-06 13:33:19 +00:00
2024-09-12 14:59:47 +00:00
printModes();
2024-09-12 09:23:52 +00:00
Serial.println();
2024-09-06 13:33:19 +00:00
}
}