通过XiaoJi UPS做供电,通过Arduino做控制。
通过XiaoJi UPS来感知车辆是否处于启动状态(电瓶供电),如果在启动状态转为熄火状态,则XiaoJi UPS指定针脚会由高电平变为低电瓶,Arduino监测到这种变化时,给树莓派发sudo poweroff指令,(需要判断树莓派完全关机后,写开关机状态的标志位),随后通过继电器断开给树莓派的供电;如果车辆由熄火变为点火状态,XiaoJi UPS会又低电平转为高电瓶,这时可以通过Arduino使继电器给树莓派供电开机。已经开发完成。(蜂鸣器用于当检测到树莓派开关机异常时报警)。此外,车辆熄火后,arduino会进入到LowPower Mode来节约电量(同时干掉了arduino的power led和继电器的power led来节省待机时耗电量)
视频请见http://v.youku.com/v_show/id_XMzYxMTQzMDk5Ng==.html
不是专业开发,代码写的比较烂,开发完也没重构,凑合看,理解意思就行了。
arduino代码:
#include "LowPower.h"
const int wakeUpPin = 2; //用于接收外部中断(中断为UPS产生)
int piUartTxDetectPin = 3; //定义用于连接Raspberry Pi的UART_TX来检测关机完全完成的针脚
int piStartDetectPin = 8; //定于用于连接Raspberry Pi的检测开机完全完成的针脚 (Raspberry Pi开机完成后把其特定pin置为高电平)
int relayPin = 5; //用于控制继电器
int lowPowerDetectPin = 6; //用于通过树莓派IIC检测UPS电量,如果电量低则该针为高电平
int piHaltTriggerPin = 7; //用于向树莓派触发关机指令
int buzzerPin = 4; //蜂鸣器
int ledPin = 13; //定义板载LED针脚
int startCounter = 0; //用于检测开机完全完成的计数器
int haltCounter = 0; //用于检测关机完全完成的计数器
int errorCounter_start = 0; //检测开机过程中未能正常启动
int errorCounter_halt = 0; //检测关机过程中未能正常关机
String raspStatus = "start"; //用于操作树莓派的状态标志
void setup()
{
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); //关闭板载LED
pinMode(piUartTxDetectPin, INPUT); //不支持Pull-down,需要加硬下拉
pinMode(piStartDetectPin, INPUT);
digitalWrite(piStartDetectPin, HIGH); //上拉
pinMode(wakeUpPin, INPUT);
digitalWrite(wakeUpPin, HIGH); //上拉
pinMode(piHaltTriggerPin, OUTPUT);
digitalWrite(piHaltTriggerPin, LOW);
pinMode(lowPowerDetectPin, INPUT);
digitalWrite(lowPowerDetectPin, HIGH); //上拉
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600); //设置串口,仅用于调试目的
delay (200); //开机时针脚电平不稳定,等待500ms再开始执行后续判断逻辑
}
void wakeUp() //中断函数,修改raspStatus状态
{
raspStatus = "start";
//Serial.println("wakeUp");
}
void loop()
{
if (raspStatus == "start"){ //执行开机操作
//Serial.println("raspStatus: start and set relayPin to HIGH");
digitalWrite(relayPin, HIGH); //接通继电器,开始给树莓派供电
//Serial.println(digitalRead(piStartDetectPin));
if (digitalRead(piStartDetectPin) == LOW){ //用于判断树莓派系统是否已经完全启动
startCounter++;
}
else{
startCounter = 0;
errorCounter_start += 1;
}
if (startCounter >= 4){ //树莓派系统已经完全启动
if (digitalRead(wakeUpPin) == HIGH){ //车辆处于点火状态
//Serial.println("System Completely Started!");
raspStatus="CompletelyStarted";
}
else{ //说明在点火后,Raspberry还没有完全开机前,又熄火了
raspStatus = "halt";
digitalWrite(piHaltTriggerPin, HIGH); //将piHaltTriggerPin置为高电平,树莓派读到这个高电平后,将进行halt
//Serial.println("Turn to halt");
}
startCounter = 0;
errorCounter_start = 0;
}
if (errorCounter_start == 20){ //在一定时间内没有完成开机
Serial.println("start_error!");
for(int i=0;i<4;i++){ //播放错误提示音
tone(buzzerPin,494);
delay(150);
noTone(buzzerPin);
delay(100);
}
}
delay (1000);
}
if (raspStatus == "CompletelyStarted"){ //树莓派已经完成启动,处于运行状态
delay(3000); //增加延迟3秒,节省资源。
if (digitalRead(wakeUpPin) == LOW){ //UPS状态输出低电平说明车辆熄火
digitalWrite(piHaltTriggerPin, HIGH); //将piHaltTriggerPin置为高电平,树莓派读到这个高电平后,将进行halt
raspStatus = "halt";
//Serial.println("Car turned off, halt...");
}
if (digitalRead(lowPowerDetectPin) == LOW){ //UPS电量低
digitalWrite(piHaltTriggerPin, HIGH); //将piHaltTriggerPin置为高电平,树莓派读到这个高电平后,将进行halt
raspStatus = "halt";
//Serial.println("UPS low power, halt...");
}
}
if (raspStatus == "halt"){ //判断树莓派完全关闭
//Serial.println(digitalRead(piUartTxDetectPin));
if (digitalRead(piUartTxDetectPin) == LOW){ //判断树莓派完全关闭
haltCounter++;
}
else{
haltCounter = 0;
errorCounter_halt += 1;
}
if (haltCounter >= 10){
raspStatus="CompletelyShutdown";
digitalWrite(piHaltTriggerPin, LOW);
haltCounter = 0;
errorCounter_halt = 0;
//Serial.println("Setting raspStatus to CompletelyShutdown and set relayPin to LOW!");
digitalWrite(relayPin, LOW); //断开继电器开关,停止给树莓派供电
}
if(errorCounter_halt == 15){ //在一定时间内没有完成关机
Serial.println("halt_error!");
for(int i=0;i<4;i++){ //播放错误提示音
tone(buzzerPin,494);
delay(150);
noTone(buzzerPin);
delay(100);
}
raspStatus="CompletelyShutdown";
digitalWrite(piHaltTriggerPin, LOW);
haltCounter = 0;
errorCounter_halt = 0;
digitalWrite(relayPin, LOW); //断开继电器开关,停止给树莓派供电
}
delay (1000);
}
if (raspStatus == "CompletelyShutdown"){ //已经完全关闭并切断Raspberry供电状态;
if (digitalRead(wakeUpPin) == LOW){ //判断车辆处于熄火状态
//Serial.println("attachInterrupt");
attachInterrupt(0, wakeUp, RISING); //开启中断
//Serial.println("Power-Down");
delay (1000); //这里必须要delay,上面的调试println才能输出,否则太快的进入到powerDown还来不及输出到串口
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); //让系统进行Power-Down状态,(系统会停止执行,等待中断,中断触发后,先执行中断函数,然后从本行下面继续执行)
detachInterrupt(0); //一旦系统中断函数执行完,立即解除中断
}
else{ //说明熄火后,还未完全关闭时候,又点火了
raspStatus = "start";
//Serial.println("Turn to start!");
}
}
delay(1000); //为了让Arduino节省资源,不要一直不停的循环,每次循环完停1秒。和上面关机和开机等多项超时判断有关系,不建议调整
}
树莓派代码:
import RPi.GPIO as GPIO
from os import system
import time
import struct
import smbus
import sys
GPIO.setmode(GPIO.BCM)
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,False)
GPIO.setup(17,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(23,GPIO.OUT)
GPIO.output(23,True)
def readVoltage(bus):
"This function returns as float the voltage from the Raspi UPS Hat via the provided SMBus object"
address = 0x36
read = bus.read_word_data(address, 2)
swapped = struct.unpack("
voltage = swapped * 1.25 /1000/16
return voltage
def readCapacity(bus):
"This function returns as a float the remaining capacity of the battery connected to the Raspi UPS Hat via the provided SMBus object"
address = 0x36
read = bus.read_word_data(address, 4)
swapped = struct.unpack("
capacity = swapped/256
return capacity
bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
while True:
count = 0
while GPIO.input(17) == GPIO.HIGH:
count += 1
time.sleep(1)
if count >= 3:
count = 0
system('halt')
# ~ if readCapacity(bus) <= 30:
# ~ temp = readCapacity(bus)
# ~ time.sleep(2)
# ~ if temp > readCapacity(bus):
# ~ GPIO.output(23,False)
time.sleep(2)