0

I have 2 txt files with values, one with x coordinates of point1 and one with x coordinates of a point2, point 2 needs to follow point1, and after running the difference between the points through the regulator i need to get speed for point 2 out of the regulator line by line let's say every 8ms since the system is discrete

in short: read line from txt of point1 and point2 go through the regulator write speed for point2, and this every 8ms or slower, doesn't matter

coordinates of the points are listed in txt files line by line, i already have code for the regulator(below) but don't know how to do to do it with txt files since i just started using python

class PID:
    """
    Discrete PID control
    """

    def __init__(self, P=1.3, I=0.3, D=0.0, Derivator=0, Integrator=0, Integrator_max=500, Integrator_min=-500):

        self.Kp=P
        self.Ki=I
        self.Kd=D
        self.Derivator=Derivator
        self.Integrator=Integrator
        self.Integrator_max=Integrator_max
        self.Integrator_min=Integrator_min

        self.set_point=0.0
        self.error=0.0

    def update(self,current_value):
        """
        Calculate PID output value for given reference input and feedback
        """

        self.error = self.set_point - current_value

        self.P_value = self.Kp * self.error
        self.D_value = self.Kd * ( self.error - self.Derivator)
        self.Derivator = self.error

        self.Integrator = self.Integrator + self.error

        if self.Integrator > self.Integrator_max:
            self.Integrator = self.Integrator_max
        elif self.Integrator < self.Integrator_min:
            self.Integrator = self.Integrator_min

        self.I_value = self.Integrator * self.Ki

        PID = self.P_value + self.I_value + self.D_value

        return PID

    def setPoint(self,set_point):
        """
        Initilize the setpoint of PID
        """
        self.set_point = set_point
        self.Integrator=0
        self.Derivator=0

    def setIntegrator(self, Integrator):
        self.Integrator = Integrator

    def setDerivator(self, Derivator):
        self.Derivator = Derivator

    def setKp(self,P):
        self.Kp=P

    def setKi(self,I):
        self.Ki=I

    def setKd(self,D):
        self.Kd=D

    def getPoint(self):
        return self.set_point

    def getError(self):
        return self.error

    def getIntegrator(self):
        return self.Integrator

    def getDerivator(self):
        return self.Derivator
Jonas
  • 121,568
  • 97
  • 310
  • 388
user2481520
  • 1
  • 1
  • 2

1 Answers1

2

Say you have two files containing int -- one per line.
Here is in_1.txt:

12
15
117
1

And here is in_2.txt:

22
25
217
2

Here is step by step how process those files according to your needs.

Step 1: read both input files

The following code will be able to read each file content in its own Python list:

# Read first file
with open("in_1.txt", "rt") as f:
    # in the following line, replace `int` by `float` to read ... floats
    data1 = [int(line) for line in f.readlines()]

# Read second file
with open("in_2.txt", "rt") as f:
    # in the following line, replace `int` by `float` to read ... floats
    data2 = [int(line) for line in f.readlines()]

print "data1 =", data1
print "data2 =", data2

Producing:

data1 = [12, 15, 117, 1]
data2 = [22, 25, 217, 2]

Here I read each file in its own list in order to merge them afterward for sake of simplicity. But, depending of the size of you data files, they could be more memory-friendly ways to read them. Anyway, lets process further to "the merge"...

Step 2a: Concatenating both list

You could append all the items of a list to an other using extends:

data1.extend(data2)
print "data1 =", data1

Producing:

data1 = [12, 15, 117, 1, 22, 25, 217, 2]

Step 3a: Writing data to the output file

Finally, you have to write the content on the list to out.txt one value per line:

with open("out.txt", "wt") as f:
    for i in data1:
        f.write("{0}\n".format(i)) 

You now have that in out.txt:

12
15
117
1
22
25
217
2

Step 2b: producing pairs of data

Say you still have in data1 and data2 the lists found at the end of Step 1. But now, you wish to mate them by pair. Since/if you two lists have the same length, you could use zip (or better for memory efficiency itertools.izip)

pairs = zip(data1, data2)
print data1
print data2
print pairs

Producing the output:

[12, 15, 117, 1]
[22, 25, 217, 2]
[(12, 22), (15, 25), (117, 217), (1, 2)]

Step 3b: Writing to output file, one pair per line, separated by spaces

Finally, say you will have to write those pairs to a "space separated" file:

with open("out.txt", "wt") as f:
    for x, y in pairs:
        f.write("{0} {1}\n".format(x,y))

Producing the output file out.txt:

12 22
15 25
117 217
1 2

Last word

One last words. There is plenty of rooms to "optimize" or otherwise "customize" all those examples. But I think they will show you some basic usage to read/write integers from/to files. And finally, I've done all of this on integers. But if you work with floats, you will have to replace each instance of the word int by float ... and it should work.

Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
  • One more question if its not a problem, can you tell me how to use data from txt file line by line, lets say i have 2 txt files with data in lines and i want to add them and print the result in 3th txt file also line by line – user2481520 Jun 19 '13 at 08:11
  • @user2481520 I've modified my answer to show (much) more details. I hope this will give you enough information to adapt that on your specific needs. – Sylvain Leroux Jun 19 '13 at 09:52