Last active
September 29, 2015 20:02
-
-
Save antonvh/c81c247fc03029a1ba6a to your computer and use it in GitHub Desktop.
Revisions
-
antonvh revised this gist
Sep 29, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,7 +28,7 @@ def get_voltage(): vratio = (last_4 << 6) | first_6 # Now we can calculate the battery voltage like so: ratio = 0.0179 # This is an empyrical value based on several different batteries voltage = vratio * ratio return voltage -
antonvh revised this gist
Sep 29, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,7 +28,7 @@ def get_voltage(): vratio = (last_4 << 6) | first_6 # Now we can calculate the battery voltage like so: ratio = 0.0179 # this is 0.1/5.5V Still have to find out why... voltage = vratio * ratio return voltage -
antonvh revised this gist
Sep 29, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -34,7 +34,7 @@ def get_voltage(): return voltage except: return 0.0 if __name__ == "__main__": print get_voltage() -
antonvh revised this gist
Sep 28, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,7 +21,7 @@ def get_voltage(): # from this data we need the last 4 bits and the first 6. last_4 = data & 0b1111 # using a bit mask first_6 = data >> 10 # right shift 10 because data is 16 bits # together they make the voltage conversion ratio # to make it all easier the last_4 bits are most significant :S -
antonvh revised this gist
Sep 28, 2015 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,10 +11,11 @@ def get_voltage(): :return: voltage (float) """ try: bus = smbus.SMBus(1) # SMBUS 1 because we're using greater than V1. address = 0x48 # time.sleep(0.1) #Is this necessary? # read data from i2c bus. the 0 command is mandatory for the protocol but not used in this chip. data = bus.read_word_data(address, 0) -
antonvh revised this gist
Sep 18, 2015 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,16 +18,16 @@ def get_voltage(): # read data from i2c bus. the 0 command is mandatory for the protocol but not used in this chip. data = bus.read_word_data(address, 0) # from this data we need the last 4 bits and the first 6. last_4 = data & 0b1111 # using a bit mask first_6 = data >> 10 # left shift 10 because data is 16 bits # together they make the voltage conversion ratio # to make it all easier the last_4 bits are most significant :S vratio = (last_4 << 6) | first_6 # Now we can calculate the battery voltage like so: ratio = 0.01818 # this is 0.1/5.5V Still have to find out why... voltage = vratio * ratio return voltage -
antonvh revised this gist
Sep 18, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,7 +24,7 @@ def get_voltage(): # together they make the voltage conversion ratio # to make it all easier the last_4 bits are most significant :S vratio = (last_4 << 6) | first_6 # Now we can calculate the battery voltage like so: ratio = 0.01818 -
antonvh revised this gist
Sep 18, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -35,5 +35,5 @@ def get_voltage(): except: return False if __name__ == "__main__": print get_voltage() -
antonvh renamed this gist
Sep 18, 2015 . 1 changed file with 6 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,10 +27,13 @@ def get_voltage(): vratio = ((last_4 << 6) | first_6) / 1024.0 # Now we can calculate the battery voltage like so: ratio = 0.01818 voltage = vratio * ratio return voltage except: return False if __name__ == "main": print get_voltage() -
antonvh revised this gist
Sep 18, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ import smbus def get_voltage(): """ Reads the digital output code of the MCP3021 chip on the BrickPi+ over i2c. -
antonvh created this gist
Sep 18, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ def get_voltage(): """ Reads the digital output code of the MCP3021 chip on the BrickPi+ over i2c. Some bit operation magic to get a voltage floating number. If this doesnt work try this on the command line: i2cdetect -y 1 The 1 in there is the bus number, same as in bus = smbus.SMBus(1) Google the resulting error. :return: voltage (float) """ bus = smbus.SMBus(1) # SMBUS 1 because we're using greater than V1. address = 0x48 # time.sleep(0.1) #Is this necessary? try: # read data from i2c bus. the 0 command is mandatory for the protocol but not used in this chip. data = bus.read_word_data(address, 0) # from this data we need the last 4 bites and the first 6. last_4 = data & 0b1111 # using a byte mask first_6 = data >> 10 # left shift 10 because data is 16 bits # together they make the voltage conversion ratio # to make it all easier the last_4 bits are most significant :S vratio = ((last_4 << 6) | first_6) / 1024.0 # Now we can calculate the battery voltage like so: RPi_voltage = 5.0 voltage = RPi_voltage / vratio return voltage except: return False