00001 // 00002 // Created by tom on 16/05/16. 00003 // 00004 00005 #include<robotican_hardware_interface/Relay.h> 00006 00007 namespace robotican_hardware { 00008 void Relay::update(const DeviceMessage *deviceMessage) { 00009 00010 } 00011 00012 void Relay::write() { 00013 if(isReady()) { 00014 if(_isChange) { 00015 RelaySetState state; 00016 state.length = sizeof(state); 00017 state.checkSum = 0; 00018 state.id = getId(); 00019 state.state = _relayState; 00020 00021 uint8_t *rawData = (uint8_t *) &state; 00022 state.checkSum = _transportLayer->calcChecksum(rawData, state.length); 00023 _transportLayer->write(rawData, state.length); 00024 _isChange = false; 00025 } 00026 } 00027 } 00028 00029 void Relay::buildDevice() { 00030 BuildRelay buildRelay; 00031 buildRelay.length = sizeof(buildRelay); 00032 buildRelay.checkSum = 0; 00033 buildRelay.id = getId(); 00034 buildRelay.pin = _pin; 00035 00036 uint8_t *rawData = (uint8_t*) &buildRelay; 00037 buildRelay.checkSum = _transportLayer->calcChecksum(rawData, buildRelay.length); 00038 _transportLayer->write(rawData, buildRelay.length); 00039 } 00040 00041 void Relay::deviceAck(const DeviceAck *ack) { 00042 Device::deviceAck(ack); 00043 if (isReady()) { 00044 _server = _nodeHandle.advertiseService(_serviceName, &Relay::relayCallback, this); 00045 ros_utils::rosInfo("relay is ready"); 00046 } 00047 else { 00048 ros_utils::rosError("RiCBoard can't build relay object for spme reason, this program will shut down now"); 00049 ros::shutdown(); 00050 } 00051 } 00052 00053 Relay::Relay(byte id, TransportLayer *transportLayer, byte pin, std::string serviceName) 00054 : Device(id, transportLayer) { 00055 _pin = pin; 00056 _serviceName = serviceName; 00057 _relayState = false; 00058 _isChange = true; 00059 00060 } 00061 00062 bool Relay::relayCallback(ric_board::RelayRequest &req, ric_board::RelayResponse &res) { 00063 _relayState = req.req; 00064 res.ack = true; 00065 _isChange = true; 00066 return true; 00067 } 00068 }