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 VFlorio94

      @robert-hh The first time the program terminates without error.
      if I run for the second time the same main program don't go on and give me the message 'memory allocation...'

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

        @robert-hh I suppose that the problem can be the function 'calcolo_position', but i'm not sure, beacuse the message is only which I copy before.
        the first time the program terminates in the right way, with no error and everything it's ok.
        then I run the program newly and it don't work.
        In my opinion, for my experience with c++ for example , I have to search to release the memory allocation and restart with the allocation from the begging each time that i call the function 'calcolo_position(1). but i don't know how. can you help me?
        thank you for your help

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

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

          hen I run the program newly and it don't work.

          How do you restart the program?
          Normally, when objects are not used any more, they are released and garbage collection will reclaim the space. You can fore garbage collection with the call to gc.collect().

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

            @robert-hh to restart the program I mantain the program open and I reuse the command run.
            I put gc.collect() in the code, you can see where in the code that I copy to you before, but the error stay.

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

              @vflorio94 Can you try to start and run the program from a simple terminal emulator like putty or screen. Using the intgrated environments of Atom or VSC may hide or create problems.

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

                @robert-hh Is IDLE Python GUI good?
                now I need to implement code with more lines, to compare the other data that calcolo_position(1) return in the same function 'acquisizione_interval_pitch()'.
                I'm afraid about this, because I think that the error can be show before the end of the acquisition.

                And more I have to use an interval of acquisition more longer than 20 second, like 120 second.

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

                  @vflorio94 You can use any editor you like for writing the code on you PC. The PyCom plugin for upload and run is what may create unexpected result. To sort that out, upload your code with ftp (Filezilla) and use a simple serial terminal to run the code.

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

                    @robert-hh yes, I'll try to use GUI Idle python , but I have some problem with socket

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

                      @robert-hh I have some difficult to use a serial terminal to run the code, because I have 'import machine' in main, so I have to use socket, but I don't understand how.
                      Can you hel me?

                      #Sending
                      import socket
                      
                      DEST_IP = "192.168.4.255"
                      UDP_PORT = 15005
                      sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                      
                      def sending(msg):
                          sock.sendto(msg, (DEST_IP, UDP_PORT))
                      
                      #main
                      import machine 
                      import pycom
                      import time
                      import lib_imu
                      import gc
                      import interval_position
                      import sending
                      #rilevazione posizione centrale corretta
                      print("INIZIO ACQUISIONE")
                      print("Rilevazione posizione corretta")
                      time.sleep(1)
                      position_ok = []
                      position_ok = interval_position.calcolo_position(6, 1)
                      mean_pos_ok = position_ok[0]
                      print("media posizione corretta", mean_pos_ok[2])
                      dev_std_ok = position_ok[1]
                      gc.collect()
                      print("FINE Rilevazione posizione corretta")
                      time.sleep(2)
                      print("Rilevazione posizione errata")
                      time.sleep(1)
                      position_no =[]
                      position_no = interval_position.calcolo_position(6, 2)
                      mean_pos_no = position_no[0]
                      dev_std_no = position_no[1]
                      gc.collect()
                      print("FINE Rilevazione posizione errata")
                      time.sleep(1)
                      print("Inizio test pitch")
                      time.sleep(1)
                      # the function below return 1 if the test is completed correctly
                      p = interval_position.acquisizione_interval_pitch(20, 1,  mean_pos_ok, mean_pos_no, dev_std_no, 3)
                      del position_ok
                      del position_no
                      if p == 1:
                          stringa = "Acquisizione terminata"
                          sending.sending(stringa)
                      
                      robert-hhR 1 Reply Last reply Reply Quote 0
                      • robert-hhR Offline
                        robert-hh @VFlorio94
                        last edited by

                        @vflorio94 Using a serial terminal is not at all in conflict with "import machine". What you might see is, that once you runa program on your device, the REPL prompt is not available any more.

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

                          @robert-hh yes , when I run rhe code in Visual Studio code with this main I have this output :

                          Calcolo media
                          Acquisizione terminata
                          fine prova
                          ╝╝>
                          

                          the program end correctly. And the REPL prompt is not avaiable any more.
                          Now i don't have anymore the message of error with memory allocation.
                          but now I try to complete the code with more lines and more implementation

                          1 Reply Last reply Reply Quote 0
                          • V Offline
                            VFlorio94
                            last edited by

                            @robert-hh And I 'm not able to use serial terminal

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

                              @robert-hh Sorry, but I need your help one more if is possible.
                              I have to acquire data from IMU for a long time ( 20 minuts) and write the data on file.
                              but also this time I have the error

                              ╝Traceback (most recent call last):
                               File "<stdin>", line 22, in <module>
                              MemoryError: memory allocation failed, allocating 2048 bytes
                              ╝>
                              

                              Here there are the simple code lines that I wrote. This error is terrible for me, probably I make an error on how to do this . please , can you Help me please?

                              import machine 
                              import pycom
                              from machine import Timer
                              import time
                              import lib_imu
                              from machine import I2C
                              from machine import PWM
                              
                              chrono = Timer.Chrono()
                              lib_imu.set_configmode()
                              time.sleep(0.07)
                              lib_imu.set_units()
                              time.sleep(0.3)
                              lib_imu.set_mode('ndof')
                              List =[]
                              chrono.start()
                              while 1 :
                                  lap = chrono.read()
                                  if lap <= 1200: 
                                      eul = lib_imu.get_euler()    
                                      List.append(eul[0])
                                      time.sleep(0.02)
                                  else:
                                      print("Salvataggio su file")
                                      file_dati = open("dati360.txt", "w")
                                      file_dati.write(repr(List[1:len(List)]))
                                      file_dati.close()
                                      print("fine")
                                      chrono.stop()
                                      del List
                                      break
                              
                              robert-hhR 1 Reply Last reply Reply Quote 0
                              • robert-hhR Offline
                                robert-hh @VFlorio94
                                last edited by

                                @vflorio94 So you want first to collect for 1200 seconds data, with a delay of 20 ms each. I do not know how long taking a value needs, if have someting in mind like ~100 ms, which give a list of 10000 entries, and then you want to store that. AFAIK, the basic mp object type needs 8 bytes/entry, so you need 80kbytes for the fill table. Being already low on memory, the list append fails soon, when the runtime engine of micropython wants to allocate more memory for the list, roughly after 128 cycles.
                                So no, you simply cannot do that with a WiPY2. You have to write to SD in shorter cycles, even if that would interrupt your data acquisition.
                                Alternatively, use a device with more memory, like a WiPy3.

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

                                  @robert-hh So, if i want to collect as more possible data in 1200 s how frequently can i take values?

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

                                    @vflorio94 You should collect & write at the same time. Since the writikng is buffered, most of the write operations will not take long.

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

                                      @vflorio94 The code could look like:

                                      import machine 
                                      import pycom
                                      from machine import Timer
                                      import time
                                      import lib_imu
                                      from machine import I2C
                                      from machine import PWM
                                      
                                      chrono = Timer.Chrono()
                                      lib_imu.set_configmode()
                                      time.sleep(0.07)
                                      lib_imu.set_units()
                                      time.sleep(0.3)
                                      lib_imu.set_mode('ndof')
                                      with open("dati360.txt", "w") as file_dati:
                                          chrono.start()
                                          lap = chrono.read()
                                          while lap < 1200
                                              file_dati.write(repr(lib_imu.get_euler()[0]))
                                              time.sleep(0.02)
                                              lap = chrono.read()
                                      print("fine")
                                      chrono.stop()
                                      
                                      V 1 Reply Last reply Reply Quote 0
                                      • V Offline
                                        VFlorio94 @robert-hh
                                        last edited by

                                        @robert-hh Thank you, I did in this way and the code run, but the real sample isn't 0.02 s but each 0.035 s. And I don't know why?!

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

                                          @vflorio94 Taking the measurement and writing takes time too. Now you know, it's about 0.015s.

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

                                            @robert-hh Thank you very much for your help. I've never scheduled with Python, so for me is very difficult and I have to do this for my master thesis. For me you were very valuable.
                                            thank you.

                                            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
                                            • 2 / 5
                                            • First post
                                              Last post

                                            Pycom on Twitter

                                            Pycom