Skip to content

Commit

Permalink
Merge pull request #24 from arduino-libraries/battery_behaviour
Browse files Browse the repository at this point in the history
1.0.4 - Battery alert
  • Loading branch information
gbr1 authored Oct 15, 2024
2 parents a7a796f + 721dc82 commit b4e7353
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 132 deletions.
212 changes: 109 additions & 103 deletions examples/firmware/firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -96,110 +96,113 @@ void loop(){
}
if (packeter.checkPayload()) {
code = packeter.payloadTop();
switch (code){
case 'J':
packeter.unpacketC2F(code,left,right);
alvik.disableKinematicsMovement();
alvik.disablePositionControl();
alvik.setRpm(left, right);
break;

case 'V':
packeter.unpacketC2F(code,linear,angular);
alvik.disableKinematicsMovement();
alvik.disablePositionControl();
alvik.drive(linear,angular);
break;

case 'W':
packeter.unpacketC2B1F(code,label,control_type,value);
alvik.disableKinematicsMovement();
if (label=='L'){
switch (control_type){
case 'V':
alvik.disablePositionControlLeft();
alvik.setRpmLeft(value);
break;
case 'P':
alvik.setPositionLeft(value);
ack_required=MOVEMENT_LEFT;
ack_check=true;
break;
case 'Z':
alvik.resetPositionLeft(value);
break;
if (!alvik.isBatteryAlert()){
switch (code){
case 'J':
packeter.unpacketC2F(code,left,right);
alvik.disableKinematicsMovement();
alvik.disablePositionControl();
alvik.setRpm(left, right);
break;

case 'V':
packeter.unpacketC2F(code,linear,angular);
alvik.disableKinematicsMovement();
alvik.disablePositionControl();
alvik.drive(linear,angular);
break;

case 'W':
packeter.unpacketC2B1F(code,label,control_type,value);
alvik.disableKinematicsMovement();
if (label=='L'){
switch (control_type){
case 'V':
alvik.disablePositionControlLeft();
alvik.setRpmLeft(value);
break;
case 'P':
alvik.setPositionLeft(value);
ack_required=MOVEMENT_LEFT;
ack_check=true;
break;
case 'Z':
alvik.resetPositionLeft(value);
break;
}
}
}
if (label=='R'){
switch (control_type){
case 'V':
alvik.disablePositionControlRight();
alvik.setRpmRight(value);
break;
case 'P':
alvik.setPositionRight(value);
ack_required=MOVEMENT_RIGHT;
ack_check=true;
break;
case 'Z':
alvik.resetPositionRight(value);
break;
if (label=='R'){
switch (control_type){
case 'V':
alvik.disablePositionControlRight();
alvik.setRpmRight(value);
break;
case 'P':
alvik.setPositionRight(value);
ack_required=MOVEMENT_RIGHT;
ack_check=true;
break;
case 'Z':
alvik.resetPositionRight(value);
break;
}
}
}
break;


case 'A':
packeter.unpacketC2F(code,position_left, position_right);
alvik.disableKinematicsMovement();
alvik.setPosition(position_left, position_right);
ack_required=MOVEMENT_POSITION;
ack_check=true;
break;


case 'S':
packeter.unpacketC2B(code,servo_A,servo_B);
alvik.setServoA(servo_A);
alvik.setServoB(servo_B);
break;

case 'L':
packeter.unpacketC1B(code,leds);
alvik.setAllLeds(leds);
break;

case 'P':
packeter.unpacketC1B3F(code,pid,kp,ki,kd);
if (pid=='L'){
alvik.setKPidLeft(kp,ki,kd);
}
if (pid=='R'){
alvik.setKPidRight(kp,ki,kd);
}
break;

case 'R':
packeter.unpacketC1F(code, value);
alvik.disablePositionControl();
alvik.rotate(value);
ack_required=MOVEMENT_ROTATE;
ack_check=true;
break;

case 'G':
packeter.unpacketC1F(code, value);
alvik.disablePositionControl();
alvik.move(value);
ack_required=MOVEMENT_MOVE;
ack_check=true;
break;

case 'Z':
packeter.unpacketC3F(code, x, y, theta);
alvik.resetPose(x, y, theta);
break;

break;


case 'A':
packeter.unpacketC2F(code,position_left, position_right);
alvik.disableKinematicsMovement();
alvik.setPosition(position_left, position_right);
ack_required=MOVEMENT_POSITION;
ack_check=true;
break;


case 'S':
packeter.unpacketC2B(code,servo_A,servo_B);
alvik.setServoA(servo_A);
alvik.setServoB(servo_B);
break;

case 'L':
packeter.unpacketC1B(code,leds);
alvik.setAllLeds(leds);
break;

case 'P':
packeter.unpacketC1B3F(code,pid,kp,ki,kd);
if (pid=='L'){
alvik.setKPidLeft(kp,ki,kd);
}
if (pid=='R'){
alvik.setKPidRight(kp,ki,kd);
}
break;

case 'R':
packeter.unpacketC1F(code, value);
alvik.disablePositionControl();
alvik.rotate(value);
ack_required=MOVEMENT_ROTATE;
ack_check=true;
break;

case 'G':
packeter.unpacketC1F(code, value);
alvik.disablePositionControl();
alvik.move(value);
ack_required=MOVEMENT_MOVE;
ack_check=true;
break;

case 'Z':
packeter.unpacketC3F(code, x, y, theta);
alvik.resetPose(x, y, theta);
break;
}
}
switch (code){
case 'X':
packeter.unpacketC1B(code, ack_code);
if (ack_code == 'K') {
Expand All @@ -213,6 +216,9 @@ void loop(){
case 1:
alvik.setBehaviour(LIFT_ILLUMINATOR, true);
break;
case 2:
alvik.setBehaviour(BATTERY_ALERT, true);
break;
default:
alvik.setBehaviour(ALL_BEHAVIOURS, false);
}
Expand Down Expand Up @@ -326,7 +332,7 @@ void loop(){
if (millis()-tbattery>1000){
tbattery = millis();
alvik.updateBMS();
msg_size = packeter.packetC1F('p', alvik.getBatteryChargePercentage());
msg_size = packeter.packetC1F('p', alvik.isBatteryCharging()*alvik.getBatteryChargePercentage());
alvik.serial->write(packeter.msg,msg_size);
}
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Arduino_AlvikCarrier
version=1.0.3
version=1.0.4
author=Arduino, Giovanni di Dio Bruno, Lucio Rossi
maintainer=Arduino <[email protected]>
sentence=Library and firmware for Arduino Alvik Carrier board
Expand Down
84 changes: 59 additions & 25 deletions src/Arduino_AlvikCarrier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,20 @@ void Arduino_AlvikCarrier::disconnectExternalI2C(){
/******************************************************************************************************/

int Arduino_AlvikCarrier::beginBMS(){
charging = 0.0;
while(digitalRead(NANO_CHK)==HIGH){}
return bms->begin();
}

void Arduino_AlvikCarrier::updateBMS(){
voltage = bms->readVCell();
state_of_charge = bms->readSoc();
if (bms->isCharging()){
charging = 1.0;
}
else{
charging = -1.0;
}
}


Expand All @@ -287,6 +294,14 @@ float Arduino_AlvikCarrier::getBatteryChargePercentage(){
return state_of_charge;
}

float Arduino_AlvikCarrier::isBatteryCharging(){
return charging;
}

bool Arduino_AlvikCarrier::isBatteryAlert(){
return battery_alert;
}



/******************************************************************************************************/
Expand Down Expand Up @@ -969,43 +984,62 @@ void Arduino_AlvikCarrier::beginBehaviours(){
prev_illuminator_state = illuminator_state;
behaviours = 0;
first_lift = true;
battery_alert_time = millis();
battery_alert_wave = 100;
battery_alert = false;
}


void Arduino_AlvikCarrier::updateBehaviours(){
if (behaviours|=1 == 1){
/*

// illuminator off on lift
if ((1<<(LIFT_ILLUMINATOR-1)) & behaviours){

if (isLifted()&&first_lift){
first_lift = false;
prev_illuminator_state = illuminator_state;
disableIlluminator();
//disableIlluminator();
setIlluminator(LOW);
first_lift=false;
}
if (isLifted()&&!first_lift) {
if (prev_illuminator_state!=0){
disableIlluminator();
else{
if (!isLifted()){
setIlluminator(prev_illuminator_state);
}
}
if (!isLifted()&&!first_lift){
if (prev_illuminator_state!=0){
//first_lift = true;
enableIlluminator();
if (!isLifted()&&!first_lift){
first_lift = true;
}
}
*/
if (isLifted()&&first_lift){
//disableIlluminator();
setIlluminator(LOW);
first_lift=false;
}
else{
if (!isLifted()){
setIlluminator(prev_illuminator_state);
}

// battery alert
if ((1<<(BATTERY_ALERT-1)) & behaviours){
if ((isBatteryCharging()==-1)&&(getBatteryChargePercentage()<BATTERY_ALERT_MINIMUM_CHARGE)){
if (millis()-battery_alert_time>battery_alert_wave){
battery_alert_time = millis();
if (battery_alert_wave==400){
setLeds(COLOR_RED);
setLedBuiltin(HIGH);
battery_alert_wave=100;
}
else{
setLeds(COLOR_BLACK);
setLedBuiltin(LOW);
battery_alert_wave=400;
}
}
if (getBatteryChargePercentage()<BATTERY_ALERT_STOP_CHARGE){
battery_alert = true;
setRpm(0,0);
setIlluminator(false);
}
}
if (!isLifted()&&!first_lift){
first_lift = true;
else{
battery_alert = false;
}
}
}
else{
battery_alert = false;
}

}

void Arduino_AlvikCarrier::setBehaviour(const uint8_t behaviour, const bool enable){
Expand Down
Loading

0 comments on commit b4e7353

Please sign in to comment.