1 #include "Atmosphere.hpp"
2 #include "ControlMap.hpp"
6 #include "RigidBody.hpp"
8 #include "Thruster.hpp"
10 #include "Airplane.hpp"
15 inline float norm(float f) { return f<1 ? 1/f : f; }
16 inline float abs(float f) { return f<0 ? -f : f; }
21 _pilotPos[0] = _pilotPos[1] = _pilotPos[2] = 0;
44 for(i=0; i<_fuselages.size(); i++)
45 delete (Fuselage*)_fuselages.get(i);
46 for(i=0; i<_tanks.size(); i++)
47 delete (Tank*)_tanks.get(i);
48 for(i=0; i<_thrusters.size(); i++)
49 delete (ThrustRec*)_thrusters.get(i);
50 for(i=0; i<_gears.size(); i++)
51 delete (GearRec*)_gears.get(i);
52 for(i=0; i<_surfs.size(); i++)
53 delete (Surface*)_surfs.get(i);
54 for(i=0; i<_contacts.size(); i++)
55 delete[] (float*)_contacts.get(i);
58 void Airplane::iterate(float dt)
60 // The gear might have moved. Change their aerodynamics.
65 // Consume fuel. This is a really simple implementation that
66 // assumes all engines draw equally from all tanks in proportion
67 // to the amount of fuel stored there. Needs to be fixed, but
68 // that has to wait for a decision as to what the property
69 // interface will look like.
71 float fuelFlow = 0, totalFuel = 0.00001; // <-- overflow protection
72 for(i=0; i<_thrusters.size(); i++)
73 fuelFlow += ((ThrustRec*)_thrusters.get(i))->thruster->getFuelFlow();
74 for(i=0; i<_tanks.size(); i++)
75 totalFuel += ((Tank*)_tanks.get(i))->fill;
76 for(i=0; i<_tanks.size(); i++) {
77 Tank* t = (Tank*)_tanks.get(i);
78 t->fill -= dt * fuelFlow * (t->fill/totalFuel);
85 for(int i=0; i<_thrusters.size(); i++)
86 ((ThrustRec*)_thrusters.get(i))->thruster->setFuelState(false);
89 ControlMap* Airplane::getControlMap()
94 Model* Airplane::getModel()
99 void Airplane::getPilotAccel(float* out)
101 State* s = _model.getState();
104 Glue::geodUp(s->pos, out);
105 Math::mul3(-9.8f, out, out);
107 // The regular acceleration
109 Math::mul3(-1, s->acc, tmp);
110 Math::add3(tmp, out, out);
112 // Convert to aircraft coordinates
113 Math::vmul33(s->orient, out, out);
115 // FIXME: rotational & centripetal acceleration needed
118 void Airplane::setPilotPos(float* pos)
121 for(i=0; i<3; i++) _pilotPos[i] = pos[i];
124 void Airplane::getPilotPos(float* out)
127 for(i=0; i<3; i++) out[i] = _pilotPos[i];
130 int Airplane::numGear()
132 return _gears.size();
135 Gear* Airplane::getGear(int g)
137 return ((GearRec*)_gears.get(g))->gear;
140 void Airplane::updateGearState()
142 for(int i=0; i<_gears.size(); i++) {
143 GearRec* gr = (GearRec*)_gears.get(i);
144 float ext = gr->gear->getExtension();
146 gr->surf->setXDrag(ext);
147 gr->surf->setYDrag(ext);
148 gr->surf->setZDrag(ext);
152 void Airplane::setApproach(float speed, float altitude)
154 // The zero AoA will become a calculated stall AoA in compile()
155 setApproach(speed, altitude, 0);
158 void Airplane::setApproach(float speed, float altitude, float aoa)
160 _approachSpeed = speed;
161 _approachP = Atmosphere::getStdPressure(altitude);
162 _approachT = Atmosphere::getStdTemperature(altitude);
166 void Airplane::setCruise(float speed, float altitude)
168 _cruiseSpeed = speed;
169 _cruiseP = Atmosphere::getStdPressure(altitude);
170 _cruiseT = Atmosphere::getStdTemperature(altitude);
175 void Airplane::setElevatorControl(int control)
177 _approachElevator.control = control;
178 _approachElevator.val = 0;
179 _approachControls.add(&_approachElevator);
182 void Airplane::addApproachControl(int control, float val)
184 Control* c = new Control();
185 c->control = control;
187 _approachControls.add(c);
190 void Airplane::addCruiseControl(int control, float val)
192 Control* c = new Control();
193 c->control = control;
195 _cruiseControls.add(c);
198 int Airplane::numTanks()
200 return _tanks.size();
203 float Airplane::getFuel(int tank)
205 return ((Tank*)_tanks.get(tank))->fill;
208 float Airplane::getFuelDensity(int tank)
210 return ((Tank*)_tanks.get(tank))->density;
213 float Airplane::getTankCapacity(int tank)
215 return ((Tank*)_tanks.get(tank))->cap;
218 void Airplane::setWeight(float weight)
220 _emptyWeight = weight;
223 void Airplane::setWing(Wing* wing)
228 void Airplane::setTail(Wing* tail)
233 void Airplane::addVStab(Wing* vstab)
238 void Airplane::addFuselage(float* front, float* back, float width,
239 float taper, float mid)
241 Fuselage* f = new Fuselage();
244 f->front[i] = front[i];
245 f->back[i] = back[i];
253 int Airplane::addTank(float* pos, float cap, float density)
255 Tank* t = new Tank();
257 for(i=0; i<3; i++) t->pos[i] = pos[i];
260 t->density = density;
261 t->handle = 0xffffffff;
262 return _tanks.add(t);
265 void Airplane::addGear(Gear* gear)
267 GearRec* g = new GearRec();
273 void Airplane::addThruster(Thruster* thruster, float mass, float* cg)
275 ThrustRec* t = new ThrustRec();
276 t->thruster = thruster;
279 for(i=0; i<3; i++) t->cg[i] = cg[i];
283 void Airplane::addBallast(float* pos, float mass)
285 _model.getBody()->addMass(mass, pos);
289 int Airplane::addWeight(float* pos, float size)
291 WeightRec* wr = new WeightRec();
292 wr->handle = _model.getBody()->addMass(0, pos);
294 wr->surf = new Surface();
295 wr->surf->setPosition(pos);
296 wr->surf->setTotalDrag(size*size);
297 _model.addSurface(wr->surf);
298 _surfs.add(wr->surf);
300 return _weights.add(wr);
303 void Airplane::setWeight(int handle, float mass)
305 WeightRec* wr = (WeightRec*)_weights.get(handle);
307 _model.getBody()->setMass(wr->handle, mass);
309 // Kill the aerodynamic drag if the mass is exactly zero. This is
310 // how we simulate droppable stores.
312 wr->surf->setXDrag(0);
313 wr->surf->setYDrag(0);
314 wr->surf->setZDrag(0);
316 wr->surf->setXDrag(1);
317 wr->surf->setYDrag(1);
318 wr->surf->setZDrag(1);
322 void Airplane::setFuelFraction(float frac)
325 for(i=0; i<_tanks.size(); i++) {
326 Tank* t = (Tank*)_tanks.get(i);
327 t->fill = frac * t->cap;
328 _model.getBody()->setMass(t->handle, t->cap * frac);
332 float Airplane::getDragCoefficient()
337 float Airplane::getLiftRatio()
342 float Airplane::getCruiseAoA()
347 float Airplane::getTailIncidence()
349 return _tailIncidence;
352 char* Airplane::getFailureMsg()
357 int Airplane::getSolutionIterations()
359 return _solutionIterations;
362 void Airplane::setupState(float aoa, float speed, State* s)
364 float cosAoA = Math::cos(aoa);
365 float sinAoA = Math::sin(aoa);
366 s->orient[0] = cosAoA; s->orient[1] = 0; s->orient[2] = sinAoA;
367 s->orient[3] = 0; s->orient[4] = 1; s->orient[5] = 0;
368 s->orient[6] = -sinAoA; s->orient[7] = 0; s->orient[8] = cosAoA;
370 s->v[0] = speed; s->v[1] = 0; s->v[2] = 0;
374 s->pos[i] = s->rot[i] = s->acc[i] = s->racc[i] = 0;
376 // Put us 1m above the origin, or else the gravity computation in
381 void Airplane::addContactPoint(float* pos)
383 float* cp = new float[3];
390 float Airplane::compileWing(Wing* w)
392 // The tip of the wing is a contact point
395 addContactPoint(tip);
396 if(w->isMirrored()) {
398 addContactPoint(tip);
401 // Make sure it's initialized. The surfaces will pop out with
402 // total drag coefficients equal to their areas, which is what we
408 for(i=0; i<w->numSurfaces(); i++) {
409 Surface* s = (Surface*)w->getSurface(i);
411 float td = s->getTotalDrag();
414 _model.addSurface(s);
416 float mass = w->getSurfaceWeight(i);
417 mass = mass * Math::sqrt(mass);
420 _model.getBody()->addMass(mass, pos);
426 float Airplane::compileFuselage(Fuselage* f)
428 // The front and back are contact points
429 addContactPoint(f->front);
430 addContactPoint(f->back);
434 Math::sub3(f->front, f->back, fwd);
435 float len = Math::mag3(fwd);
436 float wid = f->width;
437 int segs = (int)Math::ceil(len/wid);
438 float segWgt = len*wid/segs;
440 for(j=0; j<segs; j++) {
441 float frac = (j+0.5f) / segs;
445 scale = f->taper+(1-f->taper) * (frac / f->mid);
447 scale = f->taper+(1-f->taper) * (frac - f->mid) / (1 - f->mid);
451 Math::mul3(frac, fwd, pos);
452 Math::add3(f->back, pos, pos);
454 // _Mass_ weighting goes as surface area^(3/2)
455 float mass = scale*segWgt * Math::sqrt(scale*segWgt);
456 _model.getBody()->addMass(mass, pos);
459 // Make a Surface too
460 Surface* s = new Surface();
462 float sideDrag = len/wid;
463 s->setYDrag(sideDrag);
464 s->setZDrag(sideDrag);
465 s->setTotalDrag(scale*segWgt);
467 // FIXME: fails for fuselages aligned along the Y axis
469 float *x=o, *y=o+3, *z=o+6; // nicknames for the axes
471 y[0] = 0; y[1] = 1; y[2] = 0;
472 Math::cross3(x, y, z);
474 Math::cross3(z, x, y);
475 s->setOrientation(o);
477 _model.addSurface(s);
483 // FIXME: should probably add a mass for the gear, too
484 void Airplane::compileGear(GearRec* gr)
488 // Make a Surface object for the aerodynamic behavior
489 Surface* s = new Surface();
492 // Put the surface at the half-way point on the gear strut, give
493 // it a drag coefficient equal to a square of the same dimension
494 // (gear are really draggy) and make it symmetric. Assume that
495 // the "length" of the gear is 3x the compression distance
496 float pos[3], cmp[3];
497 g->getCompression(cmp);
498 float length = 3 * Math::mag3(cmp);
500 Math::mul3(0.5, cmp, cmp);
501 Math::add3(pos, cmp, pos);
504 s->setTotalDrag(length*length);
507 _model.addSurface(s);
511 void Airplane::compileContactPoints()
513 // Figure it will compress by 20cm
516 comp[0] = 0; comp[1] = 0; comp[2] = DIST;
518 // Give it a spring constant such that at full compression it will
519 // hold up 10 times the planes mass. That's about right. Yeah.
520 float mass = _model.getBody()->getTotalMass();
521 float spring = (1/DIST) * 9.8f * 10.0f * mass;
522 float damp = 2 * Math::sqrt(spring * mass);
525 for(i=0; i<_contacts.size(); i++) {
526 float *cp = (float*)_contacts.get(i);
528 Gear* g = new Gear();
531 g->setCompression(comp);
532 g->setSpring(spring);
537 g->setStaticFriction(0.6f);
538 g->setDynamicFriction(0.5f);
544 void Airplane::compile()
547 ground[0] = 0; ground[1] = 0; ground[2] = 1;
548 _model.setGroundPlane(ground, -100000);
550 RigidBody* body = _model.getBody();
551 int firstMass = body->numMasses();
553 // Generate the point masses for the plane. Just use unitless
554 // numbers for a first pass, then go back through and rescale to
555 // make the weight right.
559 aeroWgt += compileWing(_wing);
560 aeroWgt += compileWing(_tail);
562 for(i=0; i<_vstabs.size(); i++) {
563 aeroWgt += compileWing((Wing*)_vstabs.get(i));
567 for(i=0; i<_fuselages.size(); i++) {
568 aeroWgt += compileFuselage((Fuselage*)_fuselages.get(i));
571 // Count up the absolute weight we have
572 float nonAeroWgt = _ballast;
573 for(i=0; i<_thrusters.size(); i++)
574 nonAeroWgt += ((ThrustRec*)_thrusters.get(i))->mass;
576 // Rescale to the specified empty weight
577 float wscale = (_emptyWeight-nonAeroWgt)/aeroWgt;
578 for(i=firstMass; i<body->numMasses(); i++)
579 body->setMass(i, body->getMass(i)*wscale);
581 // Add the thruster masses
582 for(i=0; i<_thrusters.size(); i++) {
583 ThrustRec* t = (ThrustRec*)_thrusters.get(i);
584 body->addMass(t->mass, t->cg);
587 // Add the tanks, empty for now.
589 for(i=0; i<_tanks.size(); i++) {
590 Tank* t = (Tank*)_tanks.get(i);
591 t->handle = body->addMass(0, t->pos);
594 _cruiseWeight = _emptyWeight + totalFuel*0.5f;
595 _approachWeight = _emptyWeight + totalFuel*0.2f;
599 // Add surfaces for the landing gear.
600 for(i=0; i<_gears.size(); i++)
601 compileGear((GearRec*)_gears.get(i));
603 // The Thruster objects
604 for(i=0; i<_thrusters.size(); i++) {
605 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
606 tr->handle = _model.addThruster(tr->thruster);
611 float gespan = _wing->getGroundEffect(gepos);
612 _model.setGroundEffect(gepos, gespan, 0.15f);
617 // Do this after solveGear, because it creates "gear" objects that
618 // we don't want to affect.
619 compileContactPoints();
622 void Airplane::solveGear()
625 _model.getBody()->getCG(cg);
627 // Calculate spring constant weightings for the gear. Weight by
628 // the inverse of the distance to the c.g. in the XY plane, which
629 // should be correct for most gear arrangements. Add 50cm of
630 // "buffer" to keep things from blowing up with aircraft with a
631 // single gear very near the c.g. (AV-8, for example).
634 for(i=0; i<_gears.size(); i++) {
635 GearRec* gr = (GearRec*)_gears.get(i);
638 Math::sub3(cg, pos, pos);
639 gr->wgt = 1.0f/(0.5f+Math::sqrt(pos[0]*pos[0] + pos[1]*pos[1]));
643 // Renormalize so they sum to 1
644 for(i=0; i<_gears.size(); i++)
645 ((GearRec*)_gears.get(i))->wgt /= total;
647 // The force at max compression should be sufficient to stop a
648 // plane moving downwards at 2x the approach descent rate. Assume
649 // a 3 degree approach.
650 float descentRate = 2.0f*_approachSpeed/19.1f;
652 // Spread the kinetic energy according to the gear weights. This
653 // will results in an equal compression fraction (not distance) of
655 float energy = 0.5f*_approachWeight*descentRate*descentRate;
657 for(i=0; i<_gears.size(); i++) {
658 GearRec* gr = (GearRec*)_gears.get(i);
659 float e = energy * gr->wgt;
661 gr->gear->getCompression(comp);
662 float len = Math::mag3(comp);
664 // Energy in a spring: e = 0.5 * k * len^2
665 float k = 2 * e / (len*len);
667 gr->gear->setSpring(k * gr->gear->getSpring());
669 // Critically damped (too damped, too!)
670 gr->gear->setDamping(2*Math::sqrt(k*_approachWeight*gr->wgt)
671 * gr->gear->getDamping());
673 // These are pretty generic
674 gr->gear->setStaticFriction(0.8f);
675 gr->gear->setDynamicFriction(0.7f);
679 void Airplane::initEngines()
681 for(int i=0; i<_thrusters.size(); i++) {
682 ThrustRec* tr = (ThrustRec*)_thrusters.get(i);
683 tr->thruster->init();
687 void Airplane::stabilizeThrust()
690 for(i=0; i<_thrusters.size(); i++)
691 _model.getThruster(i)->stabilize();
694 void Airplane::runCruise()
696 setupState(_cruiseAoA, _cruiseSpeed, &_cruiseState);
697 _model.setState(&_cruiseState);
698 _model.setAir(_cruiseP, _cruiseT,
699 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
701 // The control configuration
704 for(i=0; i<_cruiseControls.size(); i++) {
705 Control* c = (Control*)_cruiseControls.get(i);
706 _controls.setInput(c->control, c->val);
708 _controls.applyControls(1000000); // Huge dt value
712 Math::mul3(-1, _cruiseState.v, wind);
713 Math::vmul33(_cruiseState.orient, wind, wind);
715 // Cruise is by convention at 50% tank capacity
716 setFuelFraction(0.5);
718 // Set up the thruster parameters and iterate until the thrust
720 for(i=0; i<_thrusters.size(); i++) {
721 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
723 t->setAir(_cruiseP, _cruiseT,
724 Atmosphere::calcStdDensity(_cruiseP, _cruiseT));
730 // Precompute thrust in the model, and calculate aerodynamic forces
731 _model.getBody()->recalc();
732 _model.getBody()->reset();
733 _model.initIteration();
734 _model.calcForces(&_cruiseState);
737 void Airplane::runApproach()
739 setupState(_approachAoA, _approachSpeed, &_approachState);
740 _model.setState(&_approachState);
741 _model.setAir(_approachP, _approachT,
742 Atmosphere::calcStdDensity(_approachP, _approachT));
744 // The control configuration
747 for(i=0; i<_approachControls.size(); i++) {
748 Control* c = (Control*)_approachControls.get(i);
749 _controls.setInput(c->control, c->val);
751 _controls.applyControls(1000000);
755 Math::mul3(-1, _approachState.v, wind);
756 Math::vmul33(_approachState.orient, wind, wind);
758 // Approach is by convention at 20% tank capacity
759 setFuelFraction(0.2f);
761 // Run the thrusters until they get to a stable setting. FIXME:
762 // this is lots of wasted work.
763 for(i=0; i<_thrusters.size(); i++) {
764 Thruster* t = ((ThrustRec*)_thrusters.get(i))->thruster;
766 t->setAir(_approachP, _approachT,
767 Atmosphere::calcStdDensity(_approachP, _approachT));
773 // Precompute thrust in the model, and calculate aerodynamic forces
774 _model.getBody()->recalc();
775 _model.getBody()->reset();
776 _model.initIteration();
777 _model.calcForces(&_approachState);
780 void Airplane::applyDragFactor(float factor)
782 float applied = Math::sqrt(factor);
783 _dragFactor *= applied;
784 _wing->setDragScale(_wing->getDragScale() * applied);
785 _tail->setDragScale(_tail->getDragScale() * applied);
787 for(i=0; i<_vstabs.size(); i++) {
788 Wing* w = (Wing*)_vstabs.get(i);
789 w->setDragScale(w->getDragScale() * applied);
791 for(i=0; i<_surfs.size(); i++) {
792 Surface* s = (Surface*)_surfs.get(i);
793 s->setTotalDrag(s->getTotalDrag() * applied);
797 void Airplane::applyLiftRatio(float factor)
799 float applied = Math::sqrt(factor);
800 _liftRatio *= applied;
801 _wing->setLiftRatio(_wing->getLiftRatio() * applied);
802 _tail->setLiftRatio(_tail->getLiftRatio() * applied);
804 for(i=0; i<_vstabs.size(); i++) {
805 Wing* w = (Wing*)_vstabs.get(i);
806 w->setLiftRatio(w->getLiftRatio() * applied);
810 float Airplane::clamp(float val, float min, float max)
812 if(val < min) return min;
813 if(val > max) return max;
817 float Airplane::normFactor(float f)
824 void Airplane::solve()
826 static const float ARCMIN = 0.0002909f;
829 _solutionIterations = 0;
833 printf("%d %f %f %f %f %f\n", //DEBUG
839 _approachElevator.val);
842 if(_solutionIterations++ > 10000) {
843 _failureMsg = "Solution failed to converge after 10000 iterations";
847 // Run an iteration at cruise, and extract the needed numbers:
850 _model.getThrust(tmp);
851 float thrust = tmp[0];
853 _model.getBody()->getAccel(tmp);
854 Math::tmul33(_cruiseState.orient, tmp, tmp);
855 float xforce = _cruiseWeight * tmp[0];
856 float clift0 = _cruiseWeight * tmp[2];
858 _model.getBody()->getAngularAccel(tmp);
859 Math::tmul33(_cruiseState.orient, tmp, tmp);
860 float pitch0 = tmp[1];
862 // Run an approach iteration, and do likewise
865 _model.getBody()->getAngularAccel(tmp);
866 Math::tmul33(_approachState.orient, tmp, tmp);
867 double apitch0 = tmp[1];
869 _model.getBody()->getAccel(tmp);
870 Math::tmul33(_approachState.orient, tmp, tmp);
871 float alift = _approachWeight * tmp[2];
873 // Modify the cruise AoA a bit to get a derivative
874 _cruiseAoA += ARCMIN;
876 _cruiseAoA -= ARCMIN;
878 _model.getBody()->getAccel(tmp);
879 Math::tmul33(_cruiseState.orient, tmp, tmp);
880 float clift1 = _cruiseWeight * tmp[2];
882 // Do the same with the tail incidence
883 _tail->setIncidence(_tailIncidence + ARCMIN);
885 _tail->setIncidence(_tailIncidence);
887 _model.getBody()->getAngularAccel(tmp);
888 Math::tmul33(_cruiseState.orient, tmp, tmp);
889 float pitch1 = tmp[1];
892 float awgt = 9.8f * _approachWeight;
894 float dragFactor = thrust / (thrust-xforce);
895 float liftFactor = awgt / (awgt+alift);
896 float aoaDelta = -clift0 * (ARCMIN/(clift1-clift0));
897 float tailDelta = -pitch0 * (ARCMIN/(pitch1-pitch0));
900 if(dragFactor <= 0 || liftFactor <= 0)
903 // And the elevator control in the approach. This works just
904 // like the tail incidence computation (it's solving for the
905 // same thing -- pitching moment -- by diddling a different
907 const float ELEVDIDDLE = 0.001f;
908 _approachElevator.val += ELEVDIDDLE;
910 _approachElevator.val -= ELEVDIDDLE;
912 _model.getBody()->getAngularAccel(tmp);
913 Math::tmul33(_approachState.orient, tmp, tmp);
914 double apitch1 = tmp[1];
915 float elevDelta = -apitch0 * (ELEVDIDDLE/(apitch1-apitch0));
917 // Now apply the values we just computed. Note that the
918 // "minor" variables are deferred until we get the lift/drag
919 // numbers in the right ballpark.
921 applyDragFactor(dragFactor);
922 applyLiftRatio(liftFactor);
924 // DON'T do the following until the above are sane
925 if(normFactor(dragFactor) > 1.0001
926 || normFactor(liftFactor) > 1.0001)
931 // OK, now we can adjust the minor variables:
932 _cruiseAoA += 0.5f*aoaDelta;
933 _tailIncidence += 0.5f*tailDelta;
935 _cruiseAoA = clamp(_cruiseAoA, -0.175f, 0.175f);
936 _tailIncidence = clamp(_tailIncidence, -0.175f, 0.175f);
938 if(abs(xforce/_cruiseWeight) < 0.0001 &&
939 abs(alift/_approachWeight) < 0.0001 &&
940 abs(aoaDelta) < .000017 &&
941 abs(tailDelta) < .000017)
943 // If this finaly value is OK, then we're all done
944 if(abs(elevDelta) < 0.0001)
947 // Otherwise, adjust and do the next iteration
948 _approachElevator.val += 0.8 * elevDelta;
949 if(abs(_approachElevator.val) > 1) {
950 _failureMsg = "Insufficient elevator to trim for approach";
956 if(_dragFactor < 1e-06 || _dragFactor > 1e6) {
957 _failureMsg = "Drag factor beyond reasonable bounds.";
959 } else if(_liftRatio < 1e-04 || _liftRatio > 1e4) {
960 _failureMsg = "Lift ratio beyond reasonable bounds.";
962 } else if(Math::abs(_cruiseAoA) >= .17453293) {
963 _failureMsg = "Cruise AoA > 10 degrees";
965 } else if(Math::abs(_tailIncidence) >= .17453293) {
966 _failureMsg = "Tail incidence > 10 degrees";
970 }; // namespace yasim