Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Wipy 2.0 and save data on my pc, not in flash memory

    Scheduled Pinned Locked Moved WiPy 2&3
    wipy2.0i2cpycompythonsaving data
    100 Posts 2 Posters 25.6k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • V Offline
      VFlorio94 @robert-hh
      last edited by

      @robert-hh YEs, but i have a file.txt with this content

      
      [169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5, 169.5]  [0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.1875, 0.125, 0.1875]       [-177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.5625, -177.625, -177.5625] 
      

      I want to iterate data by data in this file and compare them with other value trough control 'if'.
      for example I want to use data in the first square brackets and step by step compare them with other numeric value, do you know a way to do this?
      I am not able to convert them in numeric value and compare them one by one, I know that i have to use a 'for' to pass all the value but I am not succeded.

      robert-hhR 2 Replies Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @VFlorio94
        last edited by

        @vflorio94 Can you create the file in a different format, like having one line for each set of data in brackets?

        V 1 Reply Last reply Reply Quote 0
        • robert-hhR Offline
          robert-hh @VFlorio94
          last edited by

          @vflorio94 You can do something like:

          f = open("file.txt", "r")
          line = f.read()
          f.close()
          # now you have the full test in one string
          list = line.split("[")
          # now you have a list of three strings, and an empty one
          # remove the empty ones
          for index, item in enumerate(list):
              if len(item) == 0:
                  del list[index]
          # convert to numeric value
          for index, item in enumerate(list):
              item = item.strip("] ").split(",")
              # now item is a list of numeric strings
              for i, el in enumerate(item):
                  item[i] = float(el)
              list[index] = item
              # now list is a list of lists with numeric values
              # indexing by list[i][j]
          
          1 Reply Last reply Reply Quote 0
          • V Offline
            VFlorio94 @robert-hh
            last edited by

            @robert-hh yes, i'll create a file in which I put one set of data in brackets in a line, then there is a way to extract a single data and do a control on it and then pass to the data in the next position?

            robert-hhR 1 Reply Last reply Reply Quote 0
            • robert-hhR Offline
              robert-hh @VFlorio94
              last edited by

              @vflorio94 In my previous post I showed a code snippet, if all data is in one line. If each bracket is in a separate line, then the code snippet is much simpler.

              f = open("file1.txt", "r")
              list = f.readlines()
              f.close()
              # now you have a list of one string per line
              for index, item in enumerate(list):
                  item = item.strip("[]\n ").split(",")
                  # now item is a list of numeric strings
                  for i, el in enumerate(item):
                      item[i] = float(el)
                  list[index] = item
                  # now list is a list of lists wirth numeric values
              
              V 2 Replies Last reply Reply Quote 0
              • V Offline
                VFlorio94 @robert-hh
                last edited by

                @robert-hh Thank you, I didn't see your comment before. At the end I am succeded in a similar way, now i'll try also yours
                thank you very much

                robert-hhR 1 Reply Last reply Reply Quote 0
                • robert-hhR Offline
                  robert-hh @VFlorio94
                  last edited by

                  @vflorio94 Shortened version, not easy to read:

                  with open("file.txt", "r") as f:
                      list = f.readlines()
                      # now you have a list of one string per line
                      for index, item in enumerate(list):
                          list[index] = [float(el) for el in item.strip("[]\n ").split(",")]
                          # now list is a list of lists with numeric values
                          # accessible with list[i][j]
                  
                  1 Reply Last reply Reply Quote 0
                  • V Offline
                    VFlorio94 @robert-hh
                    last edited by

                    @robert-hh the data now are like this

                    348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000 ,348.750000
                    <class 'str'>
                    

                    so i do:

                    f= open("/sd/dat.txt", "r").read()
                    f.close()
                    f1 = f.split(',')
                    g = []
                    for i in range(1, len(f1)):
                        h = float(f1[i])
                        g.append(h)
                    
                    robert-hhR 1 Reply Last reply Reply Quote 0
                    • robert-hhR Offline
                      robert-hh @VFlorio94
                      last edited by robert-hh

                      Usign a generator instead of the loop, it can be written e.g. like:

                      with open("data.txt", "r") as f:
                          line = f.read()
                          # now you have a list of one string per line
                          g = [float(el) for el in line.strip().split(",")]
                      

                      or even shorter as

                      with open("data.txt", "r") as f:
                          g = [float(el) for el in  f.read().strip().split(",")]
                      

                      The last expression in the bracket is a little bit cryptic, to be read from left to right as:

                      • read the line
                      • strip of leading and trailing whitespace
                      • split it at , into a list of numeric strings, which is the fed into the generator expression.
                      V 1 Reply Last reply Reply Quote 0
                      • V Offline
                        VFlorio94 @robert-hh
                        last edited by

                        @robert-hh thank you, all the code that you wrote run correctly. I understand also your explanation.
                        thank you a lot !!

                        robert-hhR 1 Reply Last reply Reply Quote 0
                        • robert-hhR Offline
                          robert-hh @VFlorio94
                          last edited by robert-hh

                          @vflorio94 A simpler way to store and load is using the ujson module. So if you have a list of values like g, write it with:

                          import ujson
                          with open("data.txt", "w") as f:
                              ujson.dump(g, f)
                          

                          and read it back with:

                          with open("data.txt", "r") as f:
                              g = ujson.load(f)
                          

                          In the micropython.lib, there is also the a pickle module.https://github.com/micropython/micropython-lib/tree/master/pickle

                          V 1 Reply Last reply Reply Quote 1
                          • V Offline
                            VFlorio94 @robert-hh
                            last edited by

                            @robert-hh Thank you.
                            unfortunately I have a new problem with wipy and I2C .

                            In the library for I2C if i set baudrate to 100k it doesn't work.
                            but in the data sheet of IMU it is written that it can work with a baudrate with a maximum of 400k.
                            the error is this one:

                              File "/flash/lib_imu.py", line 382, in get_euler
                              File "/flash/lib_imu.py", line 318, in read_vector
                            OSError: I2C bus error
                            

                            I don't know how i can solve this bus error. Do you have an idea about this topic?

                            robert-hhR 1 Reply Last reply Reply Quote 0
                            • robert-hhR Offline
                              robert-hh @VFlorio94
                              last edited by

                              @vflorio94 Does it work at lower baud rates? if yes, then maybe the pull-up resistors are missing. The SDA and SCL lines must have a pull-up resistor in the range of 4.7 kOhm. Sometimes they are built on the board, sometimes not. The ESP32 has internal pull-up resistors at about 30 kOhm, which is good for lower baud rates.

                              V 1 Reply Last reply Reply Quote 0
                              • V Offline
                                VFlorio94 @robert-hh
                                last edited by

                                @robert-hh It works at lower baudrate like 10K, 15K but for value higher then 20K it doesn't work.

                                robert-hhR 1 Reply Last reply Reply Quote 0
                                • robert-hhR Offline
                                  robert-hh @VFlorio94
                                  last edited by

                                  @vflorio94 Which BN055 module are you using, and how long are the connecting wires?

                                  V robert-hhR 2 Replies Last reply Reply Quote 0
                                  • V Offline
                                    VFlorio94 @robert-hh
                                    last edited by

                                    @robert-hh I use Fusion mode and operating mode: NDOF.
                                    The connecting wires are about 60 cm.
                                    I have to use two I2C and collect data and wrote on a file with a sample rate of 50 Hz.
                                    But if I set a time.sleep(0.2) that I think means 50Hz, from a sample to another it has a big delay.
                                    So i try to increase baudrate as I tell you before.

                                    1 Reply Last reply Reply Quote 0
                                    • robert-hhR Offline
                                      robert-hh @robert-hh
                                      last edited by

                                      @robert-hh said in Wipy 2.0 and save data on my pc, not in flash memory:

                                      Which BN055 module

                                      I was asking about the vendor and make of the BNO055 board.

                                      1 Reply Last reply Reply Quote 0

                                      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                                      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                                      With your input, this post could be even better 💗

                                      Register Login
                                      • 1
                                      • 2
                                      • 3
                                      • 4
                                      • 5
                                      • 5 / 5
                                      • First post
                                        Last post

                                      Pycom on Twitter

                                      Pycom