Skip to content

Commit

Permalink
fix issue #22
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelvdv committed Jul 13, 2016
1 parent 3932940 commit 9f5170c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
14 changes: 6 additions & 8 deletions Pozyx.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,8 @@ typedef struct __attribute__((packed))_UWB_settings {
* See the reg:POZYX_UWB_PLEN register for more information.
*/
uint8_t plen;
/** the transmit gain. The gain is given in double dBm, i.e., a gain of 2 equals 1dBm. See the reg:POZYX_UWB_GAIN register for more information.*/
int8_t gain;
/** The trim value for the UWB transceiver clock. The value must be lower than 32. See the reg:POZYX_UWB_XTALTRIM register for more information.*/
unsigned trim:8;
/** The transmission gain in dB. Possible values are between 0dB and 33.5dB, with a resolution of 0.5dB. See the reg:POZYX_UWB_GAIN register for more information.*/
float gain_db;
}UWB_settings_t;

/**
Expand Down Expand Up @@ -547,14 +545,14 @@ class PozyxClass
* In order to communicate, two pozyx devices must have exactly the same UWB settings.
* Upon reset, the default UWB settings will be loaded.
*
* @param UWB_settings the new UWB settings
* @param UWB_settings reference to the new UWB settings. If the gain_db is set to 0, it will automatically load the default gain value for the given uwb paramters.
* @param remote_id optional parameter that determines the remote device to be used
*
* @retval #POZYX_SUCCESS success.
* @retval #POZYX_FAIL function failed.
* @retval #POZYX_TIMEOUT function timed out, no response received.
*/
static int setUWBSettings(UWB_settings_t UWB_settings, uint16_t remote_id = NULL);
static int setUWBSettings(UWB_settings_t *UWB_settings, uint16_t remote_id = NULL);

/**
* Set the Ultra-wideband frequency channel.
Expand Down Expand Up @@ -595,7 +593,7 @@ class PozyxClass
* transmitter and the receiver must set the appropriate transmit power.
* Changing the UWB channel will reset the power to the default value.
*
* @param txgain_dB the transmission gain in dB. Possible values are between 0dB and 35dB, with a resolution of 0.5dB.
* @param txgain_dB the transmission gain in dB. Possible values are between 0dB and 33.5dB, with a resolution of 0.5dB.
* @param remote_id optional parameter that determines the remote device to be used.
*
* @retval #POZYX_SUCCESS success.
Expand All @@ -610,7 +608,7 @@ class PozyxClass
* This function obtains the configured UWB transmission power gain. The default value is different for each UWB channel.
* Changing the UWB channel will reset the TX power to the default value.
*
* @param txgain_dB the configured transmission gain in dB. Possible values are between 0dB and 35dB, with a resolution of 0.5dB.
* @param txgain_dB the configured transmission gain in dB. Possible values are between 0dB and 33.5dB, with a resolution of 0.5dB.
* @param remote_id optional parameter that determines the remote device to be used.
*
* @retval #POZYX_SUCCESS success.
Expand Down
64 changes: 43 additions & 21 deletions Pozyx_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,37 +364,52 @@ int PozyxClass::getUWBSettings(UWB_settings_t *UWB_settings, uint16_t remote_id)
{
assert(UWB_settings != NULL);

int status;
uint8_t tmp[4];

if(remote_id == NULL){
return regRead(POZYX_UWB_CHANNEL, (uint8_t *) UWB_settings, sizeof(UWB_settings_t));
status = regRead(POZYX_UWB_CHANNEL, tmp, 4);
}
else{
return remoteRegRead(remote_id, POZYX_UWB_CHANNEL, (uint8_t *) UWB_settings, sizeof(UWB_settings_t));
status = remoteRegRead(remote_id, POZYX_UWB_CHANNEL, tmp, 4);
}

// copy the register data in the UWB_settings structure
memcpy((uint8_t *) UWB_settings, tmp, 3);
UWB_settings->gain_db = ((float)tmp[3])*0.5f;

return status;
}

int PozyxClass::setUWBSettings(UWB_settings_t UWB_settings, uint16_t remote_id)
int PozyxClass::setUWBSettings(UWB_settings_t *UWB_settings, uint16_t remote_id)
{
assert(UWB_settings->channel >= 1);
assert(UWB_settings->channel <= 7);
assert(UWB_settings->channel != 6);

int status;

// first set the uwb channel, bitrate, prf and plen, this will set gain to the default gain computed for these settings
if(remote_id == NULL){
status = regWrite(POZYX_UWB_CHANNEL, (uint8_t *) &UWB_settings, sizeof(UWB_settings_t));
delay(2 * POZYX_DELAY_LOCAL_WRITE);
if (status == POZYX_FAILURE){
return status;
}
status = regWrite(POZYX_UWB_GAIN, (uint8_t *) &(UWB_settings.gain), 1);
delay(POZYX_DELAY_LOCAL_WRITE);
}
else{
status = remoteRegWrite(remote_id, POZYX_UWB_CHANNEL, (uint8_t *) &UWB_settings, sizeof(UWB_settings_t));
status = regWrite(POZYX_UWB_CHANNEL, (uint8_t *) UWB_settings, 3);
delay(2 * POZYX_DELAY_LOCAL_WRITE);
}else{
status = remoteRegWrite(remote_id, POZYX_UWB_CHANNEL, (uint8_t *) UWB_settings, 3);
delay(2 * POZYX_DELAY_REMOTE_WRITE);
if (status == POZYX_FAILURE){
return status;
}
status = remoteRegWrite(remote_id, POZYX_UWB_GAIN, (uint8_t *) &(UWB_settings.gain), 1);
delay(POZYX_DELAY_REMOTE_WRITE);
}

if (status == POZYX_FAILURE){
return status;
}

// afterwards, it is possible to set the gain to a custom value
if(UWB_settings->gain_db > 0.1){
Serial.println(UWB_settings->channel);
Serial.println(UWB_settings->gain_db );
status = setTxPower(UWB_settings->gain_db, remote_id);
}else
getTxPower(&(UWB_settings->gain_db), remote_id);

return status;
}

Expand Down Expand Up @@ -428,17 +443,24 @@ int PozyxClass::getUWBChannel(int* channel_num, uint16_t remote_id)
int PozyxClass::setTxPower(float txgain_dB, uint16_t remote_id)
{
assert(txgain_dB >= 0.0f);
assert(txgain_dB <= 35.0f);
assert(txgain_dB <= 33.5f);

// convert to an int where one unit is 0.5dB
uint8_t doublegain_dB = (int)(2.0*txgain_dB + 0.5f);
int status;

if(remote_id == NULL){
return regWrite(POZYX_UWB_GAIN, &doublegain_dB, 1);
status = regWrite(POZYX_UWB_GAIN, &doublegain_dB, 1);
}
else{
return remoteRegWrite(remote_id, POZYX_UWB_GAIN, &doublegain_dB, 1);
status = remoteRegWrite(remote_id, POZYX_UWB_GAIN, &doublegain_dB, 1);
}

// give the pozyx system some time to change the power
if(status == POZYX_SUCCESS)
delay(1);

return status;
}

int PozyxClass::getTxPower(float* txgain_dB, uint16_t remote_id)
Expand Down

0 comments on commit 9f5170c

Please sign in to comment.